(POJ - 3080)Blue Jeans
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18876 Accepted: 8402
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
题目大意:各处n个DNA序列,问这n个DNA序列最长的公共子串是什么,如果有多个输出字典序小的那一个。
思路:求最长公共子串,再看一下它的数据大小,小的可怜。摆明了让我们直接利用C++的string类来做,那么我们直接暴力求解就行,中间用到了C++string类的一些函数和变量。(其中s.find(tmp)==string::npos或s.find(tmp)==s.npos代表tmp这个字符串在s里面没找到)。
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=15;
string s[maxn];
bool cmp(string a,string b)
{
return a<b;
}
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
for(int i=0;i<n;i++) cin>>s[i];
sort(s,s+n,cmp);
int len=s[0].length();
string ans=" ";
for(int l=3;l<=len;l++)
for(int i=0;i+l<=60;i++)
{
string tmp=s[0].substr(i,l);
bool flag=true;
for(int j=1;j<n;j++)
if(s[j].find(tmp)==s[j].npos)
{
flag=false;
break;
}
if(flag&&tmp.length()>ans.length()) ans=tmp;
else if(flag&&tmp.length()==ans.length()&&tmp<ans) ans=tmp;
}
if(ans==" ") cout<<"no significant commonalities"<<endl;
else cout<<ans<<endl;
}
return 0;
}