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 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题意:有m个串(m<=10)求所有串的最长连续公共子串,如有多个解输出字典序最小的题解:按长度倒着暴枚出第一串的所有子串,用kmp查找这个子串是不是在所有其他串中即可但是说好的只有"ACGT"四个字母呢?!说好的只有长度为六十的串呢?!人与人之间基本的信任呢?!

代码如下:
#include<queue>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define hi puts("hi!");
using namespace std;

int m,n,cnt;
string ans,s[15],s1[60];

struct kmp
{
    string ss,sss;
    int ans,nxt[70];

    void getfill()
    {
        memset(nxt,0,sizeof(nxt));
        for(int j=1;j<sss.size();j++)
        {
            int k=nxt[j];
            while(k&&sss[j]!=sss[k])
            {
                k=nxt[k];
            }
            nxt[j+1]=(sss[j]==sss[k])?k+1:0;
        }
    }

    void find()
    {
        ans=0;
        getfill();
        int k=0;
        for(int j=0;j<ss.size();j++)
        {
            while(k&&ss[j]!=sss[k])
            {
                k=nxt[k];
            }
            if(ss[j]==sss[k])
            {
                k++;
            }
            if(k==sss.size())
            {
                ans=1;
            }
        }
    }

}k;

int main()
{
//    freopen("1.out","w",stdout);
    scanf("%d",&m);
    while(m--)
    {
        int flag=1,flag1=0;
        cnt=0;
        scanf("%d\n",&n);
        for(int i=1;i<=n;i++)
        {
            cin>>s[i];
        }
        int len=s[1].size();
        for(int i=len;i>=1;i--)
        {
            if(i<3)
            {
                puts("no significant commonalities");
                break;
            }
            for(int l=0;l<=len-i;l++)
            {
                string sub=s[1].substr(l,i);
                flag=1;
                for(int w=2;w<=n;w++)
                {
                    k.ss=s[w];
                    k.sss=sub;
                    k.find();
                    flag=min(flag,k.ans);
                }
                if(flag)
                {
                    s1[++cnt]=sub;
                    flag1=1;
                }
            }
            if(flag1)
            {
                string ans1=s1[1];
                for(int ha=2;ha<=cnt;ha++)
                {
                    if(ans1.compare(s1[ha])==1)
                    {
                        ans1=s1[ha];
                    }
                }
                cout<<ans1<<endl;
                break;
            }
        }
    }
}



原文地址:https://www.cnblogs.com/stxy-ferryman/p/8461692.html

时间: 2024-11-08 17:57:24

poj3080 Blue Jeans(暴枚+kmp)的相关文章

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

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

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

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

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+暴力)

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 最长公共子串)

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 三种暴力法

本题可以使用暴力法直接求解,思路也挺简单的,不过实现起来也挺麻烦的. 本题最暴力直接使用strstr过. 这里使用hash表的方法过,这种方法好像有个学名的,主要思路就是把一个需要查找的字符串赋予一个数值,那么就可以把一串字符串的比较转换为一个值的比较了,那么就可以加速字符串的查找了. #include <stdio.h> #include <string.h> #include <stdlib.h> const long long MOD = (int)(1E9+7)