Blue Jeans
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 10618 | Accepted: 4537 |
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
大致题意:给定n组测试数据每组有m个字符串(长度为60),找出这些字符串的最长公共子串,如果有多个长度相等的公共子串,输出按字典序出现的第一个子串。
解题思路:因为数据量并不是很大,采用暴力枚举就可以AC,具体实现:首先枚举其中一个字符串的子串
(从最长子串枚举),再在其他字符串中查找,如果都包含且是第一次出现此长度的子串,先保存起来,
继续查找此长度的子串,如果再次出现与上次找到的比较(用strcmp)判断字典序大小,并保存小的,
重复上述步骤,知道遍历完此长度的所有子串,最后输出结果。
提示:我并没有按固定长度60,因为以前做的一个题与这个题类似,但字符串是不定长的,如果这个题也改成
不定长的,应该更有意思。
代码:
#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>
using namespace std;
string str[11], sz;
int main()
{
int n, m, i, len;
scanf("%d", &n);
while (n--)
{
scanf("%d", &m);
len = 0xfffffff;
getchar();
int idx;
for (i=0; i<m; ++i)
{
getline(cin, str[i]);
if (str[i].length() < len)
{
idx = i;
len = str[i].length();
}
}
int j, k, flag2 = 0, flag1;
char f[200], tmp[200];
for (j=len; j>=3; --j)
{
for (k=0; k<=len-j; ++k)
{
flag1 = 1;
sz = str[idx].substr(k, j);
for (i=0; i<m; ++i)
{
if (i == idx)
continue;
if (str[i].find(sz) != -1)
strcpy(tmp, sz.c_str());
else
{
flag1 = 0;
break;
}
}
if (flag1)
{
if (flag2)
{
if (strcmp(tmp, f) < 0)
strcpy(f, tmp);
}
else
strcpy(f, tmp);
flag2 = 1;
}
}
if (flag2)
break;
}
if (flag2)
puts(f);
else
puts("no significant commonalities");
}
return 0;
}
本博客探讨了一个关于生物信息学的问题,即在一组DNA序列中寻找最长的公共子串。通过给出的示例输入和输出,阐述了解题思路和算法实现,包括如何通过枚举子串并检查其在所有序列中的共存性来解决问题。
1455

被折叠的 条评论
为什么被折叠?



