Blue Jeans(最大匹配问题)

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组数据,每组给出m串字符串,字符串由ACTG(碱基对)组成,求出这m个串的最大相同子串。

思路:自己硬钢的时候是没有思路的,看了一下下别人的代码,然后发现了自己一直以来对STL中string的使用以及认知方面的漏洞。(例如:find()==string::npos   。string s=a[0].substr(j,i);)。虽然是用了STL但还是是暴力.......题的数据小。(有kmp的冲动,不敢动,不敢动)。

代码如下:

#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
string a[1000];
int main()
{
    int t,i,n,j,k;
    cin>>t;
	while(t--)
	{
	    cin>>n;
		string b="";
		for(i=0;i<n;i++)
	    cin>>a[i];
	    for(i=0;i<=60;i++)//此处60为题目要求长度
	    {
		    for(j=0;j<=60;j++)
		    {
			   int p=0;
			   string s=a[0].substr(j,i); //截取下标为j后I位的子串
		       for(k=1;k<n;k++)
		       {
			       if(a[k].find(s)==string::npos) 
			       {
				       p=1;
			           break;
					}
		        }//枚举各串,判断该子串是否为公共子串
	            if(p==0)
	            if(s.size()>b.size()) b=s;
                else if(s.size()==b.size()&&b>s)b=s;
		    }//若为公共子串,进行最长更新
	    }
        if(b.size()<3)
		printf("no significant commonalities\n");
	    else
        cout<<b<<endl;
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/pleasantly1/article/details/80972798