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

本博客探讨了一个基因研究项目中,如何通过编程找出多个DNA序列的最长公共子序列,涉及生物信息学和算法设计。通过实例分析,展示了如何解决实际问题,并介绍了DNA序列的表示方式和算法应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

这道题目是给定几个由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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值