zoukankan      html  css  js  c++  java
  • 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

    题意:

    给定$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] = '';
     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
    ");
    110         }
    111         else{
    112             //cout<<ans<<endl;
    113             printf("%s
    ", ans);
    114         }
    115 
    116     }
    117     return 0;
    118 }
  • 相关阅读:
    插入排序
    python -- 给电视剧重命名
    程序员你为什么迷茫?
    如何把自己打造成技术圈的 papi 酱
    GitHub中国区前100名到底是什么样的人?
    Python+opencv 图像拼接
    VS2015 新Web项目(C#6)出现CS1617错误的解决
    .Net Task<T>的一种比较神奇的卡死情况(Wait/Result卡死, await能得到结果)
    Xamarin Android自学和实践步骤
    跨过几个坑,终于完成了我的第一个Xamarin Android App!
  • 原文地址:https://www.cnblogs.com/wyboooo/p/10034142.html
Copyright © 2011-2022 走看看