zoukankan      html  css  js  c++  java
  • kmp(暴力匹配)

    http://poj.org/problem?id=3080

    Blue Jeans
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 23415   Accepted: 10349

    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

    题意:给你几段长为60的碱基序列,让你找出最长相同的碱基序列,如果有多个最长相同序列,则输出字典序最小的碱基序列
    思路:kmp暴力或strstr函数暴力
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include <stdio.h>
    #include <string.h>
    #define INF  10000000
    using namespace std;
    char a[30][100] , b[10009], str[10009];
    int next[100];
    int ans = 0 ;
    
    void getnext(char *b , int len , int *next)
    {
        next[0] = -1 ;
        int j = 0 , k = -1;
        while(j < len)
        {
            if(k == -1 || b[j] == b[k])
            {
                k++;
                j++;
                next[j] = k ;
            }
            else
            {
                k = next[k];
            }
        }
    }
    
    int main()
    {
        int n ;
        scanf("%d" , &n);
        while(n--)
        {
            int m ;
            memset(b , '' , sizeof(b));
            memset(str , '' , sizeof(str));
            scanf("%d" , &m);
            for(int i = 0 ; i < m ; i++)
            {
                scanf("%s" , a[i]);
            }
            int x = 3 , flag = 0 , ans = 0;
            while(x <= 60)
            {
                for(int i = 0 ; i <= 60 - x ; i ++)
                {
                    int jj = i ;
                    for(int j = 0 ; j < x ; j++)
                    {
                        b[j] = a[0][jj++];
                    }
                    getnext(b , x , next);
                    for(int j = 1 ; j < m ; j++)
                    {
                        int ii = 0 , k = 0 ;
                        while(ii < 60 && k < x)
                        {
                            if(k == -1 || a[j][ii] == b[k])
                            {
                                k++;
                                ii++;
                            }
                            else
                            {
                                k = next[k];
                            }
                        }
                        if(k == x)
                        {
                            flag = 1 ;
                        }
                        else
                        {
                            flag = 0 ;
                            break ;
                        }
                    }
                    if(flag == 1)
                    {
                        if(ans < x)
                        {
                            ans = x ;
                            strcpy(str , b);
                        }
                        else if(ans == x) //如果长度相等,输出字典序小的序列,我还以为是第一出现的序列,害我wa了这么久
                        {
                            if(strcmp(b , str) <0)
                            {
                                strcpy(str , b);
                            }
                        }
                    }
                    if(i == 60 - x)
                        x++ ;
                }
            }
            if(ans == 0)
                printf("no significant commonalities
    ");
            else
            {
                printf("%s
    " , str);
            }
    
        }
    
    
        return 0 ;
    }
  • 相关阅读:
    渗透测试中的文件传输通道1- cmd下下载文件
    内网渗透常用命令
    技术剖析中国菜刀原理
    win8 iis安装及网站发布
    C++与C的区别一
    C语言实现单链表,并完成链表常用API函数
    C语言实现数组及链表的快速排序
    使用C语言封装数组,动态实现增删改查
    C语言中静态断言的使用
    二分查找法C语言实现
  • 原文地址:https://www.cnblogs.com/nonames/p/11297267.html
Copyright © 2011-2022 走看看