POJ

来源:互联网 发布:vue.js权威指南 微盘 编辑:程序博客网 时间:2024/06/15 02:30
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
32GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA3GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAGATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAAGATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA3CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalitiesAGATACCATCATCAT
题意: 就是给你几个长度为60的字符串,让你在这里找到最长的公共字串。
思路: 把第一个字符拆成他的所有字串,然后一个一个的去匹配。
本来写这道题的时候没想到怎么写 (菜),然后去网上看看了才发现要这样写,,太暴力了吧啊,,
然后一个串的所有字串 时间复杂度是n*n*n,据说字典树可以把复杂的降到 n*n,现在还不会写 ,先占个坑把上代码 
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;char ch[11][70];int nex[70];void pre_kmp(char str[]){memset(nex,0,sizeof(nex));//printf("nex===%s   \n",str);int len = strlen(str) , k = -1 ,i = 0;nex[0]=-1;while(i < len){if( k == -1 || nex[i] == nex[k]){i++;k++;nex[i]=k;}else {k = nex[k];}}}int kmp(char Str[],char str[]){int Len = strlen (Str);int len = strlen (str);//printf("  kmp =  %s    %s  \n",Str,str);int k = 0 , i = 0;while( i < Len){if(k == -1 ||Str[i] == str[k]){i++;k++;}else k = nex[k];if( k == len){return 1;}}return 0;}int cmp(char ch[],char Ch[]){int len = strlen(ch);int Len = strlen(Ch);if(len != Len){return len > Len;}if(len == Len){return strcmp(ch,Ch)<0;}}int main(){int n;scanf("%d",&n);while(n--){int m;char te[70];char ans[70];scanf("%d",&m);for(int i = 0 ; i < m ; i++){scanf("%s",ch[i]);}memset(te,0,sizeof(te));memset(ans,0,sizeof(ans));int len = strlen (ch[0]);for(int i = 0 ; i < len ; i++){for(int j = i; j < len; j++){int cnt=0;memset(te,0,sizeof(te));for(int k = 0 ; k < j - i + 1 ; k++){te[k] = ch[0][k+i];}pre_kmp(te);//printf(" TTTTE= %s\n",te);int flag = 1;for(int pp = 0 ; pp < m; pp++){//printf("te = = =%s   pp =%s\n",te,ch[pp]);if(!kmp(ch[pp],te)){flag = 0;break ;}//printf("flag = %d \n",flag);//puts("---------\n");} if(flag){if(cmp(te,ans))strcpy(ans,te);}} }//printf("------%s  \n",ans);if(strlen(ans)<3){puts("no significant commonalities");}else {printf("%s\n",ans);}}return 0;}/*4 3abcdefgssksjdkausiodsmdjaiowdiwkasdjassldiowedskdjaksabcdefgeksjdkaueeeeeeesabcdefgfksjdkauqqqqqqqqqqqqqqqqqqqqqqs*/


原创粉丝点击