kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans

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 感觉暴力可以,但是没有去写。想用kmp,但是又无从下手,就学习了一波操作。 首先暴力第一串的所有子串,然后再其他字符串里面找是否存在。技巧之一就是从长到短枚举。 暴力:
 #include<iostream>
#include<stdio.h>
#include<string>
#include<set>
#include<vector>
using namespace std;
vector<string> t;
set<string> ss;
string s;
int _,n; string fun() {
ss.clear();
string str=t[];
bool flag;
for(int len=;len>=;len--) {
for(int ix=;ix<=-len;ix++) {
string temp=str.substr(ix,len);
flag=true;
for(int k=;k<t.size();k++) {
if(t[k].find(temp)==-) {
flag=false;
break;
}
}
if(flag) ss.insert(temp);
}
if(ss.size()) return *ss.begin();
}
return "no significant commonalities";
} int main() {
// freopen("in","r",stdin);
for(scanf("%d",&_);_;_--) {
scanf("%d",&n);
for(int i=;i<n;i++) {
cin>>s;
t.push_back(s);
}
cout<<fun()<<endl;
t.clear();
} }

kmp思想:不需要找第一个串的所有子串,只需枚举每一个后缀,去和其他字符串匹配就行了。其实这个匹配过程就好比所有子串进行匹配了。

 #include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int _,n,Next[];
string s,strans;
vector<string> t; void prekmp(string s) {
int len=s.size();
int i,j;
j=Next[]=-;
i=;
while(i<len) {
while(j!=-&&s[i]!=s[j]) j=Next[j];
if(s[++i]==s[++j]) Next[i]=Next[j];
else Next[i]=j;
}
} int kmp(string p,string t) {
int len=t.size();
int i=,j=,res=-;
while(i<len) {
while(j!=-&&t[i]!=p[j]) j=Next[j];
++i;++j;
res=max(res,j);
}
return res;
} int main() {
// freopen("in","r",stdin);
for(scanf("%d",&_);_;_--) {
scanf("%d",&n);
for(int i=;i<n;i++) {
cin>>s;
t.push_back(s);
}
int ans=-;
string str=t[];
for(int i=;i<;i++) {
string temp=str.substr(i,-i);
prekmp(temp);
int maxx=;
for(int j=;j<t.size();j++) {
maxx=min(maxx,kmp(temp,t[j]));
}
if(maxx>ans) {
strans=temp.substr(,maxx);
ans=maxx;
} else if(maxx==ans) {
string anstemp=temp.substr(,maxx);
if(anstemp<strans) strans=anstemp;
}
}
if(strans.size()<) cout<<"no significant commonalities"<<'\n';
else cout<<strans<<'\n';
t.clear();
}
}
上一篇:网络虚拟化技术 TUN/TAP MACVLAN MACVTAP


下一篇:k近邻算法的Java实现