题目链接: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. BZOJ2212:[POI2011]Tree Rotation

    浅谈线段树合并:https://www.cnblogs.com/AKMer/p/10251001.html 题目传送门:https://lydsy.com/JudgeOnline/problem.ph ...

  2. 简单易懂dubbo入门实例

    一.创建Maven多模块项目 项目结构如下 模块介绍: dubbo-api            ----API接口 dubbo-consumer ----消费者 dubbo-provider     ...

  3. 【转】 Pro Android学习笔记(五一):ActionBar(4):标准和Tab模式

    之前,我们学习的Action Bar是标准模式,Tab模式的如下图所示. 对于Tab,我们在Android学习笔记(二二): 多页显示-Tag的使用中学习过,但Action Bar的tab更适合fra ...

  4. 杂项:grunt-tmod

    ylbtech-杂项:grunt-tmod 前端模板预编译工具 tmodjs 的grunt自动化插件. 1.返回顶部 1. grunt-tmod 前端模板预编译工具 tmodjs 的grunt自动化插 ...

  5. Guice 学习

    Guice: 是一个轻量级的DI框架. 不需要繁琐的配置,只需要定义一个Module来表述接口和实现类,以及父类和子类之间的关联关系的绑定,如下是一个例子. http://blog.csdn.net/ ...

  6. uboot 命令使用教程(uboot参数设置)

    1. Printenv 打印环境变量. uboot> printenv baudrate=115200 ipaddr=192.168.0.111 ethaddr=32:34:46:78:9A:D ...

  7. [51nod1113]矩阵快速幂

    解题关键:模板题,方便以后熟悉 #include<bits/stdc++.h> using namespace std; typedef long long ll; struct mat{ ...

  8. MySql获取记录的名次

    在oracle中有rownum之类的东西表示记录的名次,那么在MySql中怎么获取名次呢? as rank ) B 获取的rank就是名次了 user_id rank 134762    122139 ...

  9. LeetCode第14题:最长公共前缀

    题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...

  10. Ping++中的AlipaySDK和AlicloudUTDID冲突解决方案

    今天维护一个老项目发现阿里框架冲突 问题截图: 解决方案: 去阿里文档中心 https://docs.open.alipay.com/54/104509 重新下载没有UTDID冲突的库 下载SDK解压 ...