題目:
親和串
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11337 Accepted Submission(s): 5120
親和串的定義是這樣的:給定兩個字符串s1和s2,如果能通過s1循環移位,使s2包含在s1中,那麼我們就說s2 是s1的親和串。
描述:如題面
題解:strstr(str1,str2) 函數用於判斷字符串str2是否是str1的子串。如果是,則該函數返回str2在str1中首次出現的地址;否則,返回NULL。
代碼:
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
#define M 100000
char s1[M],s2[M],s3[2*M];
int main()
{
while(cin>>s1)
{
cin>>s2;
char *p;
int i,len=strlen(s1);
strcpy(s3,s1);
for(i=len;i<2*len;++i)
s3[i]=s1[i-len];
p=strstr(s3,s2);
if(p) cout<<"yes"<<endl;
else cout<<"no"<<endl;
memset(s1,0,sizeof(s1));
memset(s2,0,sizeof(s2));
memset(s3,0,sizeof(s3));
}
}