点击打开链接

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. Show a heart shaped

    Windows Form application version: private void Form1_Load(object sender, EventArgs e)        {       ...

  2. sql查询重复记录并取对应最小值

    原表(aa): id   a                      b 1    22                    456 2    22                    256 ...

  3. eclipse 打开其他项目的jar源码 乱码解决

    步骤1.在eclipse菜单栏中,Window–>Preferences–>General–>Content types 将JAR Content , Java Class File ...

  4. MySQL Binlog 【ROW】和【STATEMENT】选择(转)

    前言:       二进制日记录了数据库执行更改的操作,如Insert,Update,Delete等.不包括Select等不影响数据库记录的操作,因为没有对数据进行修改.二进制主要的功能有:复制(Re ...

  5. Python(迭代器 生成器 装饰器 递归 斐波那契数列)

    1.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器的一大优 ...

  6. (转)mongodb分片

    本文转载自:http://www.cnblogs.com/huangxincheng/archive/2012/03/07/2383284.html 在mongodb里面存在另一种集群,就是分片技术, ...

  7. Python Beautiful Soup模块的安装

    以安装Beautifulsoup4为例: 1.到网站上下载:http://www.crummy.com/software/BeautifulSoup/bs4/download/ 2.解压文件到C:\P ...

  8. Windows 7无线网卡启用wifi共享蓝屏!

    我的笔记本是联想Y460P,装的是Windows 7 Ultiame(x64)系统,通过设置笔记本的无线(Intel WiFi Link 1000 BGN)搭建Wifi环境并共享,使手机能够通过笔记本 ...

  9. 解读Nodejs多核处理模块cluster

    来源: http://blog.fens.me/nodejs-core-cluster/ 从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发. ...

  10. Perl参考函数

    这是标准的Perl解释器所支持的所有重要函数/功能的列表.在一个函数中找到它的详细信息. abs - 绝对值函数 accept - 接受传入的socket连接 alarm - 调度一个SIGALRM ...