poj3080--Blue Jeans(字符串匹配)
2014-08-15 16:03
405 查看
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
Sample Output
Source
South Central USA 2006
枚举s[0]每一段的长度,与下面的n-1个匹配,看是否是字串,最后保留下,长度最长的,如果长度相同的话,要求字典序最小。(如果长度小于3输出no significant commonalities)。
对于连续子串匹配,也可用子序列匹配来做,只是,连续的长度选用最长的对角线。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12233 | Accepted: 5307 |
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; }
相关文章推荐
- Blue Jeans(poj3080简单字符串匹配)
- poj3080 Blue Jeans (多个字符串匹配)
- POJ3080——Blue Jeans(暴力+字符串匹配)
- poj 3080 Blue Jeans 字符串匹配
- poj 3080 Blue Jeans kmp 字符串匹配
- 【庞果网英雄会】4月第4次面试集训#字符串#:字符串匹配问题
- 字符串匹配
- 字符串匹配——来自华为OJ平台测试的基础篇
- HDU 1075 字典树 字符串匹配
- PK字符串匹配(C语言)
- 算法——字符串匹配之Rabin-Karp算法
- 字符串匹配的几种算法总结(KMP、等)
- 字符串匹配之KMP算法
- HDU 5716 带可选字符的多字符串匹配 ShiftAnd
- 字符串匹配(2)
- 算法导论-字符串匹配
- 字符串匹配——有限自动机
- 字符串匹配
- 程序员编程艺术第三十~三十一章:字符串转换成整数,通配符字符串匹配
- 字符串匹配与KMP算法笔记