• [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher I


    I - Blue Jeans POJ - 3080

    题目链接:https://vjudge.net/contest/70325#problem/I

    题目:

    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
    
    题意:T组数据,每组包含一个n,后有n个字符串,求出这n个字符串最长子序列,并且字典序最小
    思路:利用Kmp来判断是否为其他字符串的子序列,然后从第一个字符串开始找子串,判断子串是否为其他n-1的子串,是的话就是了,
    再初始化一个字符串,因为要字典序最小,所以每次取长度相等时的min就行了,详解见代码

    // 
    // Created by HJYL on 2019/8/16.
    //
    #include <iostream>
    #include <vector>
    #include <map>
    #include <string>
    #include <queue>
    #include <stack>
    #include <set>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstdlib>
    using namespace std;
    typedef long long ll;
    const int maxn=1e6+10;
    int nextt[maxn];
    string str[maxn];
    void getnext(string str)//KMP求next数组
    {
        int i=0,j=-1;
        nextt[0]=-1;
        int len=str.size();
        while(i<len)
        {
            if(j==-1||str[i]==str[j])
            {
                i++,j++;
                if(str[i]!=str[j])
                    nextt[i]=j;
                else
                    nextt[i]=nextt[j];
            } else
                j=nextt[j];
        }
    }
    bool kmp(string a,string b)//判断b是不是a的子串
    {
        int alen=a.size();
        int blen=b.size();
        int i=0,j=0;
        while(i<alen&&j<blen)
        {
            if(j==-1||a[i]==b[j])
            {
                i++,j++;
            } else
                j=nextt[j];
        }
        if(j==blen)//已经匹配到长度为b的字符串了
            return true;
        return false;
    }
    int main()
    {
        //freopen("C:\Users\asus567767\CLionProjects\untitled\text","r",stdin);
        int T;
        scanf("%d",&T);
        int n;
        while(T--)
        {
            scanf("%d",&n);
            for(int i=0;i<n;i++)
                cin>>str[i];
            string ans="";//结果字符串初始化,方便找字典序最小的
            for(int i=1;i<=str[0].size();i++)//以第一个字符串开始
            {
                for(int j=0;j<=str[0].size()-i;j++)//暴力找
                {
                    string op=str[0].substr(j,i);//还是初始化
                    getnext(op);
                    bool falge=false;
                    for(int k=1;k<n;k++)//看后面n-1个字符串是否有op这个字符串
                    {
                        if(!kmp(str[k],op))
                        {
                            falge=true;
                            break;
                        }
                    }
                    if(!falge)
                    {
                        if(ans.size()<op.size())//要求最长的
                            ans=op;
                        else if(ans.size()==op.size())//要求字典序最小的
                            ans=min(ans,op);
                    }
                }
            }
            if(ans.size()<3)
                printf("no significant commonalities
    ");
            else
                cout<<ans<<endl;
        }
        return 0;
    }
    
    
  • 相关阅读:
    iOS:Objective-C中Self和Super详解
    调试工具Instruments----Core Animation
    iOS开发之复制字符串到剪贴板
    Copy 和 mutableCopy
    TCP/IP,Http,Socket,XMPP的区别
    iOS程序中的内存分配 栈区堆区全局区(转)
    iOS常见算法(二分法 冒泡 选择 快排)
    老司机带你走进Core Animation
    C# 爬虫小程序
    C# 房贷计算器
  • 原文地址:https://www.cnblogs.com/Vampire6/p/11364145.html
Copyright © 2020-2023  润新知