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. POJ 2182&& POJ 2828:Lost Cows 从后往前 线段树

    Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10544   Accepted: 6754 Descri ...

  2. tensorflow--保存加载模型

    s=mnist.train.next_batch(batch_size)print(xs.shape)print(ys.shape) # #从集合中取全部变量# tf.get_collection() ...

  3. Stuts2与SpringMVC

    Struts2:一个基于MVC设计模式的Web应用框架,本质上相当于一个servlet.以WebWork为核心,采用拦截器的机制处理用户的请求(Filter). 轻量级的MVC框架.低侵入性,与业务代 ...

  4. C++编程学习(一) 概述

    从一年前开通博客,陆陆续续写了一些总结类的东西,但是一直没push到网上.现在发现,分享自己的笔记也是自身学习.与他人交流的好方式.从今天开始,我会经常的push一些学习笔记到博客中,先从C++开始吧 ...

  5. storm on yarn安装时 提交到yarn失败 failed

    最近在部署storm on yarn ,部署参考文章 http://www.tuicool.com/articles/BFr2Yvhttp://blog.csdn.net/jiushuai/artic ...

  6. BZOJ:2244: [SDOI2011]拦截导弹

    问题: printf("%.5f ",0):为什么错了? 注意: 初始值很重要 题解: 三维偏序问题: 记录从前往后最长上升子序列长度pref,条数preg 从后往前suff,su ...

  7. Java基础查漏补缺(1)

    Java基础查漏补缺 String str2 = "hello"; String str3 = "hello"; System.out.println(str3 ...

  8. Navicat Premium 12.0.18 安装与激活

    Navicat Premium 12.0.18中文版 百度云链接:https://pan.baidu.com/s/1HHOOlQbbWAL-MlI908n4MQ 提取码:k9w6 1.下载好后双击运行 ...

  9. centos6-7 yum安装php的方法

    1.检查当前安装的PHP包 yum list installed | grep php 如果有安装的PHP包,先删除他们 yum remove php.x86_64 php-cli.x86_64 ph ...

  10. kafka分区选主机制

    Kafka Partition Leader选主机制 https://blog.csdn.net/qq_27384769/article/details/80115392 kafka leader选举 ...