题目链接: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

  1. 3
  2. 2
  3. GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
  4. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  5. 3
  6. GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
  7. GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
  8. GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
  9. 3
  10. CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
  11. ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  12. AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample Output

  1. no significant commonalities
  2. AGATAC
  3. CATCATCAT

Source

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

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

看代码:

  1. #include<iostream>
  2. #include<string.h>
  3. #include<map>
  4. #include<cstdio>
  5. #include<cstring>
  6. #include<stdio.h>
  7. #include<cmath>
  8. #include<ctype.h>
  9. #include<math.h>
  10. #include<algorithm>
  11. #include<set>
  12. #include<queue>
  13. typedef long long ll;
  14. using namespace std;
  15. const ll mod=;
  16. const int maxn=+;
  17. const int maxk=5e3+;
  18. const int maxx=1e4+;
  19. const ll maxe=+;
  20. #define INF 0x3f3f3f3f3f3f
  21. #define Lson l,mid,rt<<1
  22. #define Rson mid+1,r,rt<<1|1
  23. string a[];
  24. int next[maxn];
  25. void cal_next(string s)
  26. {
  27. int len=s.length();
  28. int k=-;
  29. next[]=-;
  30. for(int i=;i<len;i++)
  31. {
  32. while(k>-&&s[k+]!=s[i])
  33. {
  34. k=next[k];
  35. }
  36. if(s[k+]==s[i]) k++;
  37. next[i]=k;
  38. }
  39. }
  40. bool kmp(string x,string y)
  41. {
  42. int k=-;
  43. for(int i=;i<x.length();i++)
  44. {
  45. while(k>-&&y[k+]!=x[i])
  46. {
  47. k=next[k];
  48. }
  49. if(x[i]==y[k+]) k++;
  50. if(k==y.length()-) return true;
  51. }
  52. return false;
  53. }
  54. int main()
  55. {
  56. int t,n,flag=;
  57. scanf("%d",&t);
  58. while(t--)
  59. {
  60. string ans="";
  61. cin>>n;
  62. for(int i=;i<n;i++)
  63. {
  64. cin>>a[i];
  65. }
  66. for(int i=;i<=a[].size();i++)//遍历每一个长度
  67. {
  68. for(int j=;j+i<=a[].size();j++)//取每一个子串
  69. {
  70. flag=;
  71. string op=a[].substr(j,i);
  72. cal_next(op);//求每个子串的next[]
  73. for(int k=;k<n;k++)
  74. {
  75. if(!kmp(a[k],op))//和每一个串进行比较,看是否有不符合的,有点话直接break
  76. {
  77. flag=;
  78. break;
  79. }
  80. }
  81. if(flag==)
  82. {
  83. if(op.size()>ans.size()) ans=op;
  84. else if(op.size()==ans.size()) ans=min(op,ans);
  85. }
  86. }
  87. }
  88. if(ans.size()<) cout<<"no significant commonalities"<<endl;
  89. else cout<<ans<<endl;
  90. }
  91. return ;
  92. }

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. BZOJ1707:[Usaco2007 Nov]tanning分配防晒霜

    我对贪心的理解:https://www.cnblogs.com/AKMer/p/9776293.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...

  2. 不重启linuxVMWare虚拟机添加虚拟磁盘

    Vsphere Client找到要添加磁盘的虚机,如图所示   点击虚机右键,在出现的下列列表中选择“编辑设置”如图   在打开的虚拟机属性中,在”硬件对话框点击“添加"按钮,如图   在添 ...

  3. SqlServer2005的备份和还原(不同服务器)

    1 备份数据库NorthSJ 进入服务器,进入SqlServer2005,选择数据库NorthSJ进行备份

  4. Android中EditTex焦点设置和弹不弹出输入法的问题(转)

    今天编程碰到了一个问题:有一款平板,打开一个有EditText的Activity会默认弹出输入法.为了解决这个问题就深入研究了下android中焦点Focus和弹出输入法的问题.在网上看了些例子都不够 ...

  5. 用python做的windows和linx文件夹同步。解决自动同步、加快传输大量小文件的速度、更丰富的文件上传过滤设置。

    现在工具不好用,用的pycharm自动同步,但对于git拉下来的新文件不能自动上传到linux,只有自己编辑过或者手动ctrl + s的文件才会自动同步.导致为了不遗漏文件,经常需要全量上传,速度非常 ...

  6. 用C语言实现一个公用库函数void * memmove(void *dest,const void *src,size_t n)

    用C语言实现一个公用库函数void * memmove(void *dest,const void *src,size_t n). 该函数的功能是拷贝src所指的内存内容前n个字节到dest所指的地址 ...

  7. tar压缩解压缩

    Linux下的tar压缩解压缩命令详解tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以 ...

  8. mvn jetty:run 出现PermGen space outofmemeryerror

    使用mvn jetty:run跑别人的项目时出现了PermGen space outofmemeryerror异常 http://stackoverflow.com/questions/1451648 ...

  9. Python短小精悍的Orator查询构造器

    查询构造器 介绍 这个数据库查询构造器,提供便利的接口可以创建和执行查询操作,可以在大多数数据库中使用. 查询select操作 查询表中所有的数据. users = db.table('users') ...

  10. 开源库ActiveAndroid + gson使用

    ActiceAndroid的简介 ActiveAndroid是一个活跃的记录风格的ORM(对象关系映射)库.ActiveAndroid可以让您保存和检索的SQLite数据库记录而没有写一个SQL语句. ...