标签:style blog http io ar color os sp on
题意:
有两个字符串,找一个最长子串是的该串既是第一个字的前缀,又是第二个串的后缀。
分析:
把两个串并起来然后在中间加一个无关字符,求next数组即可。
1 #include <cstdio> 2 #include <cstring> 3 4 const int maxn = 50000 + 10; 5 char s1[maxn * 2], s2[maxn]; 6 int next[maxn * 2], l; 7 8 void get_next() 9 { 10 int k = -1, j = 0; 11 next[0] = -1; 12 while(j < l) 13 { 14 if(k == -1 || s1[k] == s1[j]) 15 { 16 k++; 17 j++; 18 next[j] = k; 19 } 20 else k = next[k]; 21 } 22 } 23 24 int main(void) 25 { 26 //freopen("2594in.txt", "r", stdin); 27 while(scanf("%s%s", s1, s2) == 2) 28 { 29 l = strlen(s1); 30 int l2 = strlen(s2); 31 s1[l] = ‘#‘; 32 s1[l + 1] = ‘\0‘; 33 strcat(s1, s2); 34 l = l + l2 + 1; 35 get_next(); 36 if(next[l] == 0) 37 { 38 puts("0"); 39 continue; 40 } 41 s1[next[l]] = ‘\0‘; 42 printf("%s %d\n", s1, next[l]); 43 } 44 45 return 0; 46 }
HDU 2594 (简单KMP) Simpsons’ Hidden Talents
标签:style blog http io ar color os sp on
原文地址:http://www.cnblogs.com/AOQNRMGYXLMV/p/4123979.html