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. 微信小程序拒绝授权后提示信息以及重新授权

    wx.authorize({ scope: 'scope.writePhotosAlbum', success() { // 授权成功 wx.saveImageToPhotosAlbum({ file ...

  2. Python 自省指南

    原作者:Patrick K. O'Brien 什么是自省? 在日常生活中,自省(introspection)是一种自我检查行为.自省是指对某人自身思想.情绪.动机和行为的检查.伟大的哲学家苏格拉底将生 ...

  3. oracle查询语句注意事项:

    我想查出datatype 不等于1的所有结果,包括空. '; //这条sql查不出datatype为空的数据 发现oracle需要使用  is  null  .is  not null查询空或非空 ' ...

  4. poj 3693 Maximum repetition substring

    呵呵呵呵呵呵呵呵呵呵,sb(神犇)题看了一天,还是不懂 题目要求的是最多重复的,那么就来找重复的,可以先枚举一个重复的单元(比如ababab,就枚举ab)的长度, 然后再原串中,会有ch[0],ch[ ...

  5. UIWindow的那些事

    UIView是视图的基类,UIViewController是视图控制器的基类,UIResponder是表示一个可以在屏幕上响应触摸事件的对象: 一.UIWindow是一种特殊的UIView,通常在一个 ...

  6. Codeforces 405E DFS

    这个题目要求把一个无向连通图里面的所有边,分成 两个一对,只能出现一次,而且一对边必须是连在一起的,点可以复用  但边不可复用 可解条件很易得,因为图是连通的,只要边数为偶数即可. 一开始我借着做欧拉 ...

  7. Essay写作的六大黄金法则以及四大禁区

    虽然Essay这么难写,但是,也有一些可以拿高分的准则,本文小编就为大家分享高分Essay写作必知黄金法则,希望对想要在Essay拿高分的留学生小伙伴们有些帮助. 黄金法则1.关注相关问题的重点词汇 ...

  8. HZNU-ACM寒假集训Day4小结 最短路

    最短路 1.Floy 复杂度O(N3)  适用于任何图(不存在负环) 模板 --kuangbin #include<iostream> #include<cstdio> #in ...

  9. odoo 日志文件太大处理,logfile自动轮替

    可以在conf 文件中设置配置信息: logfile = /your/logfile/path/ logrotate = True ; 设置为True 即可自动更换旧的日志文件, 参考: https: ...

  10. promise核心技术 2.两种回调函数 js中error的处理

    抽空详细学习一下什么是回调函数(一个回调函数,也被称为高阶函数) 1.什么样的函数是回调函数 自己定义的(sittimeout不是自己定义的) 没有调用 自己执行 1.同步回调与异步回调函数 同步回调 ...