D - Blue Jeans(4.6.1)

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
 
自己开始错误代码(原因:简单的找前一部分的最长子列,但是前面最长的不能代表后面全部的公共之列也是最长的,例如
3
abcdefpppfdsa
abcdefkkkfdsa
abckkofdsa)
错误代码:

#include<iostream> #include <string> #include<algorithm> using namespace std; int main( ) {  int i,j,x,y,l=3,u;  char  str[10][65],max[65],strin[65],String[65];  max[0]='\0';   int n;  cin>>n;  for(i=0;i<n;i++)   cin>>str[i];  strcpy(String,str[0]);  for(u=1;u<n;u++)  {   for(x=0;x<strlen(String);x++)////////   {      i=x;    for(y=0;y<strlen(str[u]);y++)    {     j=y;     l=0;     i=x;     for(;String[i]==str[u][j]&&i<strlen(String)&&j<strlen(str[u]);i++,j++)  ////////     //for(i++,j++;str1[i]==str2[j];i++,j++);这样和for(i++,j++;str1[i]==str2[j];i++,j++){}用法相同     {      strin[l]=String[i];      l++;     }     strin[l]='\0';     if(strlen(max)<strlen(strin))      strcpy(max,strin);

    //max[strlen(max)]='\0';    }   }   strcpy(String,max);    max[0]='\0';   cout<<String<<endl;   cout<<max<<endl;  }   if(strlen(max)<3)   cout<<"no significant commonalities"<<endl;  else   cout<<max<<endl;  return 0;

 

 

 

正确代码:

本题中引用两个库函数strncpy(s,p[0]+i,j-i+1)字符串复制函数和strstr(p[k],s)字符串查找函数   复制到s数组 被复制起始地址p[0]+i 复制长度 要查找的大数组 被查找的子数列

正确代码:

#include<cstdio> #include<cstring>

int main(void) {  int loop;  scanf("%d",&loop);  while(loop--)  {   int m;   int i;   char p[11][65];   scanf("%d",&m);   for(i=0;i<m;i++)    scanf("%s",p[i]);   int len;   char ans[65];   len=0;   for(i=0;i<strlen(p[0]);i++)        ///////////////////////要查找串的头部    for(int j=i+2;j<strlen(p[0]);j++) ///////////////////要查找的串的尾部    {     char s[65];     strncpy(s,p[0]+i,j-i+1);//字符串复制函数     s[j-i+1]='\0';     bool ok=true;     for(int k=1;ok&&k<m;k++) ////////在串中从第一个开始查找当前所选的串      if(strstr(p[k],s)==NULL)//字符串查找函数      {       ok=false;       break;////////////如果出现匹配失败时直接跳出      }     if(ok==false)      break; /////////////////////如果出现匹配失败的情况直接跳出当前串的查找,因为已经出现不能匹配的串了     if(ok&&(j-i+1>=len&&strcmp(ans,s)>0))//////////////////该处未理解??????????????     {      len=j-i+1;      strcpy(ans,s);     }    }    if(len<3)     printf("%s\n","no significant commonalities");    else    {     printf("%s\n",ans);    }  }  return 0; }

你可能感兴趣的:(D - Blue Jeans(4.6.1))