KMP专场 POJ
I - kmp
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
POJ 3080
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
/********************************* author : Grant Yuan algorithm; kmp source : POJ 3080 time ;2014/10/3 20:38 *********************************/ #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cstdio> using namespace std; char a[12][62]; char arr[62]; int ans; char ansarr[62]; int n,next[62],l1,l2; void get_next() { int i=0,j=-1; next[0]=-1; while(i<l1){ if(arr[i]==arr[j]||j==-1){ i++;j++; next[i]=j; } else j=next[j]; } } bool kmp(char *s) { int sum=0; int i=0,j=0; while(i<l2){ if(arr[j]==s[i]) {i++;j++;} else{j=next[j];if(j==-1){i++;j=0;}} if(j==l1) sum++; } if(sum>0) return true; return false; } int main() { int t; scanf("%d",&t); while(t--){ scanf("%d",&n); memset(arr,0,sizeof(arr)); ans=0; for(int i=0;i<n;i++) { scanf("%s",a[i]); } for(int i=0;i<=59;i++) for(int j=1;j+i<=60;j++) { memset(arr,0,sizeof(arr)); strncpy(arr,a[0]+i,j); get_next(); l1=strlen(arr);l2=60; bool flag=1; for(int k=1;k<n;k++) { if(kmp(a[k])==0) {flag=0;break;} } if(flag) { if(l1>ans){ans=l1;memset(ansarr,0,sizeof(ansarr));strcpy(ansarr,arr);} else if(l1==ans){ans=l1;if(strcmp(arr,ansarr)<0) strcpy(ansarr,arr);} } } if(ans<3) printf("no significant commonalities\n"); else printf("%s\n",ansarr);} return 0; }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步