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

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


GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample Output

no significant commonalities
AGATAC
CATCATCAT

题意:给定长度为60的m个字符串,找它的最长公共子串,如果长度相同,输出字典序小的,如果找到的公共子串小于3 ,就输出 一串不认识的字母,否则就输出找到的那一串同样不认识的字母。

注意到,这个题给的数据范围很小,m不大于10,长度不大于60,吐过暴力枚举的话,复杂度大概是60*60*10*(60+60),不会爆,所以果断枚举,

暴力找就行,枚举相同序列的长度以第一个DNA为模板向其他串中找。其中有个技巧性的地方就是strstr()函数的使用,strstr(a,b)函数为在a中找b,如果可以找到b那么会返回最初始找到b时的位置的地址,若找不到b则返回NULL。

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <algorithm> using namespace std; int main()
{
//freopen("sample.txt","r",stdin);
int n;
scanf("%d",&n);
while(n--)
{
char str[][];
char ans[];
ans[]=;
int m;
scanf("%d",&m);
getchar();
for(int i=;i<m;i++)
{
gets(str[i]);
}
for(int i=;i<=strlen(str[]);i++)
{
int af=;
for(int j=;j<=strlen(str[])-i;j++)
{
int flag=;
char ttm[];
strncpy(ttm,str[]+j,i);
ttm[i]=;
for(int g=;g<m;g++)
{
if(!strstr(str[g],ttm))
{
flag=;
break;
}
}
if(flag)
{
af=;
if(strlen(ttm)>strlen(ans))
strcpy(ans,ttm);
else if(strlen(ttm)==strlen(ans)&&strcmp(ttm,ans)<)
strcpy(ans,ttm);
}
}
if(!af)
break;
}
if(strlen(ans)<)
printf("no significant commonalities\n");
else
puts(ans);
}
return ;
}

POJ 3080 Blue Jeans (求最长公共字符串)的更多相关文章

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

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

  2. 算法 -- 求最长公共字符串&PHP

    https://blog.csdn.net/hongyuancao/article/details/83308093 本文是利用PHP,求最长公共字符串.思路:利用动态规划和矩阵的思想. 动态规划:就 ...

  3. POJ 3080 Blue Jeans (多个字符串的最长公共序列,暴力比较)

    题意:给出m个字符串,找出其中的最长公共子序列,如果相同长度的有多个,输出按字母排序中的第一个. 思路:数据小,因此枚举第一个字符串的所有子字符串s,再一个个比较,是否为其它字符串的字串.判断是否为字 ...

  4. POJ - 3080 Blue Jeans 【KMP+暴力】(最大公共字串)

    <题目链接> 题目大意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 限制条件: 1.  最长公共串长度小于3输出   no significant co ...

  5. poj 3080 Blue Jeans

    点击打开链接 Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10243   Accepted: 434 ...

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

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

  7. POJ 3080 Blue Jeans(Java暴力)

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

  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 该题属于字符串处理中的串模式匹配问题.题目要求我们:给出一个DNA碱基序列,输出最长的相同的碱基子序列.(保证在所有的序列中都有 ...

随机推荐

  1. P 1025 链表反转

    转跳点:

  2. 每天一点点之vue框架开发 - 引入bootstrap

    只使用css样式   如果在你的项目中只是使用css样式,那就不需要安装,直接全局引入样式就好 <link rel="stylesheet" href="https ...

  3. MFC双缓冲

    大家都知道包括windows桌面在内我们看到的一切都是系统画上去的,windows桌面就相当于一个黑板: <1>普通绘图就是直接在我们看得到的黑板上绘图 <2>双缓冲就是先在一 ...

  4. EUI库 - 10 - 使用自定义组件

      步骤 1 在根节点,添加一个自定义的命名空间  2 可以设置skinName 自定义组件规范 1 不复用的不要用自定义组件 2 属性必须要有默认值(赋值为null也可以),因为TS编译器会把没有默 ...

  5. oracle数据库语言(1)--数据定义语言

      1.数据定义语言 (DDL)DATE DEFINITION LANGUAGE 作用是用于增删改 数据库对象 (1) 创建表格 CREATE TABLE EMP ( -------创建 名为 EMP ...

  6. 新手转行必知!Python和Java到底有啥区别?

    TIOBE 9月编程语言排行榜中Java第一,但PYPL 9月排行榜中Python却是第一.两个编程语言排行榜均是旨在给开发者做一个学习参考,那么问题来了:Java和Python都很火,两个语言到底有 ...

  7. spring boot redis 缓存(cache)集成

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  8. [APIO2018]铁人两项(圆方树)

    过了14个月再重新看这题,发现圆方树从来就没有写过.然后写了这题发现自己APIO2018打铁的原因竟然是没开long long,将树的部分的O(n)写挂了(爆int),毕竟去年APIO时我啥都不会,连 ...

  9. VC++ DLL 2 静态链接库

    这一篇以VS2013为例子介绍怎样编写一个静态链接库和调用. 1.打开VS2013,新建Visual C++ 的win32项目: 新建后工程分支如下: 添加头文件和源文件: 编写头文件和源文件内容: ...

  10. BZOJ 3170 [Tjoi2013]松鼠聚会

    题解:切比雪夫距离转化为曼哈顿距离 枚举源点,横纵坐标互不影响,分开考虑,前缀和优化 横纵分开考虑是一种解题思路 #include<iostream> #include<cstdio ...