1. 程式人生 > >poj3080(kmp+枚舉)

poj3080(kmp+枚舉)

include while har 位置 project res http ext rap

Blue Jeans
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20163 Accepted: 8948

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

South Central USA 2006 題意:輸入t組數據,每組有n個60個字符大小的字符串,求他們的最長公共子序列,在長度相同的情況下,輸出字典序最小的那個,如果子序列的長度小於3,輸出
no significant commonalities
。 思路:隨便找一個字符串,從第1號位置一直枚舉到第57號位置,然後這每一種情況都與其他n-1個字符串匹配,看每一種情況與這n-1個字符串最長可以匹配多長,在每一種情況下取與這m-1個字符串匹配最小的長度(保證它可以和這n-1個字符都匹配的上),在57種情況中取最大的長度(保證是這n個字符的最長公共子序列)。 代碼:

#include<stdio.h>
#include<string.h>
char s[12][62],p[62];
char ans[62];
int next[62];
int N;
int getnext(int n)
{
  next[0]=-1;
  int i,j=1,k=-1;
  while(j<n)
  {
    while(k>-1&&p[j]!=p[k+1])
    {
      k=next[k];
    }
    if(p[j]==p[k+1])
    k++;
    next[j]=k;
    j++;
  }
  return 0;
}
int kmp(int n)
{
  getnext(n);
  int i,j,k,sum,mx=0;
  int max=100;
  for(i=1;i<N;i++)//與剩下n-1個字符匹配
  {
    j=0,k=0,mx=0;
    while(j<60&&k<n)
    {
      if(p[k]==s[i][j])//匹配時
      {
        k++;
        j++;
      }
      else
      {
        if(k==0)//回到了模式串的開頭
        j++;
        else
        k=next[k-1]+1;                

      }
      if(mx<k)
      mx=k;
    }
    if(max>mx)
    max=mx;
  }
  return max;
}
int main()
{
  int t;
  scanf("%d",&t);
  int i,j;
  int len;
  while(t--)
  {
    len=0;
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
      scanf("%s",s[i]);
      //printf("%s\n",s[i]);
    }
    for(i=0;i<58;i++)
    {
      strcpy(p,s[0]+i);
      p[60-i]=‘\0‘;
      int mx=kmp(60-i);
      if(len<mx)
      {
        strncpy(ans,s[0]+i,mx);
        ans[mx]=‘\0‘;
        len=mx;
      }
      else if(len==mx)
      {
        p[mx]=‘\0‘;
        if(strcmp(p,ans)<0)
        {
          strcpy(ans,p);
          ans[mx]=‘\0‘;
        }
      }
    }
    if(len>=3)
    printf("%s\n",ans);
    else
    printf("no significant commonalities\n");
  }
  return 0;
}

poj3080(kmp+枚舉)