Blue Jeans
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 24011 Accepted: 10610
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 ©. 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
题目大意:
给你长度任意的n条串,要你找他们中最大长度的公共串是什么,如果长度相同按照字典序输出。
思路:
因为是最长公共串也就是说每个串的公共部分是相同的,我们只需要将第一条串拆开,然后和后面的串匹配找最长的串然后存起来,最后再从所有最长的串中选择最短的就是所有公共串的最长长度了。
思路大概就是上述,不过在写代码中需要注意几点:
将第一条串拆开,怎么拆?我用的O(n^2)算法,外循环长度k从1到n(一开始写的n-1结果错了一会还有评论区有测试样例,所以及时找出来了),内循环从i从0到n表示从下标i开始长度为k的字串,这样可以求出所有长度的子串,然后用子串和接下来的串用kmp去比较,匹配成功就存进去(我用的map),注意一下长度为等于的情况选择字典序小的存我们可以直接用string的<比较。
代码:
#include<vector>
#include<iostream>
#include<map>
#include<stdio.h>
#include<string>
using namespace std;
string str,ans,pt;
vector<int> getnext(string str) {//先拿next数组
vector<int>next(str.size());
for(int j=1,k=0; j<str.size(); j++) {
while(k>0&&str[j]!=str[k]) {
k=next[k-1];
}
if(str[j]==str[k]) {
next[j]=++k;
} else {
next[j]=k;
}
}
return next;
}
int main() {
int n,m;
scanf("%d",&n);
while(n--){
scanf("%d",&m);m--;
cin>>str;//先取第一条串做模式串
map<int,string>mp;//存匹配成功的串
map<int,string>mp2;//存最大长度的串
vector<int>next=getnext(str);
while(m--) {
cin>>pt;//文本串
int len=0;
for(int p=3;p<=str.size();p++)//可以枚举全部字串
for(int i=0;i<=str.size();i++){
string news=str.substr(i,p);//截取
for(int k=0,j=0;k<pt.size();k++){
while(j>0&&pt[k]!=news[j]){
j=next[j-1];
}
if(pt[k]==news[j])j++;
if(j==news.size()){//到达串尾匹配成功
if(news.size()>len){ //大于直接存到mp就好了
len=news.size();
mp[len]=news;
}else if(news.size()==len){//等于比较一下字典序
if(news<mp[len]){
mp[len]=news;
}
}
}
}
}
//if(mp2[len].empty()||mp[len]<mp2[len])
mp2[len]=mp[len];//将匹配成功的串中最大串存储起来
}
map<int, string>::iterator it = mp2.begin();
int tt=0;
for (; tt!=1; it++,tt++) {
if(it->first<3){
cout<<"no significant commonalities"<<endl;
}else{
cout<<it->second<<endl;
}
}
}
}