Blue Jeans
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
题目链接:http://poj.org/problem?id=3080
题目大意:给出k个串,求这k个串的最长公共子串,如果有多个长度大于等于3的最长公共子串,输出字典序最小的,如果没有输出:no significant commonalities
思路:和POJ-3450一毛一样,二分是否存在长度为mid的公共子串
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
const int N=1000005;
int a[N],id[N],sa[N],rak[N],h[N],c[N],t1[N],t2[N];
bool cmp(int *f,int x,int y,int w){return f[x]==f[y]&&f[x+w]==f[y+w];}
void da(int a[], int sa[], int rak[], int h[], int n, int m)
{
n++;
int i,j,p,*x = t1,*y = t2;
for (i=0; i<m; i++) c[i]=0;
for (i=0; i<n; i++) c[x[i]=a[i]]++;
for (i=1; i<m; i++) c[i]+=c[i-1];
for (i=n-1; i>=0; i--) sa[--c[x[i]]]=i;
for (j=1; j<=n; j<<=1)
{
p=0;
for (i=n-j; i<n; i++) y[p++]=i;
for (i=0; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for (i=0; i<m; i++) c[i]=0;
for (i=0; i<n; i++) c[x[y[i]]]++;
for (i=1; i<m; i++) c[i]+=c[i-1];
for (i=n-1; i>=0; i--) sa[--c[x[y[i]]]]=y[i];
swap(x, y); p=1; x[sa[0]]=0;
for (i=1; i<n; i++) x[sa[i]]=cmp(y, sa[i-1], sa[i], j)?p-1:p++;
if(p>=n) break;
m=p;
}
n--;
for(i=0;i<=n;i++) rak[sa[i]]=i;
int k=0;
for(int i=0;i<n;h[rak[i++]]=k)
for(k=k?k-1:k,j=sa[rak[i]-1];a[i+k]==a[j+k];k++);
}
int vis[4005],n,m,k;
char b[205],s[205];
int fun(int x)
{
memset(vis,0,sizeof(vis));
int cnt=0;
for(int i=2;i<=n;i++)
{
if(h[i]<x)
{
memset(vis,0,sizeof(vis));
cnt=0;
continue;
}
if(!vis[id[sa[i-1]]]) vis[id[sa[i-1]]]=1,cnt++;
if(!vis[id[sa[i]]]) vis[id[sa[i]]]=1,cnt++;
if(cnt==k)
{
for(int j=0;j<x;j++) b[j]=a[sa[i]+j];
b[x]='\0';
return 1;
}
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&k);
n=0,m=127+k;
for(int i=0;i<k;i++)
{
scanf("%s",s);
int l=strlen(s);
for(int j=0;j<l;j++) a[n]=s[j],id[n++]=i;
a[n]='#'+i,id[n++]='#'+i;
}
a[n]=0;
da(a,sa,rak,h,n,m);
int l=3,r=strlen(s),ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(fun(mid))
{
ans=mid;
l=mid+1;
}
else r=mid-1;
}
if(ans) printf("%s\n",b);
else printf("no significant commonalities\n");
}
return 0;
}