hdu3080(KMP+枚举)

Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10119   Accepted: 4280

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

Source

South Central USA 2006
 
同HDU2328
本题要求多个字符串的最长公共子串。
求最长公共子串是个常见的算法。但本题所求的是多个字符串的,排除常见的两个字符串的DP做法。
可以用后缀数组,但我刚刚接触后缀数组,不能灵活运用。
尝试用KMP+枚举,不料没有超时。
找到最短的字符串,枚举该字符串的子串,然后分别与其他字符串做KMP,找到长度最长且在长度相同的情况下字典序最小的公共子串。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int MAXN=60+10;
char str[10+10][MAXN];
int next[MAXN];

//KMP算法中计算next[]数组
void getNext(char *p)
{
    int j,k,len=strlen(p);
    j=0;
    k=-1;
    next[0]=-1;
    while(j<len)
    {
        if(k==-1||p[j]==p[k])
        {
            next[++j]=++k;
        }
        else k=next[k];
    }
}


//返回模式串T在主串S中首次出现的位置
//返回的位置是从0开始的,若没有找到返回-1。
int KMP_Index(char *W,char *T)
{
    int i=0, j=0,wlen=strlen(W),tlen=strlen(T);
    getNext(W);
    while(i<tlen&&j<wlen)
    {
        if(j==-1||T[i]==W[j])
        {
            i++; j++;
        }
        else
            j=next[j];
    }
    if(j==wlen)
        return i-wlen;
    else
        return -1;
}


int main()
{
	int cas,n,i,j,k,Minlen,Minid;
	char tmp[MAXN],ans[MAXN];
	bool flag;
	cin>>cas;
	while(cas--)
	{
		scanf("%d",&n);
		scanf("%s",str[0]);
		Minlen=strlen(str[0]);
		flag=false;
		for(i=1;i<n;i++)
		{
			scanf("%s",str[i]);
			Minid=0;
			if(strlen(str[i])<Minlen)
			{
				Minid=i;Minlen=strlen(str[i]);
			}
		}
	//	printf("str[Minid]=%s\n",str[Minid]);

		ans[0]=0;
		for(i=Minlen;i>=0;i--)
		{
			for(j=0;j<=Minlen-i;j++)
			{
				strncpy(tmp,str[Minid]+j,i);
				tmp[i]=0;
			//	printf("tmp=%s\n",tmp);
				
				for(k=0;k<n;k++)
				{
					if(k!=Minid)
					{
						if(KMP_Index(tmp,str[k])==-1)
							break;
					}
				}
				
				if(k==n)
				{
					if(strlen(ans)<i)
						strcpy(ans,tmp);
					else if(strlen(ans)==i&&strcmp(ans,tmp)>0)
						strcpy(ans,tmp);
					flag=true;
				}
			}
			//if(flag)break;
		}
	if(strlen(ans)<3)
		printf("no significant commonalities\n");
	else printf("%s\n",ans);
	}
	return  0;
}

你可能感兴趣的:(数据结构,动态规划,KMP)