The
main topic: There are m strings, each string length is 60, find the M string of the longest common substring (continuous), the length can not be less than 3, if the same length has multiple output dictionary order the smallest one.
Analysis: Because the string is not many, and relatively short, resulting in direct violence enumeration of the first string of all substrings, compared to the practice of violence, if the length of the string is more than can not play. The
code is as follows:
====================================================================================
#include <stdio.h>#include<string.h>Const intMAXN =107;Const intOO = 1e9+7;CharS[MAXN][MAXN];intNEXT[MAXN];voidGetNext (Chars[]) { intI=0, j=-1, n=strlen (s); next[0] = -1; while(I <N) {if(j==-1|| s[i]==S[j]) next[++i] = + +J; ElseJ=Next[j]; }}BOOLKMP (CharA[],Chars[]) { intI=0, j=0; intNa=strlen (a), Ns =strlen (s); while(I <Na) { while(j==-1|| (A[i]==s[j] && i<Na)) I+ +, J + +; if(j = = Ns)return true; J=Next[j]; } return false;}intMain () {intT; scanf ("%d", &T); while(t--) { intI, J, Len, M, maxlen= -; CharANS[MAXN] ="Z"; scanf ("%d", &l); for(i=0; i<m; i++) scanf ("%s", S[i]); for(len= -; len>=3; len--) for(i=0; i<=maxlen-len; i++) {///enumerates all substrings of the first string Charb[maxn]={0}; strncpy (b, s[0]+I, Len); GetNext (b); for(j=1; j<m; J + +) { if(KMP (S[j], b) = =false) Break; } if(J==m && strcmp (ans, b) >0) strcpy (ans, b); if(ans[0] !='Z'&& i==maxlen-len) I= -, Len =0;///Jump out of the loop } if(ans[0] =='Z') printf ("no significant commonalities\n"); Elseprintf ("%s\n", ans); } return 0;}
Blue jeans-poj 3080 (multiple strings of common substrings)