题目链接:http://poj.org/problem?id=3080
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
题意理解:
求出所给字符串的最长子串。
这个题方法比较多,这里就讲最简单并且直接的枚举方法。
因为给的m范围比较小,所以我们可以暴力一点。
先枚举给的第一个串和第二个串的子串,用sub_str求,并存入vector里面,
之后枚举vector里面的所有子串,与其他串匹配,如果不能匹配就删除,
最后对vector排一下序(因为要求相同长度字典序最小)就可以得到结果。
算是比较精妙的枚举吧。
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e6+7;
vector<string> s;
int nxt[maxn];
string s1,s2;
int m;
class cmp{
public:
bool operator ( )(const string a,const string b) const{
return a.size()==b.size()?a>b:a.size()<b.size();
}
};
void getnext(string a){
int len=a.size();
int j=0,k=-1;
nxt[0]=-1;
while(j<len){
if(k==-1||a[j]==a[k]){
j++;
k++;
nxt[j]=k;
}
else k=nxt[k];
}
}
int kmp(string a,string b){
int len1=a.size();
int len2=b.size();
int i=0,j=0;
getnext(b);
while(i<len1){
if(j==-1||a[i]==b[j]){
i++;
j++;
}
else j=nxt[j];
if(j==len2) return 1;
}
return 0;
}
int main(int argc, char** argv) {
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int T;
cin>>T;
while(T--){
cin>>m>>s1>>s2;
s.clear();
int len=s1.size();
for(int i = 0;i<len;++i){
for(int j = 0;j+i<len;++j){
string sub_str=s1.substr(i,j+1);
if(kmp(s2,sub_str))
s.push_back(sub_str);
}
}
for(int i = 1;i<=m-2;++i){
cin>>s2;
for(vector<string>::iterator it=s.begin();it!=s.end();){
if(!kmp(s2,*it)) it=s.erase(it);
else it++;
}
}
if(s.empty()){
cout<<"no significant commonalities\n";
continue;
}
string ans=*max_element(s.begin(),s.end(),cmp());
if(ans.size()<3) cout<<"no significant commonalities\n";
else cout<<ans<<endl;
}
return 0;
}