zoukankan      html  css  js  c++  java
  • POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)

    Blue Jeans
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 20966   Accepted: 9279

    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

    题目意思:
    给你n个字符串,要你找这n个字串的最长公共字串
    如果最长公共字串的长度相同,输出字典序最小的那个
    如果最长公共字串的长度小于3输出指定语句
     
    分析:
    直接在第一个串枚举模板串(短的)的长度和起点
    然后在剩余的n-1个串里面匹配
    匹配到了的话,保存下来,但值保存最长的那个,相同长度的保存字典序最小的那个
    注意:
    min函数比较字符串的话,可以按照字典序比较!!!!
     
    code:
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<memory>
    #include<algorithm>
    using namespace std;
    #define mod 360000
    string a,b[15];
    int next1[65];
    int sum;
    void getnext(string s,int next1[],int m)
    {
        next1[0]=0;
        next1[1]=0;
        for(int i=1; i<m; i++)
        {
            int j=next1[i];
            while(j&&s[i]!=s[j])
                j=next1[j];
            if(s[i]==s[j])
                next1[i+1]=j+1;
            else
                next1[i+1]=0;
        }
    }
    int kmp(string ss,string s,int next1[],int n,int m)
    {
        getnext(s,next1,m);
        int j=0;
        for(int i=0; i<n; i++)
        {
            while(j&&s[j]!=ss[i])
                j=next1[j];
            if(s[j]==ss[i])
                j++;
            if(j==m)
            {
                return 1;
            }
        }
        return 0;
    }
    int main()
    {
        int t;
        string ans;
        scanf("%d",&t);
        while(t--)
        {
            ans="";
            int n;
            scanf("%d",&n);
            for(int i=0; i<n; i++)
                cin>>b[i];
            int l=b[0].size();
            for(int i=1; i<=l; i++)//字串长度
            {
                for(int j=0; j<=l-i; j++)//字串起点
                {
                    a=b[0].substr(j,i);//substr 起点是j,长度为i
                    bool flag=1;
                    for(int k=1; k<n; k++)
                    {
                        if(!kmp(b[k],a,next1,b[k].size(),a.size()))
                            flag=0;
                    }
                    if(flag)
                    {
                        if(ans.size()<a.size())
                            ans=a;
                        else if(ans.size()==a.size())
                            ans=min(ans,a);
                    }
                }
            }
            if(ans.size()<3)
                printf("no significant commonalities
    ");
            else
                cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    poj_1236 强连通分支
    【winform程序】自定义webrowser控件调用IE的版本
    【小程序开发】微信小程序开发中遇到的那些坑...
    【C#多线程】C#多线程 Thread 开发基础
    【管理心得】不懂带人,你就自己干到死
    【80端口占用】win7下80端口被(Pid=4)占用的解决方法
    【顽固BUG】Visual Studio 2015 + TestDriven.NET-3.8.2860_Personal_Beta 调用的目标发生了异常。
    【HPP开发】让所有中小企业拥有自己的APP
    【创业积累】如何快速开发出一个高质量的APP
    【架构师之路】依赖注入原理---IoC框架
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9457524.html
Copyright © 2011-2022 走看看