Question poj1226 (violent, suffix array)
// Obtain the maximum length of a substring for a given n strings, // make it or its reverse string appear in each string. // Train of thought: first obtain the sho string sho [], and then enumerate the answer length ans (LEN ~ 1 ). // Therefore, Sho [] is divided into Len-ans + 1 sub-string POS [], // obtain the reverse string inv [] Of these strings respectively, // determine whether POS [] and inv [] appear in all strings. # Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; struct TT {char s [110]; int Len;} AA [110]; struct TTT {char a [110], B [110]; int Len;} BB [10000]; int CMP (TTT X, TTT y) {return X. len> Y. len ;}; int main () {int t; scanf ("% d", & T); While (t --) {int N; scanf ("% d ", & N); int Minn = 210000, minid = 0; For (INT I = 0; I <n; I ++) {scanf ("% s ", AA [I]. s); AA [I]. len = strlen (AA [I]. s); If (AA [I]. len <Minn) Minn = AA [I]. len, minid = I;} int id = 0; For (INT I = 0; I <Minn; I ++) {for (Int J = I; j <Minn; j ++) {int KK = 0; For (int K = I; k <= J; k ++) bb [ID]. A [Kk ++] = AA [minid]. s [k]; bb [ID]. A [Kk] = '\ 0'; KK = 0; For (int K = J; k> = I; k --) bb [ID]. B [Kk ++] = AA [minid]. s [k]; bb [ID]. B [Kk] = '\ 0'; bb [ID]. len = KK; Id ++ ;}} sort (BB, BB + id, CMP); int exist, ANS = 0; For (int ii = 0; II <ID; II ++) {exist = 1; for (INT I = 0; I <n; I ++) {If (strstr (AA [I]. s, BB [II]. a) = 0 & strstr (AA [I]. s, BB [II]. b) = 0) {exist = 0; break ;}} if (exist) {ans = BB [II]. len; break ;}} printf ("% d \ n", ANS) ;}return 0 ;}
View code
Take a look at the string and leave it as a souvenir.