poj3080(kmp)

欢迎参加hihoCoder挑战赛14和15,赢取100件Tshirt!


Language:
Default

Blue Jeans

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14450   Accepted: 6437

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

/*
   相当于用kmp的暴力QAQ
   n^2枚举第一个串的子串,然后看这个子串能不能匹配所有的串
   注意:输出保证是字典序最小

*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <cmath>
#include <vector>
#include<cstdlib>
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
#define maxn 100
int aa,bb,cc;
char str1[maxn];
char str2[maxn];
int next_[maxn];
int len1,len2;

struct str
{
    char s[maxn];
    int len;
}node[20];
int n;
int mini,minn;

void get_next()
{
    int i=0;
    int j=-1;
    while(i<len2)
    {
        if(str2[i]==str2[j] || j==-1)
        {
            ++i;
            ++j;
            next_[i]=j;
        }
        else
             j=next_[j];
    }
}
int kmp(int x)
{

    for(int i=0;i<=node[x].len;i++)
        str1[i]=node[x].s[i];
    len1=node[x].len;
    int i=0;
    int j=0;
    while(i<len1)
    {
        if(j==-1 || str1[i]==str2[j])
        {
            ++i;
            ++j;
        }
        else
            j=next_[j];
        if(j==len2)
        {
            return 1;
        }
    }
    return 0;
}
int check(int a1,int b1,int a2,int b2)
{
    for(int i=0;i<b1-a1;i++)
    {
        if(node[1].s[a1+i]<node[1].s[a2+i])
            return 1;
        else if(node[1].s[a1+i]>node[1].s[a2+i])
            return 0;
    }
    return 0;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n;
        mini=1;
        minn=1000000;
        for(int i=1;i<=n;i++)
        {
             scanf("%s",node[i].s);
             node[i].len=60;
        }
        aa=bb=cc=0;
        for(int i=0;i<60;i++)
        {
            for(int j=59;j>=i;j--)
            {
                int k;
                for(k=i;k<=j;k++)
                 str2[k-i]=node[1].s[k];
                str2[k]='\0';
                memset(next_,-1,sizeof(next_));
                len2=j-i+1;
                get_next();

                int ans=1;
                for(int i=1;i<=n;i++)
                {
                    if(!kmp(i))
                       ans=0;
                    if(!ans)
                        break;
                }

                if(ans)
                {
                    if(j-i+1>cc)
                    {
                        aa=i;
                        bb=j;
                        cc=j-i+1;
                    }
                    else if(j-i+1==cc)
                    {
                        if(check(i,j,aa,bb))
                        {
                             aa=i;
                             bb=j;
                        }
                    }
                }
            }
        }
        if(cc>=3)
        {
            for(int i=aa;i<=bb;i++)
            cout<<node[1].s[i];
            cout<<endl;
        }
        else
            cout<<"no significant commonalities"<<endl;
    }
    return 0;
}

版权声明:本文为博主原创文章,欢迎指教~

时间: 2024-07-30 13:49:40

poj3080(kmp)的相关文章

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

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

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

poj3080(kmp+枚举)

Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20163   Accepted: 8948 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】【暴力】

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

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 暴力水过】

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

字符串处理------Brute Force与KMP

一,字符串的简单介绍 例:POJ1488  http://poj.org/problem?id=1488 题意:替换文本中的双引号: #include <iostream> #include <cstring> #include <cstdio> using namespace std; int main() { char c,flag=1; //freopen("Atext.in","r",stdin); while((c=ge

POJ3080题解——暴力orKMP

题目链接:http://poj.org/problem?id=3080 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 resea

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp