POJ - 3080 Blue Jeans (暴力kmp)

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
 
 
题意: 就是给你几个长度为60的字符串,让你在这里找到最长的公共字串。
思路: 把第一个字符拆成他的所有字串,然后一个一个的去匹配。
本来写这道题的时候没想到怎么写 (菜),然后去网上看看了才发现要这样写,,太暴力了吧啊,,
然后一个串的所有字串 时间复杂度是n*n*n,据说字典树可以把复杂的降到 n*n,现在还不会写 ,先占个坑把上代码 
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
char ch[11][70];
int nex[70];
void pre_kmp(char str[])
{
	memset(nex,0,sizeof(nex));
//	printf("nex===%s   \n",str);
	int len = strlen(str) , k = -1 ,i = 0;
	nex[0]=-1;
	while(i < len)
	{
		if( k == -1 || nex[i] == nex[k])
		{
			i++;
			k++;
			nex[i]=k;
		}
		else 
		{
			k = nex[k];
		}
	}
}

int kmp(char Str[],char str[])
{
	int Len = strlen (Str);
	int len = strlen (str);
//	printf("  kmp =  %s    %s  \n",Str,str);
	int k = 0 , i = 0;
	while( i < Len)
	{
		if(k == -1 ||Str[i] == str[k])
		{
			i++;
			k++;
		}
		else k = nex[k];
		if( k == len)
		{
			return 1;
		}
	}
	return 0;
}
int cmp(char ch[],char Ch[])
{
	int len = strlen(ch);
	int Len = strlen(Ch);
	if(len != Len)
	{
		return len > Len;
	}
	if(len == Len)
	{
		return strcmp(ch,Ch)<0;
	}
}
int main()
{
	int n;
	scanf("%d",&n);
	while(n--)
	{
		int m;
		char te[70];
		char ans[70];
		scanf("%d",&m);
		for(int i = 0 ; i < m ; i++)
		{
			scanf("%s",ch[i]);
		}
		memset(te,0,sizeof(te));
		memset(ans,0,sizeof(ans));
		int len = strlen (ch[0]);

		for(int i = 0 ; i < len ; i++)
		{
			for(int j = i; j < len; j++)
			{
				int cnt=0;
				memset(te,0,sizeof(te));
				for(int k = 0 ; k < j - i + 1 ; k++)
				{
					te[k] = ch[0][k+i];
				}
				pre_kmp(te);
			//	printf(" TTTTE= %s\n",te);
				int flag = 1;
				for(int pp = 0 ; pp < m; pp++)
				{
				//	printf("te = = =%s   pp =%s\n",te,ch[pp]);
					if(!kmp(ch[pp],te))
					{
						flag = 0;
						break ;
					}
				//	printf("flag = %d \n",flag);	
				//	puts("---------\n");
				} 	
				if(flag)
				{
					if(cmp(te,ans))
					strcpy(ans,te);
				}
			} 
		}
	//	printf("------%s  \n",ans);
		if(strlen(ans)<3)
		{
			puts("no significant commonalities");
		}
		else 
		{
			printf("%s\n",ans);
		}
	}
	return 0;
}
/*
4 3
abcdefgssksjdkausiodsmdjaiowdiwkasdjas
sldiowedskdjaksabcdefgeksjdkaueeeeeees
abcdefgfksjdkauqqqqqqqqqqqqqqqqqqqqqqs*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值