zoukankan      html  css  js  c++  java
  • POJ3080 Blue Jeans —— 暴力枚举 + KMP / strstr()

    题目链接:https://vjudge.net/problem/POJ-3080

    Blue Jeans
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 19152   Accepted: 8524

    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

    题解:

    找出所有字符串的最长公共连续子串。

    直接枚举第一个字符串的每个子串,然后通过kmp算法或者strstr()函数,取判断该子串是否存在于每一个字符串。

    KMP:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <string>
     6 #include <vector>
     7 #include <map>
     8 #include <set>
     9 #include <queue>
    10 #include <sstream>
    11 #include <algorithm>
    12 using namespace std;
    13 typedef long long LL;
    14 const double eps = 1e-6;
    15 const int INF = 2e9;
    16 const LL LNF = 9e18;
    17 const int MOD = 1e9+7;
    18 const int MAXN = 100+10;
    19 
    20 char s[MAXN][MAXN], tmp[MAXN], ans[MAXN];
    21 int Next[MAXN], Len[MAXN];
    22 
    23 void get_next(char x[], int m)
    24 {
    25     int i, j;
    26     j = Next[0] = -1;
    27     i = 0;
    28     while(i<m)
    29     {
    30         while(j!=-1 && x[i]!=x[j]) j = Next[j];
    31         Next[++i] = ++j;
    32     }
    33 }
    34 
    35 bool kmp(char x[], int m, char y[], int n)
    36 {
    37     int i, j;
    38     get_next(x, m);
    39     i = j = 0;
    40     while(i<n)
    41     {
    42         while(j!=-1 && y[i]!=x[j]) j = Next[j];
    43         i++; j++;
    44         if(j>=m) return true;
    45     }
    46     return false;
    47 }
    48 
    49 int main()
    50 {
    51     int T, n;
    52     scanf("%d", &T);
    53     while(T--)
    54     {
    55         scanf("%d", &n);
    56         for(int i = 1; i<=n; i++)
    57             scanf("%s", s[i]), Len[i] = strlen(s[i]);
    58 
    59         bool hav_ans;
    60         for(int len = Len[1]; len>=2; len--)
    61         {
    62             hav_ans = false;
    63             for(int st = 0; st<=Len[1]-len; st++)
    64             {
    65                 int en = st+len-1, cnt = 0;
    66                 for(int i = st; i<=en; i++)
    67                     tmp[cnt++] = s[1][i];
    68                 tmp[cnt] = 0;
    69 
    70                 bool all = true;
    71                 for(int i = 2; i<=n; i++)
    72                     all = all&&kmp(tmp, cnt, s[i], Len[i]);
    73 
    74                 if(all)
    75                 {
    76                     if(!hav_ans || strcmp(tmp, ans)<0)
    77                     {
    78                         strcpy(ans, tmp);
    79                         hav_ans = true;
    80                     }
    81                 }
    82             }
    83             if(hav_ans) break;
    84         }
    85         if(hav_ans) puts(ans);
    86         else puts("no significant commonalities");
    87     }
    88 }
    View Code

    strstr():

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <string>
     6 #include <vector>
     7 #include <map>
     8 #include <set>
     9 #include <queue>
    10 #include <sstream>
    11 #include <algorithm>
    12 using namespace std;
    13 typedef long long LL;
    14 const double eps = 1e-6;
    15 const int INF = 2e9;
    16 const LL LNF = 9e18;
    17 const int MOD = 1e9+7;
    18 const int MAXN = 100+10;
    19 
    20 char s[MAXN][MAXN], tmp[MAXN], ans[MAXN];
    21 
    22 int main()
    23 {
    24     int T, n;
    25     scanf("%d", &T);
    26     while(T--)
    27     {
    28         scanf("%d", &n);
    29         for(int i = 1; i<=n; i++)
    30             scanf("%s", s[i]);
    31 
    32         int Len = strlen(s[1]);
    33         bool hav_ans;
    34         for(int len = Len; len>=2; len--)
    35         {
    36             hav_ans = false;
    37             for(int st = 0; st<=Len-len; st++)
    38             {
    39                 int en = st+len-1, cnt = 0;
    40                 for(int i = st; i<=en; i++)
    41                     tmp[cnt++] = s[1][i];
    42                 tmp[cnt] = 0;
    43 
    44                 bool all = true;
    45                 for(int i = 2; i<=n; i++)
    46                     all = all&&(strstr(s[i], tmp));
    47 
    48                 if(all)
    49                 {
    50                     if(!hav_ans || strcmp(tmp, ans)<0)
    51                     {
    52                         strcpy(ans, tmp);
    53                         hav_ans = true;
    54                     }
    55                 }
    56             }
    57             if(hav_ans) break;
    58         }
    59         if(hav_ans) puts(ans);
    60         else puts("no significant commonalities");
    61     }
    62 }
    View Code
  • 相关阅读:
    P3919 【模板】可持久化线段树 1(可持久化数组)
    P3384 【模板】轻重链剖分
    P2680_运输计划
    CSP-S2 T4/P7078 贪吃蛇_set 70pts/100pts(O2)
    SPFA判负环
    P6394 樱花,还有你
    CSP-S2T4/P7078 贪吃蛇
    【模板】单源最短路径(标准版)
    U135649 皇室战争
    【离散数学】实验三 偏序关系中盖住关系的求取及格论中有补格的判定
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7860665.html
Copyright © 2011-2022 走看看