Link:
http://poj.org/problem?id=3080
The main effect of the topic:
A DNA sequence of M (2<=m<=10) is given to find the longest common string in this m sequence. If there are multiple identical lengths, the output dictionary order is minimal.
Analysis and Summary:
Enumerate all the substrings in turn, and then see if they can match in all the sequences. Save the sequence with the largest length and the smallest dictionary order.
Code:
#include <iostream> #include <cstdio> #include <cstring> using namespace std;
const int MAXN = 1000005;
const int N = 60;
int Nmax;
Char seq[12][85];
Char ans[85];
int f[85];
void Getfail (char *p, int *f) {int m=strlen (p);
f[0]=f[1]=0;
for (int i=1; i<m; ++i) {int j=f[i];
while (J && P[i]!=p[j]) j=f[j];
F[I+1] = p[i]==p[j]?1+j:0;
} bool Find (char *t,char *p,int *f) {getfail (p,f);
int N=strlen (T);
int M=strlen (p);
int j=0;
for (int i=0; i<n; ++i) {while (J && T[i]!=p[j]) j=f[j];
if (T[i]==p[j]) ++j;
if (j==m) {return true;
return false;
int main () {int ncase, m;
Char str[85];
scanf ("%d", &ncase);
while (ncase--) {scanf ("%d", &m);
for (int i=0; i<m; ++i) {scanf ("%s", Seq[i]);
}
BOOL Ok=false;
nmax=3; Enumerate all substrings for (int i=0; i<m; ++i) {for (int j=0; j<n-3; ++j) {//enum start Me
Mset (str, 0, sizeof (str));
for (int k=j,p=0; k<n; ++k) {str[p++]=seq[i][k];
if (P>=nmax) {bool Flag=false;
for (int l=0; l<m; ++l) if (l!=i) {if (!find (seq[l],str,f)) {
Flag=true;
Break
} if (!flag) {if (P==nmax) {
if (!ok) {ok=true;
strcpy (ANS,STR);
else if (strcmp (ANS,STR) >0) { strcpy (ANS,STR); } else if (P>nmax) {Nmax
=p;
strcpy (ANS,STR); } else{break;
If this length does not match, then even longer all cannot match}}}
if (OK) puts (ans);
Else puts ("no significant commonalities");
return 0; }
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/