• POJ3080题解——暴力orKMP


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

    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 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

    题目思路:首先一点:我们都是利用第一个字符串作为比较(截取其中一部分作为模式串)
    两个概念:
    文本串:就是待查找(询)的字符串(比如abcde)
    模式串:就是要在文本串中查找的字符串(比如bcd,和上面对应的模式串)
    首先思路一是大家都想得到的暴力枚举法(但是因为本人是个蒟蒻,有些函数还不会用,不知道怎么实现555~),那就是从第一个字符串中列举所有按长度值递增的字符串(长度值一样按照串的起始位置从前往后取,注意选取的字符串最后一位不要越界)与除此之外的后面所有字符串比较,如果选取的字符串(长度大于等于3)在后面的待比较的字符串中均有出现。则输出该字符串;否则输出
    no significant commonalities
    题目思路二是列举截取第一个字符串的起始地址而不需要列举它的长度,而只需要用 截取的字符串 和每个字符串(第2个到第n个)kmp匹配到指针在模式串中所能达到的最大距离当中取最小的,就是我们要的答案了。
    然后代码如下:
    //POJ3080
    //暴力枚举法 
    #include<iostream>
    #include<string>
    using namespace std;
    int t,n;
    string s[11];
    
    int main()
    {
        cin>>t;
        while(t--)
        {
            cin>>n;
            string snk="no significant commonalities";
            for(int i=1;i<=n;i++)cin>>s[i];
            for(int i=3;i<=60;i++)//列举长度3-60
            {
                for(int j=0;j<=60-i;j++)//列举长度为i的字符串从哪里开始
                {
                    string mo=s[1].substr(j,i);//s[1]中从j开始长度为i的字符串付给mo
                    bool flag=true;
                    for(int k=2;k<=n;k++)//从s[2]开始对比 
                    {
                        if(s[k].find(mo)==string::npos)//没有查找到
                        {
                            flag=false;
                            break;
                        } 
                    } 
                    if(!flag)continue;//没有就重来
                    else//如果找到了 
                    {
                        if(snk=="no significant commonalities")snk=mo;
                        else if(snk.size()<mo.size())snk=mo;
                        else if(snk.size()==mo.size())snk=min(snk,mo);
                    } 
                }
            } 
            cout<<snk<<endl;
        }
        return 0;
    }
    暴力枚举法
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    char s[12][1010],p[1010],ans[1010],temp[1010];//ans为最终字符串 
    int t,n,ansl,next[1010];//ansl为最终字符串长度 
    int main()
    {
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);ansl=0;
            for(int i=0;i<n;i++)scanf("%s",s[i]);//先输入所有字符串
            for(int i=0;i<=57;i++)//此循环列举要截取第一个字符串的位置 即从s[0][i]开始 
            {
                strcpy(p,s[0]+i);//先将起始地址之后的字符串放进p(模式串)
                int l=60-i;//对应p的长度
                
                //下列为求next数组 
                int slen=-1;
                int plen=0;
                next[0]=-1;
                while(plen<l)
                {
                    if(slen==-1||p[plen]==p[slen])
                    {
                        ++plen;++slen;
                        if(p[plen]!=p[slen])
                        next[plen]=slen;
                        else
                        next[plen]=next[slen];//之前写kmp一直漏了这句然后wa了一天555~ 
                    }
                    else
                    slen=next[slen];
                } 
                
                //下列为kmpsearch
                int snk,max=100;
                for(int k=1;k<n;k++)//列举后面待比较的字符串  列举第k个字符串
                {
                    slen=plen=0;snk=0;
                    while(plen<l&&slen<60)//kmp
                    {
                        if(plen==-1||s[k][slen]==p[plen])
                        {
                            slen++;
                            plen++;
                        }
                        else plen=next[plen];
                        if(plen>snk)snk=plen;//p对同一个s[k][]内找最长的 
                    }
                    if(max>snk)max=snk;//不同的s[k][]内找最短的
                } 
                
                
                if(ansl<max)//长度必须比上一个长
                {
                    ansl=max;strncpy(ans,s[0]+i,max);ans[ansl]='';
                }
                else if(ansl==max)//长度相同选字典序小的 
                {
                    strncpy(temp,s[0]+i,ansl);temp[ansl]='';
                    if(strcmp(temp,ans)<0)
                    {
                        strcpy(ans,temp);ans[ansl]='';
                    }
                }
            }
            if(ansl>=3)printf("%s
    ",ans);
            else puts("no significant commonalities");
        }
        return 0;
    }
    KMP
  • 相关阅读:
    c++ tinyxml
    fmod简单播放mp3
    D3DXVec3TransformCoord 函数 D3DXVec3TransformNormal 函数
    D3DXMATRIX
    OpenGL FBO
    Opengl Freeimage 保存屏幕像素到图片
    fmod播放mp3 vs2010测试通过
    Hermite与Bezier曲线绘制方法研究
    D3DXVECTOR3
    c++ windows下创建文件夹和删除文件夹
  • 原文地址:https://www.cnblogs.com/Mingusu/p/11799788.html
Copyright © 2020-2023  润新知