POJ Blue Jeans [枚举+KMP]

传送门

F - Blue Jeans

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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

题意及题解转自:http://blog.csdn.net/qiqijianglu/article/details/7851454

题意:求n个字符串的最长公共串。

求n个字符长度最长公共子串。对于多模式匹配问题,一般是不可以用KMP解决得,因为忒暴力。

思路很简单:我们先按字符串的长度由短到长进行快排。枚举第一个字符串的不同长度子串,判断她是否为下面多有的公共子串?如果是的话,那么我们就表明找到,则比较其长度,如果比已经找到的串长,那么就替换结果串 否则按字典序比较。取字典序考前的,就可以。

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 #include<set>
 10 #include<stack>
 11 #include<string>
 12
 13 #define N 65
 14 #define M 105
 15 #define mod 10000007
 16 //#define p 10000007
 17 #define mod2 1000000000
 18 #define ll long long
 19 #define LL long long
 20 #define eps 1e-6
 21 #define inf 100000000
 22 #define maxi(a,b) (a)>(b)? (a) : (b)
 23 #define mini(a,b) (a)<(b)? (a) : (b)
 24
 25 using namespace std;
 26
 27 int T;
 28 int n;
 29 char text[N][N];
 30 char result[N];
 31 int ma;
 32 int l;
 33 int le;
 34 char pat[N];
 35 int next[N];
 36 int mma;
 37
 38 void ini()
 39 {
 40     int i;
 41     ma=-1;
 42     scanf("%d",&n);
 43     for(i=1;i<=n;i++){
 44         scanf("%s",text[i]);
 45     }
 46     l=strlen(text[1]);
 47 }
 48
 49 void get_next()
 50 {
 51     memset(next,-1,sizeof(next));
 52     int i,j;
 53     j=-1;next[0]=-1;
 54     i=0;
 55     while(i<le)
 56     {
 57         if(j==-1 || pat[i]==pat[j]){
 58             i++;j++;next[i]=j;
 59         }
 60         else{
 61             j=next[j];
 62         }
 63     }
 64 }
 65
 66 void KMP()
 67 {
 68     int i,j,k,m;
 69     mma=200;
 70     for(k=2;k<=n;k++){
 71         i=0;j=0;m=0;
 72         while(i<l && j<le)
 73         {
 74             if(j==-1 || text[k][i]==pat[j])
 75             {
 76                 i++;j++;
 77                 m=max(m,j);
 78             }
 79             else{
 80                 j=next[j];
 81             }
 82         }
 83         mma=min(m,mma);
 84     }
 85 }
 86
 87 void solve()
 88 {
 89     int i;
 90     char te[N];
 91     for(i=0;i<l;i++){
 92         strcpy(pat,text[1]+i);
 93         le=strlen(pat);
 94         get_next();
 95         KMP();
 96         if(mma>ma){
 97             ma=mma;
 98             strncpy(result,text[1]+i,ma);
 99             result[ma]=‘\0‘;
100         }
101         else if(mma==ma){
102             strncpy(te,text[1]+i,ma);
103             result[ma]=‘\0‘;
104             if(strcmp(te,result)==-1){
105                 strcpy(result,te);
106             }
107         }
108     }
109 }
110
111 void out()
112 {
113     if(ma<3){
114         printf("no significant commonalities\n");
115     }
116     else{
117         printf("%s\n",result);
118     }
119 }
120
121 int main()
122 {
123     //freopen("data.in","r",stdin);
124     //freopen("data.out","w",stdout);
125     scanf("%d",&T);
126     //for(int ccnt=1;ccnt<=T;ccnt++)
127     while(T--)
128     //scanf("%d%d",&n,&m);
129     //while(scanf("%s",s)!=EOF)
130     {
131         ini();
132         solve();
133         out();
134     }
135     return 0;
136 }
时间: 2024-10-13 04:38:38

POJ Blue Jeans [枚举+KMP]的相关文章

poj3080(Blue Jeans)kmp求多个串公共子串

题意:给出1-10个长度为60的字符串,求出最长的公共子串(长度不能小于3),如果有多个一样长的,输出字典序最短的. 解法:想到kmp时,自己第一反应枚举第一个串的所有子串,在其他所有串中走一遍kmp,复杂度为10*60*60*60,但是发现只需枚举第一个串后缀就可以,每次枚举记录在所有串能走最远中走的最短的那个长度.这样复杂度就成了10*60*60,0ms AC. 代码: /**************************************************** * autho

POJ 3080--Blue Jeans【KMP &amp;&amp; 暴力枚举】

Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14316   Accepted: 6374 Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousa

POJ 题目3080 Blue Jeans(KMP+暴力)

Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14303   Accepted: 6367 Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousa

poj 3080 Blue Jeans (KMP)

http://poj.org/problem?id=3080 Blue Jeans 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 populate

poj3080 Blue Jeans【KMP】【暴力】

Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:21746   Accepted: 9653 Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousan

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 tas

POJ3080 Blue Jeans 【KMP 暴力水过】

题目描述 求n个字符串的最长公共序列,若长度相同,输出字典序最小的.若长度小于3,输出no significant commonalities Sample Input 3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 3 GATACCAGATACCAGATACCAGATACCAGATACCAGA

POJ 3080 Blue Jeans(KMP 最长公共子串)

Blue Jeans 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 ha

poj 3080 Blue Jeans (kmp暴力)

# include <stdio.h> # include <algorithm> # include <cstring> using namespace std; int next[100]; char pat[100]; char a[100][100]; int ma; int lenp; int n; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<=lenp) { if(j==-1||pat[j]==