点击打开链接

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;
}

poj 3080 Blue Jeans的更多相关文章

  1. POJ 3080 Blue Jeans (求最长公共字符串)

    POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...

  2. POJ 3080 Blue Jeans (字符串处理暴力枚举)

    Blue Jeans  Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 21078        Accepted: ...

  3. POJ 3080 Blue Jeans(Java暴力)

    Blue Jeans [题目链接]Blue Jeans [题目类型]Java暴力 &题意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 规定: 1. 最长公共 ...

  4. POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20966   Accepted: 9279 Descr ...

  5. poj 3080 Blue Jeans 解题报告

    题目链接:http://poj.org/problem?id=3080 该题属于字符串处理中的串模式匹配问题.题目要求我们:给出一个DNA碱基序列,输出最长的相同的碱基子序列.(保证在所有的序列中都有 ...

  6. poj 3080 Blue Jeans(水题 暴搜)

    题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...

  7. POJ 3080 Blue Jeans(后缀数组+二分答案)

    [题目链接] http://poj.org/problem?id=3080 [题目大意] 求k个串的最长公共子串,如果存在多个则输出字典序最小,如果长度小于3则判断查找失败. [题解] 将所有字符串通 ...

  8. POJ 3080 Blue Jeans 后缀数组, 高度数组 难度:1

    题目 http://poj.org/problem?id=3080 题意 有m个(2<=m<=10)不包含空格的字符串,长度为60个字符,求所有字符串中都出现过的最长公共子序列,若该子序列 ...

  9. poj 3080 Blue Jeans【字符串处理+ 亮点是:字符串函数的使用】

    题目:http://poj.org/problem?id=3080 Sample Input 3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCA ...

随机推荐

  1. SQL中 EXCEPT、INTERSECT用法

    EXCEPT 返回两个结果集的差(即从左查询中返回右查询没有找到的所有非重复值). INTERSECT 返回 两个结果集的交集(即两个查询都返回的所有非重复值). UNION返回两个结果集的并集. 语 ...

  2. maven学习笔记(定制普通Java一个项目)

    创建一个新项目: mvn archetype:generate -DgroupId=cn.net.comsys.ut4.simpleweather -DartifactId=simple-weathe ...

  3. ASP.NET MVC3在页面上获取当前控制器名称、Action名称以及路由参数

    参考:http://ulfqbpl.blog.163.com/blog/static/87783552201272824843607/ 获取控制器名称: RouteData.Values[" ...

  4. mysql 获取一个表中缺失的最小编号

    select count(*),t1.`name` from test_id t1INNER JOIN test_id t2on t1.id >= t2.idgroup by t1.id,t1. ...

  5. (转帖)BootStrap入门教程 (三)

      上讲回顾:Bootstrap的基础CSS(Base CSS)提供了优雅,一致的多种基础Html页面要素,包括排版,表格,表单,按钮等,能够满足前端工程师的基本要素需求. Bootstrap作为完整 ...

  6. 关于SqlServer的DBHelper类以及数据分页

    前端: <My:AspNetPager class="arPage" PageSize="20" UrlPaging="true" r ...

  7. EXECUTE 后的事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配

    代码中的事务包含了存储过程中的事务.

  8. LintCode "Maximum Gap"

    Bucketing! A lot of details to take care. struct Bucket { Bucket() :l(-), r(-), bValid(false){}; int ...

  9. HDU3516 树的构造

    题目大意:平面上有n个点,构成一个单调递减的序列.即对于任意的i<j,有xi<xj,yi>yj.现在要用一棵树连接这n个点.树边为有向边,只能向右或向上.求最小的权值. 分析:本题其 ...

  10. encodeURIComponent编码后java后台的解码 (AJAX中文解决方案)

    encodeURIComponent编码后java后台的解码 (AJAX中文解决方案) 同学的毕业设计出现JavaScript用encodeURIComponentt编码后无法再后台解码的问题. 原来 ...