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 ;
}
原文地址:https://www.cnblogs.com/nonames/p/11297267.html