「 「 「字符串算法 」 」 」第 1 1 1章 字符串处理
目录:
A.数字反转
B.位移包换
C.单词替换
D.字符串环
E.生日相同
这题也太屑了……
A . A. A. 例题 1 1 1 数字反转
分析:
可以直接用函数 s t r r e v strrev strrev 再处理一下 0 0 0
也可以直接转换
CODE:
#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. 例题 2 2 2 位移包换
分析:
一道咳咳题
把最长的字符串复制一遍 再找短的即可.
CODE:
#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. 例题 3 3 3 单词替换
分析:
其实可以分别输入每个单词 然后直接判断相等 就替换即可.
CODE:
#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. 例题 4 4 4 字符串环
分析:
可以 m o d mod mod字符串长度 来更新匹配位置 就不用复制两个字符串了 直接做即可.
CODE:
#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;
}
E . E. E. 例题 5 5 5 生日相同
扫描二维码关注公众号,回复:
12302771 查看本文章
分析:
至于考虑相等 不如直接考虑不相等 接着不断比较判断
所以遇到不是同月同日的 直接换行重新输出一行
其余的就跟着输出名字啦 注意 s o r t sort sort
p s : ps: ps:不要用 c h a r char char数组 用 s t r i n g string string
CODE:
#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;
}