Here are two strings. the prefix of your first string is equal to the maximum length of the suffix of the second string.
Solution: KMP: match twice, but the method is clever. Just find next separately.
Solution code:
1 // file name: getnext. CPP 2 // Author: darkdream 3 // created time: tuesday, September 09, 2014, 02 seconds, 4 5 # include <vector> 6 # include <list> 7 # include <map> 8 # include <set> 9 # include <deque> 10 # include <stack> 11 # include <bitset> 12 # include <algorithm> 13 # include <functional> 14 # include <numeric> 15 # include <utility> 16 # include <sstream> 17 # include <iostream> 18 # include <iomanip> 19 # include <cstdio> 20 # include <cmat H> 21 # include <cstdlib> 22 # include <cstring> 23 # include <ctime> 24 # define ll long long25 26 using namespace STD; 27 char STR [50005]; 28 char str1 [50005]; 29 int next [50005]; 30 int next1 [50005]; 31 void getnext () 32 {33 int Len = strlen (STR ); 34 int len1 = strlen (str1); 35 next [0] =-1; 36 int K =-1; 37 Int J = 0; 38 int sum = 0; 39 while (j <= len-1) 40 {41 if (k =-1 | STR [J] = STR [k]) 42 {43 ++ J; 44 + + K; 45 next [J] = K; 46} 47 else {48 k = next [k]; 49} 50} 51 K = 0; 52 J = 0; 53 While (j <= len1-1) 54 {55 if (k =-1 | STR [k] = str1 [J]) 56 {57 ++ J; 58 + + k; 59 next1 [J] = K; 60} else {61 K = next [k]; 62} 63} 64/* For (INT I = 0; I <= len1; I ++) 65 printf ("% d", next1 [I]); 66 printf ("\ n "); */67 If (next1 [len1] = 0) 68 printf ("0 \ n"); 69 else {70 for (INT I = 0; I <next1 [len1]; I ++) 71 printf ("% C", STR [I]); 72 Printf ("% d \ n", next1 [len1]); 73} 74} 75 int main () {76 while (scanf ("% s", STR )! = EOF) 77 {78 scanf ("% s", str1); 79 getnext (); 80} 81 return 0; 82}
View code
HDU 2594 Simpsons 'den den talents KMP