Blue Jeans(字符串kmp)

Blue Jeans

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17146 Accepted: 7602

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

题意:给你几个DNA序列长度为60,以第一个为模板,找到之后的DNA中与模板DNA相同的子序列,且保证子序列最长(长度大于等于3)。

用kmp,以第一个为模板,将其长度由60缩减到3,后续字符串依次与其匹配看

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char text[15][70];
int next[70];
void getnext(char s[],int len)
{
   int i=0,j=-1;
   next[0]=-1;
   while(i<len)
   {
     if(j==-1||s[i]==s[j])
     {
        i++;
        j++;
        next[i]=j;
     }
     else
     j=next[j];
   }

}
int kmp(char a[],char b[])
{
   int i=0,j=0;
   int len1=strlen(a),len2=strlen(b);
   while(i<len1&&j<len2)
   {
      if(j==-1||a[i]==b[j])
      {
         i++;
         j++;
      }
      else j=next[j];
   }
   if(j==len2)
   return 1;
   else return 0;
}
int main()
{
    int T,i,j,n,flag1;
    scanf("%d",&T);
    while(T--)
    {
      char ans[70]={'Z'};//相当于inf最大
      scanf("%d",&n);
      for(i=1;i<=n;i++)
      {
          scanf("%s",text[i]);
      }
      int len;

      for(len=60;len>=3;len--)//模板串的长度,最小为三,暴力枚举
      {
          for(i=0;i<=60-len;i++)//可能有多个符合条件的同长度的符合条件字符串,最多不能超过60-len保证最后一个len长度的字符串
          {   char str[70];
              strncpy(str,text[1]+i,len);//把字符串从第一个字母开使依次后移一个单位,每次都将长度为len的字符串赋给str
              str[len]='\0';
              getnext(str,len);//str作为子串
              int flag=0;
              for(j=2;j<=n;j++)
              {
                  if(!kmp(text[j],str))//后续串判断
                  {
                     flag=1;
                     break;
                  }
              }
          if(flag==0&&strcmp(ans,str)>0)//全部都能匹配,得到一个字符串赋给ans,ans相当于inf为了得到字典序最小的字符串
          {
             strcpy(ans,str);//len长度的字符串可能有多个
          }
          }
          flag1=0;
          if(ans[0]!='Z')
          {
             printf("%s\n",ans);//找到了符合条件的字符串输出后结束
             flag1=1;
             break;
          }


      }
      if(flag1==0)//没找到
      {
         printf("no significant commonalities\n");
      }
    }
    return 0;
}

有无符合要求的字符串

    原文作者:KMP算法
    原文地址: https://blog.csdn.net/jinzk123/article/details/53310868
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞