AABCD CDAA ASD ASDF
yes no
#include <stdio.h> #include <string.h> #define MAX 100100 char str1[MAX] , str2[MAX] , str3[MAX*2]; int next[MAX] ; void getNext() { int i = -1 , j = 0 ; int len = strlen(str2) ; next[0] = -1 ; while(j<len) { if(i == -1 || str2[i] == str2[j]) { ++i; ++j; next[j] = i ; } else { i = next[i] ; } } } int kmp() { int i = 0 , j = 0 ; int len1 = strlen(str3) , len2 = strlen(str2) ; while(i<len1 && j<len2) { if(j == -1 || str3[i] == str2[j]) { ++i, ++j ; } else j = next[j] ; } if(j == len2) return i-j ; return -1 ; } int main() { while(gets(str1)) { gets(str2) ; strcpy(str3,str1) ; strcpy(str3+strlen(str1),str1) ; getNext() ; int ans = kmp() ; if(ans==-1) puts("no") ; else puts("yes") ; } return 0 ; }
#include <stdio.h> #include <string.h> #define MAX 100100 char str1[MAX] , str2[MAX] , str3[MAX*2]; int main() { while(gets(str1)) { gets(str2) ; strcpy(str3,str1) ; strcpy(str3+strlen(str1),str1) ; if(strstr(str3,str2)) puts("yes") ; else puts("no") ; } return 0 ; }
原文地址:http://blog.csdn.net/lionel_d/article/details/44935731