您的位置:首页 > 产品设计 > UI/UE

poj3080--Blue Jeans(字符串匹配)

2014-08-15 16:03 405 查看
Blue Jeans

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12233 Accepted: 5307
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

Source
South Central USA 2006
 
枚举s[0]每一段的长度,与下面的n-1个匹配,看是否是字串,最后保留下,长度最长的,如果长度相同的话,要求字典序最小。(如果长度小于3输出no significant commonalities)。
对于连续子串匹配,也可用子序列匹配来做,只是,连续的长度选用最长的对角线。
 
 
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int mm[100][100] ;
char s[100][100] , str1[100] , str2[100] ;
int f(int i,int j,int k,int num)
{
int ii , jj , ll ,flag;
ll = strlen(s[k]);
memset(mm,0,sizeof(mm));
flag = 0 ;
for(ii = i ; ii <= j ; ii++)
{
for(jj = 0 ; jj < ll ; jj++)
{
if(ii == i || jj == 0)
{
if( s[0][ii] == s[k][jj] )
mm[ii][jj] = 1 ;
}
else
{
if( s[0][ii] == s[k][jj] )
mm[ii][jj] = 1 ;
if( mm[ii][jj] )
mm[ii][jj] += mm[ii-1][jj-1] ;
if( mm[ii][jj] == num )
flag = 1 ;
}
}
}
if( flag )
return 1 ;
else
return 0 ;
}
int main()
{
int t , n , i , j , l , ll , k , num , flag , max1 , a , b ;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(i = 0 ; i < n ; i++)
scanf("%s", s[i]);
l = strlen(s[0]);
max1 = 0 ;
for(i = 0 ; i < l ; i++)
{
for(j = l-1 ; j-2 >= i ; j--)
{
num = j - i + 1 ;
for(k = 1 ; k < n ; k++)
{
if( !f(i,j,k,num) )
break;
}
if(k == n )
{
if(num > max1)
{
max1 = num ;
for(a = 0 ; a < num ; a++)
str1[a] = s[0][a+i] ;
str1[a] = '\0' ;
}
else if( num == max1 )
{
for(a = 0 ; a < num ; a++)
str2[a] = s[0][a+i] ;
str2[a] = '\0' ;
if( strcmp(str1,str2) > 0 )
strcpy(str1,str2);
}
}
}
}
if( max1 < 3 )
printf("no significant commonalities\n");
else
{
printf("%s\n", str1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: