题意:有多个基因碱基序列(只由AGCT组成),在这些序列中找出一个最长的公共子串,如果长度相等,优先输出字典序较小的串。
思路:由于数据较小,所以直接枚举第一个串中的子串,然后依次对之后的串每次作一次KMP判断是否匹配即可。
注意:当串的长度相同时,要优先输出字典序较小的串。(尼玛最开始没看到好蛋疼)
# include<iostream>
# include<string>
# include<cstdio>
using namespace std;
char st1[1000],st2[10000],str[10000][10000];
int next[10010];
int num,l1,l2,i,j,k,start,end;
void kmp1()//求next值
{
int p,q;
q=-1;
next[0]=-1;
for (p=1;p<=l2-1;p++)
{
while (q>-1&&st2[q+1]!=st2[p])
q=next[q];
if (st2[q+1]==st2[p])
q++;
next[p]=q;
}
}
void kmp2()//KMP函数
{
int p,q;
q=-1;
for (p=0;p<=l1-1;p++)
{
while (q>-1&&str[k][p]!=st2[q+1])
q=next[q];
if (str[k][p]==st2[q+1])
q++;
if (q==l2-1)
{
num++;//如果当前串与模式串匹配则num++
break;
}
}
}
int main(void)
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,t;
cin>>t;
while(t--)
{
int ok=0,max=-1;
start=end=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%s",str[i]);
//枚举第一个串中的每个子串作为模式串
for(i=0;i<=57;i++)
for(j=i+2;j<=59;j++)//模式串从i开始到j
{
l2=j-i+1;//l2为模式串长度
l1=60;//l1为主串长度
int count=i;
for(k=0;k<l2;k++)//将模式串存入str数组
st2[k]=str[1][count++];
kmp1();//求出模式串的next值
num=0;
for(k=2;k<=n;k++)//对之后每一个串求一次KMP
kmp2();
if(num==n-1)
{
ok=1;
if(end-start<j-i)
{
start=i;
end=j;
}
else if(end-start==j-i)//当两个模式串相等时要保证字典序
for(k=0;k<=end-start;k++)
if(str[1][start+k]>str[1][i+k])
start=i,end=j;
}
}
if(ok)
{
for(int temp=start;temp<=end;temp++)
printf("%c",str[1][temp]);
printf("\n");
}
else
printf("no significant commonalities\n");
}
return 0;
}