「」 「文字列アルゴリズム」」「いいえ1第1章文字列処理
ディレクトリ:
A.数字反转
B.位移包换
C.单词替换
D.字符串环
E.生日相同
この質問はぼろぼろすぎます...
A。A。A 。例111つの数値反転
分析:
関数strrevstrrevを直接使用できますs t r r evもう一度やり直してください000
は直接変換することもできます
コード:
#include<iostream>
#include<cstdio>
#pragma GCC optimize(2)
using namespace std;
int numa,numb;
int main(){
for(scanf("%d",&numa);numa!=0;numa/=10)
numb=numb*10+numa%10;
printf("%d",numb);
return 0;
}
B。B。B 。実施例222排気量交換
分析:
咳の質問最長の文字列をコピーし
て、短い文字列を見つけてください。
コード:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
//#pragma GCC optimize(2)
#define reg register
using namespace std;
typedef long long ll;
typedef double db;
string s1,s2;
int main(){
cin>>s1>>s2;
if(s1.length()<s2.length()) swap(s1,s2); //找最长
s1+=s1; //复制
if(s1.find(s2)==-1) puts("false"); //查找
else puts("true");
return 0;
}
C。C。C 。実施例333語の置換
分析:
実際、各単語を個別に入力してから、同等性を直接判断して置き換えることができます。
コード:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#pragma GCC optimize(2)
#define reg register
using namespace std;
typedef long long ll;
typedef double db;
string s[101],a,b;
void work(int x)
{
if(a==s[x]) cout<<b<<" "; //匹配 替换
else cout<<s[x]<<" "; //原样输出
}
int main(){
int n=0;
char ch;
do{
cin>>s[++n];//cout<<"XXX ";
scanf("%c",&ch);
}while(ch==' ');
cin>>a>>b;
for(reg int i=1;i<=n;i++)
work(i);
return 0;
}
D。D。D 。実施例444ストリングループ
分析:
modを変更できますM O Dの文字列の長さに更新するマッチング位置は、あなたがする必要はありませんコピーし、直接2つの文字列を。
コード:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
//#pragma GCC optimize(2)
#define reg register
using namespace std;
typedef long long ll;
typedef double db;
string s1,s2;
int ans,cnt;
int main(){
cin>>s1>>s2;
//s1+=s1;
//s2+=s2;
//if(s1.length()<s2.length()) swap(s1,s2);
int minlen=min(s1.length(),s2.length());
for(int i=0;i<s1.length();i++)
for(int j=0;j<s2.length();j++)
{
int k=i,l=j;
cnt=0;
while(s1[k]==s2[l]) //公共
{
cnt++;
k=(k+1)%s1.length(); //向后更新
l=(l+1)%s2.length();
if(cnt>=minlen) break;
}
ans=max(ans,cnt);
}
printf("%d",ans);
return 0;
}
です。ISです。E 。例555歳の誕生日
分析:
限り等しい直接対価としては一致しないかを決定するために比較絶えず続い
出会いがないので、同じ日の直接交換ラインの再輸出ライン
、それの残りの部分出力の名前とともに、それは注意してソートソートs o r t
ps:ps:P S : Doが使用していない文字文字をC H A Rを有するアレイストリング列s t r i n g
コード:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 100005
using namespace std;
struct st{
int month,day;
string name;
bool operator<(const st& a)const{
if(month!=a.month) return month<a.month;
if(day!=a.day) return day<a.day; //排序
if(name.length()!=a.name.length()) return name.length()<a.name.length();
return name<a.name;
}
}a[MAXN];
int n,next_month,next_day,k;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
cin>>a[i].name;
scanf("%d%d",&a[i].month,&a[i].day);
}
sort(a+1,a+n+1);
next_month=a[1].month,next_day=a[1].day;
for(int i=2;i<=n;i++)
{
if(a[i].month!=next_month||a[i].day!=next_day)
{
next_month=a[i].month;
next_day=a[i].day;
}else k++; //有相同生日
}
if(k)
{
printf("%d %d ",a[1].month,a[1].day);
cout<<a[1].name<<" ";
next_month=a[1].month,next_day=a[1].day;
for(int i=2;i<=n;i++)
{
if(a[i].month!=next_month||a[i].day!=next_day) //不是同生日
{
printf("\n");
next_month=a[i].month;
next_day=a[i].day;
printf("%d %d ",next_month,next_day); //重新输出一行
cout<<a[i].name<<" ";
}
else cout<<a[i].name<<" "; //跟着输出
}
}else puts("None");
return 0;
}