Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions:21746 | Accepted: 9653 |
Description
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
- 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
Sample Input
3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities
AGATAC
CATCATCAT
Source
题意:
给定$m$个场长度为$60$的字符串,问他们的最长公共子串。
思路:
因为$m$和长度都很小,所以可以暴力枚举一个串的所有子串,$KMP$进行匹配。
等之后【后缀数组】刷熟练一点了再用后缀数组做一次。奇怪的是hdu2328明明是一样的题意,怎么就总是WA
#include<iostream>
//#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<climits>
#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define pi 3.1415926535
#define inf 0x3f3f3f3f const int maxn = ;
int n, m;
char str[][maxn];
int nxt[maxn]; void getnxt(char *s)
{
int len = strlen(s);
nxt[] = -;
int k = -;
int j = ;
while(j < len){
if(k == - || s[j] == s[k]){
++k;++j;
if(s[j] != s[k]){
nxt[j] = k;
}
else{
nxt[j] = nxt[k];
}
}
else{
k = nxt[k];
}
}
} bool kmp(char *s, char *t)
{
getnxt(s);
int slen = strlen(s), tlen = strlen(t);
int i = , j = ;
while(i < slen && j < tlen){
if(j == - || s[i] == t[j]){
j++;
i++;
}
else{
j = nxt[j];
}
}
if(j == tlen){
return true;
}
else{
return false;
}
} int main()
{
scanf("%d", &n);
while(n--){
scanf("%d", &m);
for(int i = ; i < m; i++){
scanf("%s", str[i]);
} char ans[maxn];
int anslen = -;
for(int i = ; i < ; i++){
for(int j = i; j < ; j++){
char t[maxn];
memcpy(t, str[] + i, j - i + );
t[j - i + ] = '\0';
bool flag = true;
for(int k = ; k < m; k++){
if(!kmp(str[k], t)){
flag = false;
break;
}
}
if(flag){
if(j - i + > anslen){
anslen = j - i + ;
strcpy(ans, t);
}
else if(j - i + == anslen){
if(strcmp(ans, t) > ){
strcpy(ans, t);
}
}
}
else{
break;
}
}
} if(anslen < ){
printf("no significant commonalities\n");
}
else{
//cout<<ans<<endl;
printf("%s\n", ans);
} }
return ;
}