点击打开链接

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. 详尽介绍FireFox about:config

    一.什么是about:config about: config: 是Firefox的设置页面,Firefox提供了不少高级设置选项在这里以便让你可以更加详细地控制Firefox的运行方式.官方不推荐 ...

  2. TKinter之窗口美化 窗口大小、图标等

    设置窗口大小.设置窗口标题.设置窗口图标 效果图: 代码示例: #!/usr/bin/env python # _*_ coding:utf-8 _*_ from Tkinter import * r ...

  3. FileMode文件模式

    一.FileMode.Append Append追加: 如果文件存在,则打开文件,把指针指到文件的末尾:如果不存在,则新建文件 二.FileMode.Create Create:新建 如果文件存在,则 ...

  4. ASP.NET MVC 页面调整并传递参数

    转自:http://blog.csdn.net/zhensoft163/article/details/7174661 使用过ASP.NET MVC的人都知道在MVC中页面后台中常用的页面跳转方法有几 ...

  5. oracle 行转列的例子

    with test as(select '1' bit from dual union select '0' from dual )select replace(sys_connect_by_path ...

  6. 用Opera Mobile调试手机版网页【转】

    注意:新版本的opera已经采用webkit内核,没有dragonfly了. 要下载12版的http://get.geo.opera.com/pub/opera/win/1216/int/Opera_ ...

  7. Angular学习(3)- 双向梆定

    示例代码: <!DOCTYPE html> <html ng-app="MyApp"> <head> <title>Study 3& ...

  8. 【linux磁盘分区--格式化】fdisk,parted,mkfs.ext3

    磁盘分区完成后,一般就需要对分区进行格式化 磁盘分区命令主要有两个: fdisk :最大支持不超过2T分区: parted :支持GPT,适用于大容量分区: 分区指令的选择: 在RHEL系统上,用fd ...

  9. IntelliJ IDEA中如何设置同时打开多个文件且分行显示?

    Window→Editor Tabs→Tabs Placement→Show Tabs in Single Row 取消选中后即可在多行显示 下图为实际显示效果:  还可以自行设置打开文件窗口数(默认 ...

  10. hist和bar画图关系

    1.hist是绘制直方图,直方图显示了数据值的分布情况.  1>n = hist(Y,n)      将向量Y中的元素分到n个等间隔的范围内(默认为10个间隔),并返回每个范围内元素的个数作为一 ...