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


题目:

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

题意:给出几个长度为60的字符串 求他们的最长公共子序列

思路:因为长度很短 可以用暴力来做把第一个字符串当作T串 拆分出所有的子串 然后和下面所有的字符串进行匹配

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>using namespace std;
const int maxn=70;
int t,n,nx[maxn];
string s[maxn];void getnx(string tmp,int len){int j=0,k=-1;nx[0]=-1;while(j<len){if(k==-1 || tmp[j]==tmp[k]) nx[++j]=++k;k=nx[k];}
}int KMP_Index(string S,string T){int i=0,j=0;int slen=S.size(),tlen=T.size();while(i<slen && j<tlen){if(j==-1 || S[i]==T[j]) i++,j++;else j=nx[j];}if(j==tlen) return 1;else return 0;
}int main(){//   freopen("1.in","r",stdin);scanf("%d",&t);while(t--){scanf("%d",&n);for(int i=1;i<=n;i++) cin>>s[i];string ans=" ";for(int i=1;i<=s[1].size();i++){for(int j=0;j<=s[1].size()-i;j++){memset(nx,0,sizeof(nx));string tmp=s[1].substr(j,i);getnx(tmp,tmp.size());int flag=0;for(int k=2;k<=n;k++){if(KMP_Index(s[k],tmp)==0) flag=1;}if(flag==0){if(ans.size()<tmp.size()) ans=tmp;else if(ans.size()==tmp.size()) ans=min(ans,tmp);}}}if(ans.size()<3) printf("no significant commonalities\n");else cout<<ans<<endl;}return 0;
}

转载于:https://www.cnblogs.com/whdsunny/p/10853252.html

POJ 3080 Blue Jeans (KMP)相关推荐

  1. POJ - 3080 Blue Jeans(暴力+KMP)

    题目链接:点击查看 题目大意:给出n组长度为60的字符串,问这n组中最长的公共连续子串是什么,若有多个不同的最长公共子串,输出字典序最小的那个 题目分析:一开始看到这个题目的时候我是没有想到暴力的.. ...

  2. poj 3080 Blue Jeans

    #include <iostream> //KMP+枚举#include<string>using namespace std;#define len 60char str[1 ...

  3. POJ 3080 - Blue Jeans

    题意: 求所有串的最长公共子串,若有多个输出字典序最小的 分析: 对第一个串的每一个后缀分别与剩下的所有串进行匹配,求得公共子串 对每一个公共子串,记录下最大值即可. 1 #include <i ...

  4. POJ 3080 Blue Jeans (多个字符串的最长公共序列,暴力比较)

    题意:给出m个字符串,找出其中的最长公共子序列,如果相同长度的有多个,输出按字母排序中的第一个. 思路:数据小,因此枚举第一个字符串的所有子字符串s,再一个个比较,是否为其它字符串的字串.判断是否为字 ...

  5. POJ 3080 Blue Jeans (后缀数组)

    题目大意: 求出这些DNA序列中的最长且字典序最小的公共子串. 思路分析: 二分长度的答案,去height中扫描这个长度是否满足,一旦满足就立即输出.这样就能够保证字典序最小了. #include & ...

  6. 暴力枚举(字符串匹配)-Blue Jeans POJ - 3080

    暴力枚举(字符串匹配)-Blue Jeans POJ - 3080 题目: Genographic项目是IBM与国家地理学会之间的研究合作伙伴关系,该合作伙伴正在分析数十万贡献者的DNA,以绘制地球的 ...

  7. poj 1961 Period(KMP)

    题目链接:http://poj.org/problem?id=1961 题目大意:给出一个长为n的字符串,求到每个字符之前有多少个字串循环次数大于1 方法: kmp ,求出这个字符串的next数组.在 ...

  8. POJ:3461-Oulipo(KMP模板题)

    原题传送:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Description The F ...

  9. POJ 3080 多个串最长公共子序列

    求多个串最长公共子序列,字典序最小输出.枚举剪枝+kmp.比较简单,我用find直接查找16ms #include<iostream> #include<string> #in ...

最新文章

  1. 工具类的方法怎么引用_Hutool中那些常用的工具类和方法
  2. Spring MVC - URL路径映射
  3. 2017你该买房,还是卖房?
  4. redis使用sysc超时_基于redis的分布式锁实现
  5. erp系统方案书_一次ERP选型实施失败的血泪教训!
  6. 微信小程序定义全局变量_微信小程序第二天学习内容分享
  7. ecshop备份数据 ecshop转移数据 ecshop更换主机
  8. 剑指offer——面试题35:第一个只出现一次的字符
  9. 二十五、JAVA多线程(一、理论知识)
  10. java pdf查看_Java检查PDF文件是否损坏
  11. java做度量衡换算器_简易单位换算器_度量衡单位转换
  12. keras入门 ---在小数据集上训练神经网络
  13. 使用python+ffmpeg批量将视频水平翻转
  14. ORACLE 19C 单实例数据库安装
  15. 小米6MIUI稳定版安装谷歌相机
  16. 万台服务器一人挑的奥秘
  17. 《邱岳的产品实战》学习笔记:第9周
  18. sql语句语法数据定义语句alter table
  19. Unity新手引导(圆形指引、矩形指引)
  20. 【异常记录(九)】 System.Threading.ThreadAbortException: 正在中止线程

热门文章

  1. idea package放在什么位置_NBA现役球员大排名,你会把20岁的东契奇放在什么位置?...
  2. linux启动中继服务器失败,Tor中继服务器在Linux里安装配置的流程
  3. html怎么添加遮罩层,如何在浏览器窗口上添加一个遮罩层
  4. linux进程优雅退出,Golang信号处理及如何实现进程的优雅退出详解
  5. drop out, learning rate in nn
  6. java无参构造有参构造_Java ——补充:构造方法 super()与构造方法 无参 有参构造方法 this()与构造方法...
  7. java实现英文文件单词搜索系统_java对于目录下文件的单词查找操作代码实现
  8. recvfrom函数 非阻塞_那些年让你迷惑的阻塞、非阻塞、异步、同步
  9. Linux IO控制命令生成
  10. 删除“ie8左侧收藏夹图标(黄星星)”及“恢复”的方法