POJ 3080 Blue Jeans(KMP:最長連續公共子序列)
Blue Jeans
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 21712 | Accepted: 9636 |
Description
The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM researcher, you have been tasked with writing a program that will find commonalities amongst given snippets of DNA that can be correlated with individual survey information to identify new genetic markers. A DNA base sequence is noted by listing the nitrogen bases in the order in which they are found in the molecule. There are four bases: adenine (A), thymine (T), guanine (G), and cytosine (C). A 6-base DNA sequence could be represented as TAGACC. Given a set of DNA base sequences, determine the longest series of bases that occurs in all of the sequences.
Input
Input to this problem will begin with a line containing a single integer n indicating the number of datasets. Each dataset consists of the following components:
- A single positive integer m (2 <= m <= 10) indicating the number of base sequences in this dataset.
- m lines each containing a single base sequence consisting of 60 bases.
Output
For each dataset in the input, output the longest base subsequence common to all of the given base sequences. If the longest common subsequence is less than three bases in length, display the string "no significant commonalities" instead. If multiple subsequences of the same longest length exist, output only the subsequence that comes first in alphabetical order.
Sample Input
3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 3 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA 3 CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities AGATAC CATCATCAT
Source
找到滿足條件的第一個子串,看剩下的是否匹配,找到字典序最小的匹配長度,這裡採用二分
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
char s[15][100];
char p[100];
char ans[100];
int m,next[100];
int n;
void getfail()
{m=strlen(p);
next[0]=next[1]=0;
for(int i=1;i<m;i++)
{
int j=next[i];
while(j&&p[i]!=p[j])
j=next[j];
next[i+1]=(p[i]==p[j])?j+1:0;
}
}
int find(char*t)
{int j=0;
getfail();
for(int i=0;i<60;i++)
{
while(j&&t[i]!=p[j])
j=next[j];
if(t[i]==p[j])
j++;
if(j==m)
return 1;
}
return 0;
}
bool solve1(int len)
{
for(int i=0;i+len-1<60;i++)
{
strncpy(p,s[0]+i,len);
p[len]=0;
bool ok=true;
for(int j=1;j<n;j++)
if(!find(s[j]))
{ok=false;
break;
}
if(ok)
return true;
}
return false;
}
void solve2(int len)
{bool first=true;
for(int i=0;i+len-1<60;i++)
{
strncpy(p,s[0]+i,len);
p[len]=0;
bool ok=true;
for(int j=1;j<n;j++)
{
if(!find(s[j]))
{
ok=false;
break;
}
}
if(ok)
{
if(first)
{strncpy(ans,p,len+1);
first=false;
}
else if(strcmp(ans,p)>0)
strncpy(ans,p,len+1);
}
}
}
bool solve()
{
int l=3,r=60;
if(!solve1(3))
return false;
while(r>l)
{
int m=l+(r-l+1)/2;
if(solve1(m))
l=m;
else
r=m-1;
}
solve2(l);
return true;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s",s[i]);
if(!solve())
printf("no significant commonalities\n");
else
printf("%s\n",ans);
}
return 0;
}