POJ 3080:Blue Jeans:枚举求解n个字符串的最长公共连续子串

Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12468   Accepted: 5407

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
这道题目是给定几个由CAGT构成的字符串,求解这些字符串的公共子串中的最长者。此处想到用dp做公共子串,但是dp是求解两个字符串的最大公共子串,这里是多个字符串。没有想到好的办法,使用枚举法,从长度为60的子串(以第一个字符串为基准,应该只有一个这种子串)开始,判断是否也是后面字符串的子串;若不成立,查询长度为59的子串(应该有两个),知道找到满足所有字符串的子串,输出字典序列较小的即可。
此题目多使用手写函数,若使用C语言的字符函数,将更为便捷。搜索子串使用的KMP算法。同时温习了一下KMP,能够理解,还是很难独自书写完整。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int n,m,lenmax,cbegin;
char a[15][65];
int x,y;
int next[65];
void init()
{
	int i,j;
	scanf("%d",&m);
	for(i=0;i<m;i++)
		scanf("%s",a[i]);
	lenmax=60;
	cbegin=-1;
}
void formNext()
{
	int i,j;
	next[x]=-1;
	i=x;
	j=-1;
	while(i<y)
	{
		if(j==-1)
		{
			i++;
			j=x;
			next[i]=j;
		}
		else
		{
			if(a[0][i]==a[0][j])
			{
				i++;
				j++;
				next[i]=j;
			}
			else
				j=next[j];
		}
	}
}
int KMP(int target)
{
	int i,j;
	formNext();
	i=0;
	j=x;
	while(i<60)
	{
		if(a[0][j]==a[target][i]||j==-1)
		{
			i++;
			if(j==-1)
				j=x;
			else
				j++;
		}
		else
		{
			j=next[j];
		}
		if(j==y+1)
			return 1;
	}
	return -1;
}
int sure()
{
	int i,j;
	for(i=1;i<m;i++)
	{
		if(KMP(i)==-1)
			break;
	}
	if(i==m)
		return 1;
	return 0;
}
void update(int newCbegin)
{
	int i,j;
	if(cbegin==-1)
		cbegin=newCbegin;
	else
	{
		for(i=cbegin,j=newCbegin;i<cbegin+lenmax;i++,j++)
			if(a[0][i]>a[0][j])
			{
				cbegin=newCbegin;
				break;
			}
			else
				if(a[0][i]<a[0][j])
					break;
	}
}
void print()
{
	int i,j;
	for(i=cbegin;i<cbegin+lenmax;i++)
		printf("%c",a[0][i]);
	printf("\n");
}
int main()
{
	int i,j;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<n;i++)
		{
			init();
			for(lenmax=60;lenmax>=3;lenmax--)
			{
				x=0;
				y=x+lenmax-1;
				while(y<60)
				{
					if(sure())
					{
						update(x);
					}
					x++;
					y=x+lenmax-1;
				}
				if(cbegin!=-1)
					break;
			}
			if(cbegin==-1)
				printf("no significant commonalities\n");
			else
				print();
		}
	}
	return 0;
}


你可能感兴趣的:(POJ 3080:Blue Jeans:枚举求解n个字符串的最长公共连续子串)