poj-3080

来源:互联网 发布:施工图设计优化 编辑:程序博客网 时间:2024/06/04 19:36
Blue Jeans
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 15694 Accepted: 6978

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 commonalitiesAGATAC

CATCATCAT

解体思路:

KMP+暴力,就这么简单.

代码如下:

#include<stdio.h>#include<string.h>int n,len2;char test[12][65],pat[65];int per[65];void get_per(){int i,j;memset(per,0,sizeof(per));per[0]=-1;i=0;j=-1;while(i<len2){if(j==-1||pat[i]==pat[j]){i++;j++;per[i]=j;}else j=per[j];}}int KMP(){int i,j,k,m;int ma=100;get_per();for(k=1;k<n;k++){i=0;j=0;m=0;while(i<60&&j<len2){if(j==-1||test[k][i]==pat[j]){i++;j++;}else j=per[j];if(j>m)m=j;//从首元素开始,最长匹配长度 }if(ma>m) ma=m;//与所有字符串匹配的最小长度 }return ma;}int main(){int t,i,ans;char re[65];scanf("%d",&t);while(t--){ans=0;scanf("%d",&n);    for(i=0;i<n;i++){    scanf("%s",test[i]);    }for(i=0;i<=57;i++){//字符串至少长度为3 strcpy(pat,test[0]+i);//遍历test的后缀数组 len2=60-i;int l=KMP();if(l>ans){ans=l;strncpy(re,pat,l);re[l]='\0';}else if(l==ans){char re1[65];;strncpy(re1,pat,l);re1[l]='\0';if(strcmp(re,re1)>0)strcpy(re,re1);}}if(ans>=3) printf("%s\n",re);else printf("no significant commonalities\n");}return 0;}



0 0
原创粉丝点击