题目链接:http://poj.org/problem?id=3080

学习博客:https://www.cnblogs.com/acjiumeng/p/6818213.html

Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21342   Accepted: 9467

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

Source

 
题目大意:输入t,t组样例,输入n,有n个字符串,求他们最长的公共子串是什么,如果长度小于3,输出"no significant commonalities",否则输出那个最长的公共子串(如果有相同长度的输出字典序小的)
思路:这一题一直觉得用kmp会超时,后来看了一下别人的代码,并不会超时,这里介绍一下这一道题的时间复杂度:

首先你必须遍历第一个串每一个子串长度的(60*60),然后求每一个子串的next[]数组,最大为60,最后和剩下的串进行比较,最大10*60,所以这道题最大的时间复杂度是(60*60*60*10*60),差不多刚好卡在时限左右

看代码:

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=;
const int maxn=+;
const int maxk=5e3+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
string a[];
int next[maxn];
void cal_next(string s)
{
int len=s.length();
int k=-;
next[]=-;
for(int i=;i<len;i++)
{
while(k>-&&s[k+]!=s[i])
{
k=next[k];
}
if(s[k+]==s[i]) k++;
next[i]=k;
}
}
bool kmp(string x,string y)
{
int k=-;
for(int i=;i<x.length();i++)
{
while(k>-&&y[k+]!=x[i])
{
k=next[k];
}
if(x[i]==y[k+]) k++;
if(k==y.length()-) return true;
}
return false;
}
int main()
{
int t,n,flag=;
scanf("%d",&t);
while(t--)
{
string ans="";
cin>>n;
for(int i=;i<n;i++)
{
cin>>a[i];
}
for(int i=;i<=a[].size();i++)//遍历每一个长度
{
for(int j=;j+i<=a[].size();j++)//取每一个子串
{
flag=;
string op=a[].substr(j,i);
cal_next(op);//求每个子串的next[]
for(int k=;k<n;k++)
{
if(!kmp(a[k],op))//和每一个串进行比较,看是否有不符合的,有点话直接break
{
flag=;
break;
}
}
if(flag==)
{
if(op.size()>ans.size()) ans=op;
else if(op.size()==ans.size()) ans=min(op,ans);
}
}
}
if(ans.size()<) cout<<"no significant commonalities"<<endl;
else cout<<ans<<endl;
}
return ;
}

poj(3080)的更多相关文章

  1. POJ - 3294~Relevant Phrases of Annihilation SPOJ - PHRASES~Substrings POJ - 1226~POJ - 3450 ~ POJ - 3080 (后缀数组求解多个串的公共字串问题)

    多个字符串的相关问题 这类问题的一个常用做法是,先将所有的字符串连接起来, 然后求后缀数组 和 height 数组,再利用 height 数组进行求解. 这中间可能需要二分答案. POJ - 3294 ...

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

    POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...

  3. POJ 3080 后缀数组/KMP

    题目链接:http://poj.org/problem?id=3080 题意:给定n个DNA串,求最长公共子串.如果最长公共子串的长度小于3时输出no significant commonalitie ...

  4. poj 3080 Blue Jeans 解题报告

    题目链接:http://poj.org/problem?id=3080 该题属于字符串处理中的串模式匹配问题.题目要求我们:给出一个DNA碱基序列,输出最长的相同的碱基子序列.(保证在所有的序列中都有 ...

  5. poj 3080 Blue Jeans(水题 暴搜)

    题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...

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

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

  7. POJ 3080 Blue Jeans 后缀数组, 高度数组 难度:1

    题目 http://poj.org/problem?id=3080 题意 有m个(2<=m<=10)不包含空格的字符串,长度为60个字符,求所有字符串中都出现过的最长公共子序列,若该子序列 ...

  8. (字符串 KMP)Blue Jeans -- POJ -- 3080:

    链接: http://poj.org/problem?id=3080 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...

  9. poj 3080 Blue Jeans【字符串处理+ 亮点是:字符串函数的使用】

    题目:http://poj.org/problem?id=3080 Sample Input 3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCA ...

随机推荐

  1. MySQL查询计划输出列的含义

    "一:MySQL查询计划输出列的含义:1.id:每个被独立执行的操作的标识,表示对象被操作的顺序:id值越大,先被执行:如果相同,执行顺序从上到下.2.select_type:查询中每个se ...

  2. NSArray用法

    //类方法初始化一个数组对象 [array count] : 得到这个数组对象的长度. [array objectAtIndex index]: 传入数组的索引(index) 得到数据对象. [arr ...

  3. dubbo、dubbox、motan、thrift、grpc等RPC框架比较及选型

    概述 前段时间项目要做服务化,所以我比较了现在流行的几大RPC框架的优缺点以及使用场景,最终结合本身项目的实际情况选择了使用dubbox作为rpc基础服务框架.下面就简单介绍一下RPC框架技术选型的过 ...

  4. 基于STM32的三轴数字罗盘HMC5883L模块的测试

    最近买了个数字罗盘模块,调通后发现很不错,非常灵敏,测试的时候精度在1°以内.连续测量模式下,最快测量.输出速率可达75hz,模块每次测量完毕并将数据更新至寄存器后,其DRDY引脚便产生一个低电平脉冲 ...

  5. MySql数据库数据更新操作其高级应用

    数据更新操作有3种:向表中添加数据.修改表中的数据和删除表中的数据. 用来演示的数据表用student.course.sc三个数据表,数据表具体内容在:PHP和MySql数据库,如何获取每个分类的记录 ...

  6. Angular08 依赖注入

    1 angular应用中依赖注入的工作原理 技巧01:在模块级别进行注册时所有在应用级别的组件都可以使用,因为主模块会导入其他模块,所以在模块中注入就相当于在主模块进行注入操作:懒加载的模块除外 技巧 ...

  7. storm定时器timer源码分析-timer.clj

    storm定时器与java.util.Timer定时器比较相似.java.util.Timer定时器实际上是个线程,定时调度所拥有的TimerTasks:storm定时器也有一个线程负责调度所拥有的& ...

  8. Entity Framework Code-First(13):Configure Many-to-Many

    Configure Many-to-Many relationship: Here, we will learn how to configure Many-to-Many relationship ...

  9. grep的常用命令语法

    grep的常用命令语法 1. 双引号引用和单引号引用在g r e p命令中输入字符串参数时,最好将其用双引号括起来.例如:"m y s t r i n g".这样做有两个原因,一是 ...

  10. 服务器控件button点击时执行脚本弹出提示对话框Button2.Attributes.Add("onclick","事件")

    <HTML> <HEAD>  <title>**********资料更新</title>  <meta content="Microso ...