Blue Jeans
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15090 | Accepted: 6696 |
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.
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
题意就是求最多三个字符串的最长的公共子序列的问题。
考虑用后缀数组的思维求解,从第一个字符开始依次向后移动,取后缀数组, 然后拿到后两个字符串中进行比较,取两者匹配长度的最小值,最后取所有后缀数组中匹配长度的最大值作为结果。
一共就只有最多三个字符串,且每个字符串都只有60个字符,所以这么做是完全可以的。
代码如下:
/************************************************************************* > File Name: Blue_Jeans.cpp > Author: Zhanghaoran > Mail: chilumanxi@xiyoulinux.org > Created Time: Thu 26 Nov 2015 06:21:10 PM CST ************************************************************************/ #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cstdlib> using namespace std; char str[10][150]; int nexti[150]; char cmp[150]; int len; int T; int n; int ans; void preKMP(){ int i, j; i = 0; j = nexti[0] = -1; while(i < len){ if(j != -1 && cmp[i] != cmp[j]){ j = nexti[j]; } else if(cmp[++ i] == cmp[++ j]) nexti[i] = nexti[j]; else nexti[i] = j; } //for(int i = 0; i <= m; i ++){ // cout << kmpnext[i] << " "; //} //cout << endl; } void Kmp_Count(){ preKMP(); int i, j, res; ans = 110; for(int temp = 1; temp < n; temp ++){ i = 0; j = 0; res = 0; while(i < 60 && j < len){ if(j == -1 || str[temp][i] == cmp[j]){ i ++; j ++; } else j = nexti[j]; if(j > res) res = j; } if(res < ans) ans = res; } } int main(void){ scanf("%d", &T); char ss[150]; while(T --){ scanf("%d", &n); for(int i = 0; i < n; i ++) scanf("%s", str[i]); int res = 0; for(int i = 0; i <= 57; i ++){ strcpy(cmp, str[0] + i); len = 60 - i; Kmp_Count(); if(res < ans){ res = ans; strncpy(ss, str[0] + i, res); ss[res] = '