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

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值