首页 > 代码库 > poj 3080 Blue Jeans(kmp)
poj 3080 Blue Jeans(kmp)
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.
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
题意:先输入的测试数据的组数,接下来是每组数据的字符串数,每组字符串有60个字符,求最长公共子串,如果最长的公共子串长度小于3,就输出
no significant commonalities,否则输出字典序最小的最长公共子串。
思路:枚举第一个串的子串为模板与其他的字串比较。
ac代码:
1 #include<stdio.h> 2 #include<string.h> 3 char s1[200],s2[15][200]; 4 int next[200],cnt,n1,n2,n,ma; 5 void nextshuzu() //next数组 6 { 7 int i=0,j=-1; 8 next[0]=-1; 9 while(i<n1)10 {11 if(j==-1||s1[i]==s1[j])12 next[++i]=++j;13 else14 j=next[j];15 }16 }17 void kmp()18 {19 nextshuzu();20 int i=0,j=0;21 ma=100;22 for(int k=1; k<n; k++)23 {24 int m=0;25 while(j<n2&&i<60)26 {27 if(j==-1||s1[j]==s2[k][i])28 {29 i++;30 j++;31 }32 else33 j=next[j];34 if(j>m) //求出这个子串的最大值35 m=j;36 }37 if(m<ma) //取出最小的那个,最小的都满足38 ma=m;39 }40 }41 int main()42 {43 int b;44 char s[200];45 scanf("%d",&b);46 while(b--)47 {48 scanf("%d",&n);49 for(int i=0;i<n;i++)50 scanf("%s",s2[i]);51 int ans=0;52 for(int i=0;i<58;i++)53 {54 strcpy(s1,s2[0]+i);55 n2=60-i;56 kmp();57 if(ans<ma)58 {59 ans=ma;60 strncpy(s,s2[0]+i,ans);61 s[ans]=‘\0‘;62 }63 else if(ma==ans) //开始第一次没写这里,wr后看别人的博客加上的,首先谢谢那个大神,在这我就先用了啊64 { //如果长度相同,用字典序最小的那个65 char s3[200];66 strncpy(s3,s2[0]+i,ans);67 s3[ans]=‘\0‘;68 if(strcmp(s,s3)>0)69 strcpy(s,s3);70 }71 }72 if(ans>2)73 printf("%s\n",s);74 else75 printf("no significant commonalities\n");76 }77 }
poj 3080 Blue Jeans(kmp)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。