Blue Jeans (HDU_3080) KMP + 枚举

来源:互联网 发布:c语言教程 it教程网 编辑:程序博客网 时间:2024/06/08 18:07

Blue Jeans
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 15524 Accepted: 6895

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

32GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA3GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAGATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAAGATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA3CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample Output

no significant commonalitiesAGATACCATCATCAT



题目大意:给出若干条碱基序列,求他们的最长公共序列


解题思路:将第一条主串分割,一一枚举。每一分割序列用KMP于其他序列一一匹配,该序列的成功匹配值为与其他序列匹配值中最小的一个。最终结果取这些分割序列成功匹配值得最大值。


代码如下:


#include"iostream"#include"cstdio"#include"cstring"#define INF 11100000using namespace std;const int maxn = 65;const int maxm = 15;char str[maxm][maxn];//储存DNA序列 char sub[maxn];//将第一个字符串拆开,枚举 char subch[maxn],sub_ch[maxn];int Next[maxn]; int maxsublen;//记录最大公共串的起始和长度 int flag,sublen; int max(int x,int y){return x > y ? x : y;}int min(int x,int y){return x < y ? x : y;}void MakeNext(int len){Next[0] = -1;int i = 0,j = -1;while(i < len){if(j == -1 || sub[i] == sub[j]){++i , ++j;if(sub[i] != sub[j])Next[i] = j;elseNext[i] = Next[j];}else j = Next[j];}}int  KMP(int k){int i = 0,j = 0;int length = 0;int klen = strlen(str[k]);int slen = strlen(sub);while(i < klen && j < slen){if(j == -1 || sub[j] == str[k][i]){i ++ , j ++;length = max(length,j);}else j = Next[j];}return length;}int main(){int T,m,len;scanf("%d",&T);while(T --){scanf("%d",&m);for(int i = 0;i < m;i ++)scanf("%s",str[i]);len = strlen(str[0]);maxsublen = 0;for(int i = 3;i <= len;i ++)for(int j = 0;i + j <= len;j ++){memset(sub,'\0',sizeof(sub));strncpy(sub,str[0] + j,i);MakeNext(i);sublen = INF;flag = 0;for(int k = 1;k < m;k ++){int t = KMP(k);if(t >= 3) sublen = min(sublen,t);else {flag = 1;break;}}if(flag) continue;else {memset(sub_ch,'\0',sizeof(sub_ch));strncpy(sub_ch,sub,sublen);if(sublen > maxsublen){maxsublen = sublen;memset(subch,'\0',sizeof(sub_ch));strcpy(subch,sub_ch);}if(sublen == maxsublen && strcmp(sub_ch,subch) == -1)//长度相等,按字典序 {memset(subch,'\0',sizeof(sub_ch));strcpy(subch,sub_ch);}}}//输出操作 if(maxsublen >= 3) printf("%s\n",subch);else printf("no significant commonalities\n");}return 0;}


0 0
原创粉丝点击