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 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

Source

South Central USA 2006

题意:

给定$m$个场长度为$60$的字符串,问他们的最长公共子串。

思路:

因为$m$和长度都很小,所以可以暴力枚举一个串的所有子串,$KMP$进行匹配。

等之后【后缀数组】刷熟练一点了再用后缀数组做一次。奇怪的是hdu2328明明是一样的题意,怎么就总是WA

  1 #include<iostream>
  2 //#include<bits/stdc++.h>
  3 #include<cstdio>
  4 #include<cmath>
  5 #include<cstdlib>
  6 #include<cstring>
  7 #include<algorithm>
  8 #include<queue>
  9 #include<vector>
 10 #include<set>
 11 #include<climits>
 12 #include<map>
 13 using namespace std;
 14 typedef long long LL;
 15 typedef unsigned long long ull;
 16 #define pi 3.1415926535
 17 #define inf 0x3f3f3f3f
 18
 19 const int maxn = 65;
 20 int n, m;
 21 char str[15][maxn];
 22 int nxt[maxn];
 23
 24 void getnxt(char *s)
 25 {
 26     int len = strlen(s);
 27     nxt[0] = -1;
 28     int k = -1;
 29     int j = 0;
 30     while(j < len){
 31         if(k == -1 || s[j] == s[k]){
 32             ++k;++j;
 33             if(s[j] != s[k]){
 34                 nxt[j] = k;
 35             }
 36             else{
 37                 nxt[j] = nxt[k];
 38             }
 39         }
 40         else{
 41             k = nxt[k];
 42         }
 43     }
 44 }
 45
 46 bool kmp(char *s, char *t)
 47 {
 48     getnxt(s);
 49     int slen = strlen(s), tlen = strlen(t);
 50     int i = 0, j = 0;
 51     while(i < slen && j < tlen){
 52         if(j == -1 || s[i] == t[j]){
 53             j++;
 54             i++;
 55         }
 56         else{
 57             j = nxt[j];
 58         }
 59     }
 60     if(j == tlen){
 61         return true;
 62     }
 63     else{
 64         return false;
 65     }
 66 }
 67
 68 int main()
 69 {
 70     scanf("%d", &n);
 71     while(n--){
 72         scanf("%d", &m);
 73         for(int i = 0; i < m; i++){
 74             scanf("%s", str[i]);
 75         }
 76
 77         char ans[maxn];
 78         int anslen = -1;
 79         for(int i = 0; i < 60; i++){
 80             for(int j = i; j < 60; j++){
 81                 char t[maxn];
 82                 memcpy(t, str[0] + i, j - i + 1);
 83                 t[j - i + 1] = ‘\0‘;
 84                 bool flag = true;
 85                 for(int k = 1; k < m; k++){
 86                     if(!kmp(str[k], t)){
 87                         flag = false;
 88                         break;
 89                     }
 90                 }
 91                 if(flag){
 92                     if(j - i + 1 > anslen){
 93                         anslen = j - i + 1;
 94                         strcpy(ans, t);
 95                     }
 96                     else if(j - i + 1 == anslen){
 97                         if(strcmp(ans, t) > 0){
 98                             strcpy(ans, t);
 99                         }
100                     }
101                 }
102                 else{
103                     break;
104                 }
105             }
106         }
107
108         if(anslen < 3){
109             printf("no significant commonalities\n");
110         }
111         else{
112             //cout<<ans<<endl;
113             printf("%s\n", ans);
114         }
115
116     }
117     return 0;
118 }

原文地址:https://www.cnblogs.com/wyboooo/p/10034142.html

时间: 2024-10-08 11:31:59

poj3080 Blue Jeans【KMP】【暴力】的相关文章

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]==

POJ3080——Blue Jeans(暴力+字符串匹配)

Blue Jeans DescriptionThe 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 hav

POJ 3080 Blue Jeans KMP解法

使用KMP寻找最长的前缀的方法,比一般的暴力法有快了很多. 本题一般的暴力法需要的是O(m*n*n*n),其中m是有多少字符串,而n是字符串长度,而使用KMP就可以把时间效率提高到O(m*n*n),减少了一个n,提高了一个档次啦. 速度快很多. 准确来说应该是利用KMP寻找一个字符串A,在另一个字符串B任意位置出现的A的最长的前缀字符串. 理解好KMP的next table就好办了.每次查找到相等字符的时候,保存好最长的前缀. 注意本题的条件:选取最前的字典顺序输出.老害我错的条件. #incl

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

POJ3080 Blue Jeans

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

POJ3080 Blue Jeans 【KMP 暴力水过】

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

kuangbin专题十六 KMP&amp;&amp;扩展KMP POJ3080 Blue Jeans

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 wri

poj3080 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 ta

Blue Jeans---poj3080(kmp+暴力求子串)

题目链接:http://poj.org/problem?id=3080 题意就是求n个长度为60的串中求最长公共子序列(长度>=3):如果有多个输出字典序最小的: 我们可以暴力求出第一个串的所有子串,然后判断是否是其他的子串即可: #include<iostream> #include<stdio.h> #include<string.h> using namespace std; const int N = 107; char s[N][61]; int Nex