亲和串
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5300 Accepted Submission(s): 2417
Problem Description
人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考,因为他在很小的时候就知道亲和串如何判断了,但是发现,现在长大了却不知道怎么去判断亲和串了,于是他只好又再一次来请教聪明且乐于助人的你来解决这个问题。
亲和串的定义是这样的:给定两个字符串s1和s2,如果能通过s1循环移位,使s2包含在s1中,那么我们就说s2 是s1的亲和串。
亲和串的定义是这样的:给定两个字符串s1和s2,如果能通过s1循环移位,使s2包含在s1中,那么我们就说s2 是s1的亲和串。
Input
本题有多组测试数据,每组数据的第一行包含输入字符串s1,第二行包含输入字符串s2,s1与s2的长度均小于100000。
Output
如果s2是s1的亲和串,则输出"yes",反之,输出"no"。每组测试的输出占一行。
Sample Input
AABCD CDAA ASD ASDF
Sample Output
yes no/*使用string类,很容易的*/#include<iostream> #include<algorithm> #include<string> using namespace std; int main() { string a,b; while(cin>>a){ cin>>b; if(b.length()>a.length()) cout<<"no"<<endl; else{ a=a+a; if(a.find(b)!=-1) cout<<"yes"<<endl; else cout<<"no"<<endl; } } return 0; }
/*KMP算法*/#include<iostream> #include<cstdio> using namespace std ; char a[200005] , b[200005] ,next[200005]; void pre_next(int k ) { int i =0 , j = - 1 ; next[0] = -1 ; while(i<k) { if( j == -1 || a[i] == b[j]) { j++; i++ ; next[i] = j ; } else j = next[j] ; } } bool KMP() { int i = 0 , j = 0 ; int lena = strlen(a) ; int lenb = strlen(b) ; while( i <= lena) { if(a[i]== b[j]) { i++; j++; } else { if(next[j] == - 1) { j = 0 ; i++; } else j= next[j] ; } if(j== lenb) return true; } return false; } int main() { while(scanf("%s",a)!=EOF) { int len=strlen(a); int j = len ; for( int i = 0 ; i < len ; i ++) a[j++] = a[i] ; a[j] ='\0' ; scanf("%s",b); int lenb = strlen(b) ; pre_next(lenb); if(KMP()) printf("yes\n"); else printf("no\n"); } return 0; }