poj3080 Blue Jeans
地址:http://poj.org/problem?id=3080
题目:
Blue Jeans
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17720 | Accepted: 7856 |
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
3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities
AGATAC
CATCATCAT
Source
思路:暴力枚举+kmp
1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4
5 using namespace std;
6
7 #define MP make_pair
8 #define PB push_back
9 typedef long long LL;
10 const double eps=1e-8;
11 const int K=1e6+7;
12 const int mod=1e9+7;
13
14 int nt[K],ans[K];
15 char sa[20][100],sb[100];
16 void kmp_next(char *T,int *next)
17 {
18 next[0]=0;
19 for(int i=1,j=0,len=strlen(T);i<len;i++)
20 {
21 while(j&&T[i]!=T[j]) j=next[j-1];
22 if(T[i]==T[j]) j++;
23 next[i]=j;
24 }
25 }
26 int kmp(char *S,char *T,int *next)
27 {
28 int ans=0;
29 int ls=strlen(S),lt=strlen(T);
30 for(int i=0,j=0;i<ls;i++)
31 {
32 while(j&&S[i]!=T[j]) j=next[j-1];
33 if(S[i]==T[j]) j++;
34 if(j==lt) ans++;
35 }
36 return ans;
37 }
38 int cmp(char *sb,int si,int st,int len)
39 {
40 for(int i=0;i<len;i++)
41 if(sb[si+i]<sb[st+i])
42 return -1;
43 else if(sb[si+i]>sb[st+i])
44 return 1;
45 return 0;
46 }
47 int main(void)
48 {
49 int t,n;cin>>t;
50 while(t--)
51 {
52 cin>>n;
53 for(int i=1;i<=n;i++)
54 scanf("%s",sa[i]);
55 int len,st,se;
56 len=strlen(sa[1]);
57 st=se=0;
58 for(int i=0;i<len;i++)
59 {
60 sb[0]=sa[1][i],sb[1]=sa[1][i+1];
61 for(int j=i+2;j<len;j++)
62 {
63 sb[j-i]=sa[1][j],sb[j-i+1]='\0';
64 int ff=1;
65 kmp_next(sb,nt);
66 for(int k=2;k<=n&&ff;k++)
67 if(!kmp(sa[k],sb,nt))
68 ff=0;
69 if(ff&&j-i>se-st)
70 st=i,se=j;
71 else if(ff&&j-i==se-st&&cmp(sa[1],i,st,j-i+1)<0)
72 st=i,se=j;
73 }
74 }
75 if(se-st+1<3)
76 printf("no significant commonalities\n");
77 else
78 {
79 for(int i=st;i<=se;i++)
80 printf("%c",sa[1][i]);
81 printf("\n");
82 }
83
84 }
85 return 0;
86 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。