码迷,mamicode.com
首页 > 其他好文 > 详细

poj(3080)

时间:2018-09-23 18:20:39      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:aaa   writing   memory   eterm   logs   sts   single   RKE   sign   

题目链接:http://poj.org/problem?id=3080

学习博客:https://www.cnblogs.com/acjiumeng/p/6818213.html

Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21342   Accepted: 9467

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

 
题目大意:输入t,t组样例,输入n,有n个字符串,求他们最长的公共子串是什么,如果长度小于3,输出"no significant commonalities",否则输出那个最长的公共子串(如果有相同长度的输出字典序小的)
思路:这一题一直觉得用kmp会超时,后来看了一下别人的代码,并不会超时,这里介绍一下这一道题的时间复杂度:

首先你必须遍历第一个串每一个子串长度的(60*60),然后求每一个子串的next[]数组,最大为60,最后和剩下的串进行比较,最大10*60,所以这道题最大的时间复杂度是(60*60*60*10*60),差不多刚好卡在时限左右

看代码:

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1000;
const int maxn=60+10;
const int maxk=5e3+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
string a[15];
int next[maxn];
void cal_next(string s)
{
    int len=s.length();
    int k=-1;
    next[0]=-1;
    for(int i=1;i<len;i++)
    {
        while(k>-1&&s[k+1]!=s[i])
        {
            k=next[k];
        }
        if(s[k+1]==s[i]) k++;
        next[i]=k;
    }
}
bool kmp(string x,string y)
{
    int k=-1;
    for(int i=0;i<x.length();i++)
    {
        while(k>-1&&y[k+1]!=x[i])
        {
            k=next[k];
        }
        if(x[i]==y[k+1]) k++;
        if(k==y.length()-1) return true;
    }
    return false;
}
int main()
{
    int t,n,flag=0;
    scanf("%d",&t);
    while(t--)
    {
        string ans="";
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(int i=1;i<=a[0].size();i++)//遍历每一个长度
        {
            for(int j=0;j+i<=a[0].size();j++)//取每一个子串
            {
                flag=0;
                string op=a[0].substr(j,i);
                cal_next(op);//求每个子串的next[]
                for(int k=1;k<n;k++)
                {
                    if(!kmp(a[k],op))//和每一个串进行比较,看是否有不符合的,有点话直接break
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==0)
                {
                    if(op.size()>ans.size()) ans=op;
                    else if(op.size()==ans.size()) ans=min(op,ans);
                }
            }
        }
        if(ans.size()<3) cout<<"no significant commonalities"<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}

 

poj(3080)

标签:aaa   writing   memory   eterm   logs   sts   single   RKE   sign   

原文地址:https://www.cnblogs.com/caijiaming/p/9692991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!