zoukankan      html  css  js  c++  java
  • poj 3080 kmp+暴力

    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

    题目意思是给出n行字符串求其中公共最长的子串,如果有长度相同的取字典序小的。子串长度要大于等于3,
    没有则输出 no significant commonalities
    解题思路:先暴力求出第一行字符串长度大于等于3的子串,然后kmp查找是否后面的n-1行字符串都有
    代码如下:
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    char ans[100],str[15][100],son[100];//ans为答案子串,son为暂存子串,str为给出的n个字符串 
    int next[100];
    void getnext(char p[])//求next数组的 
    {
        int i=0,j=-1;
        int len=strlen(p);
        next[0]=-1;
        while(i<len)
        {
            if(p[i]==p[j]||j==-1)
                next[++i]=++j;
            else
                j=next[j];
        }
    } 
    
    bool kmp(char t[],char p[])//求子串p是否在主串t中出现 
    {
        int i=0,j=0;
        int la=strlen(t),lb=strlen(p);
        while(i<la)
        {
            if(j==-1||t[i]==p[j])
            {
                i++;
                j++;
            }
            else
                j=next[j];
            if(j>=lb)
            { 
                return true;
                //j=next[j]
            }
        }
        return false;
    }//标准的kmp模板 
    
    int main()
    {
        int k;
        cin>>k;
        while(k--)
        {
            int n;    
            cin>>n;
            memset(ans,'',sizeof(ans));
            for(int i=0;i<n;i++)
                cin>>str[i];
            for(int i=3;i<=60;i++)//暴力求出第一行每个大于3的子串 i为长度 
                for(int j=0;j+i<=60;j++)//j为子串起始位置 
                {
                    memset(son,'',sizeof(son));
                    strncpy(son,&str[0][j],i);
                    son[i]='';//字符串最后赋防止wa 
                    getnext(son);
                    int count=0;
                    for(int k=1;k<n;k++)
                    {
                        if(kmp(str[k],son))
                            count++;
                    }//求有后面的几行字符串是否有该子串 
                    if(count==n-1)//都有就要和当前ans比较哪个更符合条件 
                    {
                        int a=strlen(ans);
                        int b=strlen(son);
                        if(a==b&&strcmp(son,ans)<0||a<b)//1 长度相等但son字典序更小 2 son长度更大 
                            strcpy(ans,son);
                    }
                }
            if(strlen(ans)<3)
                cout<<"no significant commonalities"<<endl;
            else
                cout<<ans<<endl;
        }
        return 0;
    }
    
    
    
     
  • 相关阅读:
    IDEA设置显示行号和方法间的分隔符
    IDEA设置自动导包功能
    IDEA设置设置鼠标滚轮修改字体大小
    CA数字证书部署
    mysql数据库基础
    部署集群基础环境,MySQL-MMM架构部署,MySQL-MMM架构使用
    使用binlog日志, XtraBackup备份工具 ,MySQL AB复制
    实现MySQL读写分离,MySQL性能调优
    密码恢复及设置,用户授权及撤销,MySQL管理工具
    SQL数据导入/导出,操作表记录,查询及匹配条件
  • 原文地址:https://www.cnblogs.com/xiongtao/p/9107988.html
Copyright © 2011-2022 走看看