码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3080 Blue Jeans(kmp)

时间:2014-09-19 22:21:16      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   for   数据   

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
题意:先输入的测试数据的组数,接下来是每组数据的字符串数,每组字符串有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         else
14             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             else
33                 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         else
75         printf("no significant commonalities\n");
76     }
77 }

 


poj 3080 Blue Jeans(kmp)

标签:des   style   blog   color   io   os   ar   for   数据   

原文地址:http://www.cnblogs.com/Xacm/p/3982309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!