hdu 2203(kmp)

 1 /*
 2 *  kmp
 3 */
 4 
 5 #include <cstdio>
 6 #include <cstring>
 7 #include <iostream>
 8 
 9 using namespace std;
10 
11 const int N = 100005;
12 
13 char str[N<<1];
14 char pat[N];
15 int next[N];
16 
17 void indexNext() {
18     int k = 0;
19     next[1] = 0;
20     for (int i=2; pat[i]; ++i) {
21         while (k && pat[k+1]!=pat[i]) k = next[k];
22         if (pat[k+1] == pat[i]) ++k;
23         next[i] = k;
24     }
25 }
26 
27 bool kmp() {
28     int k = 0;
29     int lenPat = strlen(pat) - 1;
30     for (int i=1; str[i]; ++i) {
31         while (k && pat[k+1]!=str[i]) k = next[k];
32         if (pat[k+1] == str[i]) ++k;
33         if (k == lenPat) return true;
34     }
35     return false;
36 }
37 
38 int main() {
39     pat[0] = str[0] = '#';
40     while (scanf("%s%s", str+1, pat+1) != EOF) {
41         int len = strlen(str);
42         if (strlen(pat) > len) {puts("no"); continue;}
43         for (int i=1; i<len; ++i) str[len+i-1] = str[i];
44         str[(len<<1)-1] = '\0'; 
45         indexNext();
46         if (kmp()) puts("yes");
47         else puts("no");
48     }
49     return 0;
50 }

 

posted on 2012-05-06 20:28  Try86  阅读(195)  评论(0)    收藏  举报