Life Forms
| Time Limit: 5000MS |
|
Memory Limit: 65536K |
| Total Submissions: 8381 |
|
Accepted: 2294 |
Description
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 has 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 this 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 is to find the longest substring tha T is shared by more than half of them.
Input
Standard input contains several test cases. Each test case is begins with 1≤n≤100 and 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 of the DNA sequence contains at least one and is more than-letters. A line containing 0 follows the last test case.
Output
For each test case, output the longest string or strings gkfx by more than half of the life forms. If There is many, output all of the them in alphabetical order. If There is no solution with at least one letter, output "?". Leave a empty line between test cases.
Sample Input
3
abcdefg
bcdefgh
cdefghi
3
xxx
yyy
zzz
0
Sample Output
BCDEFG
cdefgh
?
Test instructions: K/2 the longest common substring contained in a string
Analysis: Suffix array, all the strings are concatenated with a different delimiter, then the height, the last two to find the length of the sub-string of mid is satisfied, the record satisfies the conditions of all substrings of the first position, the final output can be
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue&
Gt
#include <algorithm> #include <map> #include <iomanip> #define INF 99999999;
typedef long Long LL;
using namespace Std;
const int MAX=200000+10;
int *rank,r[max],sa[max],height[max],size;
int Wa[max],wb[max],wm[max],len[110],pos[max];
BOOL mark[110];
Char S[max];
BOOL CMP (int *r,int a,int b,int l) {return r[a] = = R[b] && r[a+l] = = R[b+l];}
void Makesa (int *r,int *sa,int n,int m) {int *x=wa,*y=wb,*t;
for (int i=0;i<m;++i) wm[i]=0;
for (int i=0;i<n;++i) wm[x[i]=r[i]]++;
for (int i=1;i<m;++i) wm[i]+=wm[i-1];
for (int i=n-1;i>=0;--i) sa[--wm[x[i]]]=i;
for (int. i=0,j=1,p=0;p<n;j=j*2,m=p) {for (p=0,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) wm[i]=0;
for (i=0;i<n;++i) wm[x[y[i]]]++;
for (I=1;i<m;++i) wm[i]+=wm[i-1];
for (I=n-1;i>=0;--i) sa[--wm[x[y[i]]]]=y[i]; For (t=x,x=y,y=t,i=p=1,x[sa[0]]=0;i<n;++i) {x[sa[i]]=cmp (y,sa[i],sa[i-1],j)? p-1:p++;
}} rank=x; } void calheight (int *r,int *sa,int N) {for (int. i=0,j=0,k=0;i<n;height[rank[i++]]=k) {for (k?--K:0,j=sa[rank[i]-1];r
[I+k] = = r[j+k];++k);
}} bool Check (int mid,int n,int k) {memset (mark,false,sizeof mark);
int num=0,ans=0; if (s) the string to which the for (int i=1;i<=n;++i) {if (height[i]>=mid) {(int j=1;j<=size;++j) {///judgment Sa[i] and sa[i-1] has been evaluated
A[I]>LEN[J-1] && sa[i]<len[j]) ans+= (mark[j]?0:1), mark[j]=true;
if (Sa[i-1]>len[j-1] && sa[i-1]<len[j]) ans+= (mark[j]?0:1), mark[j]=true;
}}else{if (ANS>K/2) Pos[++num]=sa[i-1];//pos records the first position of the substring that satisfies the condition ans=0;
memset (mark,false,sizeof mark);
}} if (ANS>K/2) pos[++num]=sa[n];
if (num) {Pos[0]=num;return true;}
return false;
} int main () {int k,n,num=0; len[0]=-1;//Note Len[0] Initialize <0 while (cin>>k,k) {pos[0]=size=n=0;//Note Initialize for (int i=0;i<k;++i) {scanf ("%s",
S+N); for (;S[N]! = ' "; ++n) r[n]=s[n];
R[len[++size]=n++]=300+i;//300+i guarantees that each delimited character is different} r[n-1]=0;
Makesa (r,sa,n,400);
Calheight (r,sa,n-1);
int l=1,r=n,mid;
while (l<=r) {mid=l+r>>1;
if (check (mid,n,k)) l=mid+1;
else r=mid-1;
} if (num++) printf ("\ n");
if (L-1 = = 0) printf ("? \ n");
else{for (int. i=1;i<=pos[0];++i) {for (int j=pos[i];j<pos[i]+l-1;++j) printf ("%c", S[j]);
printf ("\ n");
}}} return 0; }