http://poj.org/problem?id=3294
Current Contest
Past Contests
Scheduled Contests
Award Contest 2011acy Log Out
Mail:12(12)
Login Log Archive
Language:
Life Forms
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 7797 Accepted: 2133
Description
You may have wondered why most extraterrestrial life forms resemble humans, differing by superficial traits such as height, colour, wrinkles, ears, eyebrows and the like. A few bear no human resemblance; these typically have geometric or amorphous shapes like cubes, oil slicks or clouds of dust.
The answer is given in the 146th episode of Star Trek - The Next Generation, titled The Chase. It turns out that in the vast majority of the quadrant's life forms ended up with a large fragment of common DNA.
Given the DNA sequences of several life forms represented as strings of letters, you are to find the longest substring that is shared by more than half of them.
Input
Standard input contains several test cases. Each test case begins with 1 ≤ n ≤ 100, the number of life forms. n lines follow; each contains a string of lower case letters representing the DNA sequence of a life form. Each DNA sequence contains at least one and not more than 1000 letters. A line containing 0 follows the last test case.
Output
For each test case, output the longest string or strings shared by more than half of the life forms. If there are many, output all of them in alphabetical order. If there is no solution with at least one letter, output "?". Leave an empty line between test cases.
Sample Input
3
abcdefg
bcdefgh
cdefghi
3
xxx
yyy
zzz
0
Sample Output
bcdefg
cdefgh
?
Source
Waterloo Local Contest, 2006.9.30
[Submit] [Go Back] [Status] [Discuss]
Home Page Go Back To top
解析:
题意:
给出几组字符串,求在出现次数K次以上时得到的最长公共子串,并且按照字典序输出
思路:
利用后缀数组做。
1.用不同的字符把所有字符拼起来,构造后缀树组,
2。二分答案,每次取得都是公共子串的长度
3.判断是否有一个长度为p的字符串在超过一般的串中出现,当每当height[i]小于p时就重新开一个新段
PS:看到解题思路时脑子一片空白,什么也不清楚,于是照着KB大神的代码逐行读,才搞清楚的
在半知半解的情况下照着写了,发现很多坑。。。。。。。。。
Accepted 3308K 1157MS C++ 3007B 2013-08-06 20:41:25
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include <iostream>
using namespace std;
const int maxn=101010;
int s[maxn];
char st[1010];
int sa[maxn],t[maxn],t2[maxn],c[maxn],height[maxn],rank[maxn];
int id[maxn],vis[110];
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");
}
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;
int flag=1;
while(scanf("%d",&m)!=EOF&&m)
{ if(flag)
{flag=0;}
else printf("\n");
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+130;
id[cnt+len]=-1;
cnt+=len+1;
}
cnt--;
s[cnt]=0;
build_sa(cnt+1,300);
getheight(cnt);
int l=1,r=1010,k=m/2+1;
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<=0)
printf("?\n");
else
{
print(cnt,ans,k);
}
}
return 0;
}