poj 3080 Blue Jeans

点击打开链接

Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10243   Accepted: 4347

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

题目大意就是求m个子串的最长公共子串,如果最长公共子串长度小于3,则输出 no significant commonalities,如果有多个长度相同的,则输出字典序比较小的

方法比较暴力,就是先求第一个和第二个的公共子序列,然后把所有的子序列存下来,放到一个set里,然后再从set里拿出1,2的子序列和第三个序列比,再把求得的子序列继续扔到set里,再和下一个比,直到最后一个,set里剩下的就是所有的公共子序列了,这里提一点,上面所有的比较过程中如果某个序列长度小于3了,就直接丢掉了,不会再计算,最后在set里找一个字典序最小的

#include<stdio.h>
#include<string.h>
#include<set>
#include<string>
using namespace std; set<string> ans;
char dna[11][61];
void cmp_sub(int m)
{
int i, k, l, f;
char temp[61];
char sub[61];
int total = ans.size();
set<string>::iterator j, prev;
for(i = 2; i < m; i++)
{
for(j = ans.begin(); j != ans.end();)
{ int len = (*j).length();
int count = 0;
int max = -1;
bool flag = 0;
for(k = 0; k < 60 && flag == 0; k++)
{ for(l = 0; l < len && flag == 0; l++)
{
if(dna[i][k] == (*j)[l])
{
count = 1;
sub[0] = dna[i][k];
for(f = 1; dna[i][k + f] == (*j)[l + f] && f + l < len && dna[i][k + f]; f++)
{
count++;
sub[f] = dna[i][k + f];
}
if(count >= 3)
{
max = count;
sub[f] = 0;
strcpy(temp, sub);
ans.insert(sub);
}
if(strcmp((*j).c_str(), temp) == 0 )
{
flag = 1;
break;
}
}
}
}
if(flag == 0)
{
prev = j;
j++;
ans.erase(prev); if(strlen(temp) > 2)
ans.insert(temp);
}
else
j++;
}
}
}
void find_sub()
{
int i, j, k;
char sub[61];
int total = 0;
int count;
string str;
for(i = 0; i < 60; i++)
{
for(j = 0; j < 60; j++)
{
if(dna[0][i] == dna[1][j])
{
count = 1;
sub[0] = dna[0][i];
for(k = 1; dna[0][i + k] == dna[1][j + k] && dna[0][i + k] && dna[1][j + k]; k++)
{
sub[k] = dna[0][k + i];
count++;
}
if(count >= 3)
{
sub[k] = 0;
str = sub;
ans.insert(str);
}
}
}
}
}
int main()
{
// freopen("t//t.txt", "r", st//n);
int n, m;
scanf("%d", &n);
while(n--)
{
scanf("%d", &m);
ans.clear();
int i;
getchar();
for(i = 0; i < m ;i++)
gets(dna[i]);
find_sub();
if(ans.size() == 0)
{
printf("no significant commonalities\n");
continue;
}
cmp_sub(m);
set<string>::iterator j, mark;
int max = 0;
for(j = ans.begin(); j != ans.end(); j++)
{
// printf("%d\n%d\n%d\n%d\n",(*j).length(), max, (((*j).length()) > max), ((*j).length() > 2)) ;
if(((*j).length() > max) && ((*j).length() > 2))
{
max = (*j).length();
mark = j;
}
}
if(max > 2)
{
printf("%s\n", (*mark).c_str());
}
else
printf("no significant commonalities\n");
}
return 0;
}
上一篇:c++实验8 哈夫曼编码-译码器


下一篇:文本分类学习 (五) 机器学习SVM的前奏-特征提取(卡方检验续集)