http://poj.org/problem?id=3080
http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?cid=5414&pid=1012&ojid=1&hide=1&problem=Problem%20L
Blue Jeans
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 4 Accepted Submission(s) : 1
Problem 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
解析:
求最长公共子串,并且长度不少于3(字符串出现次数为k==n)
0 MS 680 KB GNU C++
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include <iostream>
using namespace std;
const int maxn=610;
int s[maxn];
char st[100];
int sa[maxn],t[maxn],t2[maxn],c[maxn],height[maxn],rank[maxn];
int id[maxn],vis[100];
void build_sa(int n,int m)
{
int i,*x=t,*y=t2;
for(i=0;i<m;i++)c[i]=0;
for(i=0;i<n;i++)c[x[i]=s[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(int k=1;k<=n;k<<=1)
{
int p=0;
for(i=n-k;i<n;i++)y[p++]=i;
for(i=0;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k;
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]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k] ?p-1:p++;
if(p>=n)break;
m=p;
}
}
void getheight(int n)
{
int i,j,k=0;
for(i=0;i<n;i++)rank[sa[i]]=i;
for(i=0;i<n;i++)
{
if(k)k--;
j=sa[rank[i]-1];
while(s[i+k]==s[j+k])k++;
//printf("k==%d\n",k);
height[rank[i]]=k;
}
}
int check(int n,int len,int k)
{
int a=0;
memset(vis,0,sizeof(vis));
int x=id[sa[1]];
if(x!=-1&&!vis[x])
{a++;
vis[x]=1;
}
if(a==k)
return 1;
for(int i=2;i<=n;i++)
{
if(height[i]<len)//开劈一个新的段
{
a=0;
memset(vis,0,sizeof(vis));
x=id[sa[i]];
if(x!=-1&&!vis[x])
{
a++;
vis[x]=1;
}
if(a==k)return i;
}
else
{
x=id[sa[i]];
if(x!=-1&&!vis[x])
{
a++;
vis[x]=1;
}
if(a==k)return i;
}
}
return -1;
}
void print(int n,int len,int k)
{
int a=0;
memset(vis,0,sizeof(vis));
int x=id[sa[1]];
if(x!=-1&&!vis[x])
{a++;
vis[x]=1;
}
for(int i=2;i<=n;i++)
{
if(height[i]<len)
{ if(a==k)
{
for(int j=0;j<len;j++)
printf("%c",s[sa[i-1]+j]);
printf("\n");
return;
}
a=0;
memset(vis,0,sizeof(vis));
x=id[sa[i]];
if(x!=-1&&!vis[x])
{
a++;
vis[x]=1;
}
}
else
{
x=id[sa[i]];
if(x!=-1&&!vis[x])
{
a++;
vis[x]=1;
}
}
}
if(a=k)
{for(int j=0;j<len;j++)
printf("%c",s[sa[n]+j]);
printf("\n");}
}
int main()
{
int m,i,j,cnt,ans,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&m);
cnt=0;
for(i=0;i<m;i++)
{
scanf("%s",st);
//printf("st==%s\n",st);
int len=strlen(st);
for(j=0;j<len;j++)
{
s[cnt+j]=st[j];
id[cnt+j]=i;
}
s[cnt+len]=i+100;
id[cnt+len]=-1;
cnt+=len+1;
}
cnt--;
s[cnt]=0;
build_sa(cnt+1,200);
getheight(cnt);
int l=1,r=610,k=m;
int mid;
ans=-1;
while(l<=r)//利用二分法找答案
{mid=(l+r)/2;
int x=check(cnt,mid,k);
if(x==-1)
r=mid-1;
else
{
ans=mid;
l=mid+1;
}
}
if(ans<3)
printf("no significant commonalities\n");
else
{
print(cnt,ans,k);
}
}
return 0;
}