Enumerate all substrings of a string and match other strings.
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 222; const int maxd = 4010; int f [maxn]; char STR [maxn]; char t_str [maxn]; char c_str [maxd] [maxn]; void init_kmp (char * s) {int L = strlen (s ); f [0] = 0; F [1] = 0; For (INT I = 1; I <L; I ++) {Int J = f [I]; while (J & S [I]! = S [J]) J = f [J]; F [I + 1] = s [I] = s [J]? J + 1: 0 ;}} int solve_kmp (char * s, int m) {int L = strlen (s); Int J = 0; For (INT I = 0; I <L; I ++) {While (J & S [I]! = T_str [J]) J = f [J]; If (s [I] = t_str [J]) J ++; If (j = m) return 1;} return 0;} int main () {int N; while (scanf ("% d", & N) {scanf ("% s ", str); // The first input string that matches its substring as a template (INT I = 0; I <n-1; I ++) scanf ("% s", c_str [I]); int L = strlen (STR); int len_max = 0; // The final length of char ret_str [maxn]; // final string for (INT I = L; I> = 1; I --) {// enumerate substrings from small to large in length for (Int J = 0; j + I <= L; j ++) {strncpy (t_str, STR + J, I); // enumerate a substring t_str [I + J] = '\ 0'; // printf ("% d, % d: % s \ n ", i, strlen (t_str), t_str); init_kmp (t_str); // constructor int OK = 1; for (int K = 0; k <n-1; k ++) // match each string if (! Solve_kmp (c_str [K], I) {// if there is no way to match OK = 0; break;} // If (OK) puts (t_str ); if (OK & I> len_max) {len_max = I; strcpy (ret_str, t_str); // puts (t_str );} if (OK & I = len_max & strcmp (ret_str, t_str)> 0) {// puts (t_str); strcpy (ret_str, t_str );}} if (I <len_max + 1) break;} If (! Len_max) printf ("identity lost \ n"); else printf ("% s \ n", ret_str);} return 0 ;}
POJ-3450 impersonate identity