(kmp)poj 3080 ——Blue Jeans

旧时模样 提交于 2020-04-05 23:04:35
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

事实上是非常非常水的题目,可是我做的实在是太蠢了……

不过从中还是get到了一些string的特性。

想要读入数据使string下标也从1开始,可以先建立char数组读入 再string s=(string) a 但char下标0的必须得先赋上值,不然会出问题。

这样做之后string的特性都还保留,但如果想当然一个个读的话就会出问题。

感觉应该还有别的更好的办法……

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 using namespace std;
 6 int n,m,re;
 7 string x[34];
 8 int stlen[63];
 9 int f[68][68];
10 string tem;
11 string proan[68];
12 void getf(string t,int k)
13 {
14     f[k][0]=f[k][1]=0;
15     for(int i=2,j=0;i<=60-k+1;i++)
16     {
17         while(j&&t[j+1]!=t[i])
18             j=f[k][j];
19         if(t[j+1]==t[i])
20             j++;
21         f[k][i]=j;
22     }
23 }
24 int kmp(string t,string x,int k)//t是kmp的目标串(与之匹配),x是总串
25 {
26     int re=0;
27     for(int i=1,j=0;i<=60;i++)
28     {
29         while(j&&t[j+1]!=x[i])
30             j=f[k][j];
31         if(t[j+1]==x[i])
32             j++;
33         if(j>=60-k+1)
34             return 60-k+1;
35         if(j>re)
36             re=j;
37     }
38 
39     return re;
40 }
41 char bs[100];
42 int main()
43 {
44     cin>>n;
45     while(n--)
46     {
47         memset(stlen,-1,sizeof(stlen));
48         cin>>m;
49         int i;
50         bs[0]='A';
51         for(i=1;i<=m;i++)
52         {
53             scanf("%s",bs+1);
54             x[i]=string(bs);
55         }
56         for(i=1;i<=58;i++)
57         {
58             getf(x[1].substr(i-1,63-i),i);
59         }
60         for(i=2;i<=m;i++)
61         {
62             for(int j=1;j<=58;j++)
63             {
64                 if(i==2)
65                 {
66                     stlen[j]=kmp(x[1].substr(j-1,63-j),x[i],j);
67                 }
68                 else
69                     stlen[j]=min(kmp(x[1].substr(j-1,63-j),x[i],j),stlen[j]);
70             }
71         }
72         int da=0;
73         for(i=1;i<=58;i++)
74         {
75             da=max(da,stlen[i]);
76         }
77         if(da<3)
78             cout<<"no significant commonalities\n";
79         else
80         {
81             int ge=0;
82             for(i=1;i<=58;i++)
83             {
84                 if(stlen[i]==da)
85                 {
86                     proan[ge++]=x[1].substr(i,da);
87                 }
88             }
89             sort(proan,proan+ge);
90             cout<<proan[0]<<"\n";
91         }
92     }
93 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!