Blue Jeans[poj3080]题解
题目
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
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output 1
- no significant commonalities
AGATAC
CATCATCAT
思路
- 暴力循环子串长度
- \(KMP\)判断每个串中是否有该子串
- 详细见\(code\)
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
const int Max=1001;
int nx[Max];
string S[Max],P;
int T,M,N[Max];
void makenx(int M)
{
memset(nx,0,sizeof(nx));
int i=0,j=-1;
nx[i]=j;
while(i<M)
{
if(j==-1||P[i]==P[j]) i++,j++,nx[i]=j;
else j=nx[j];
}
}
int Kmp(int k,int M)
{
int i=0,j=0;
while((i<N[k])&&(j<M))
{
if(j==-1||S[k][i]==P[j]) i++,j++;
else j=nx[j];
}
if(j>=M) return true;
else return false;
}
int main()
{
int n,m;bool fl;
scanf("%d",&T);
string ans;int lans;
while(T--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++) cin>>S[i],N[i]=S[i].size();
ans="";lans=0;
m=S[1].size();
for(int i=0; i<m; i++)//循环子串起点
{
for(int j=3; j<=m-i; j++)//循环子串长度
{
fl=true;
P=S[1].substr(i,j);
M=j;makenx(M);
for(int k=2; k<=n; k++)
if(!Kmp(k,M))//Kmp判断
{fl=false;break;}
if(fl)
{
if(M>lans) ans=P,lans=M;
else if(M==lans&&ans>P) ans=P,lans=M;//字典序
}
}
}
if(ans=="") cout<<"no significant commonalities"<<endl;
else cout<<ans<<endl;
}
return 0;
}
Blue Jeans[poj3080]题解的更多相关文章
- POJ3080 Blue Jeans —— 暴力枚举 + KMP / strstr()
题目链接:https://vjudge.net/problem/POJ-3080 Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ3080——Blue Jeans(暴力+字符串匹配)
Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National ...
- poj3080 Blue Jeans【KMP】【暴力】
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions:21746 Accepted: 9653 Descri ...
- POJ 3080 Blue Jeans(Java暴力)
Blue Jeans [题目链接]Blue Jeans [题目类型]Java暴力 &题意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 规定: 1. 最长公共 ...
- POJ Blue Jeans [枚举+KMP]
传送门 F - Blue Jeans Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- poj 3080 Blue Jeans
点击打开链接 Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10243 Accepted: 434 ...
- POJ 3080 Blue Jeans (字符串处理暴力枚举)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21078 Accepted: ...
- (字符串 KMP)Blue Jeans -- POJ -- 3080:
链接: http://poj.org/problem?id=3080 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...
- POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20966 Accepted: 9279 Descr ...
随机推荐
- PgSQL备份
SQL转储. 这里我们用到的工具是pg_dump和pg_dumpall. 这种方式可以在数据库正在使用的时候进行完整一致的备份,并不阻塞其它用户对数据库的访问.它会产生一个脚本文件,里面包含备份开始时 ...
- 【转载】Linux设备树(Device Tree)机制
转:Linux设备树(Device Tree)机制 目录 1. 设备树(Device Tree)基本概念及作用2. 设备树的组成和使用 2.1. DTS和DTSI 2.2. DTC 2.3. DT ...
- Hadoop搭建record下
前言 先说一下当前环境:Ubuntu18.04 jdk1.8 Hadoop选用-2.6.0-cdh5.15.1 用户名:supershuai-VirtualBox Hadoop的下载地址:http:/ ...
- 什么是AOP面向切面编程思想
一.什么是AOP? 1.AOP不是一种语言,是一种编程范式 常见的编程范式: 面向过程.面向对象.函数式编程.事件驱动编程等 2.AOP可以解决特定问题,不能解决所有问题. 3.是面向对象的补充,不是 ...
- Git命令行操作方法
1.GitHub上创建一个Repositories(仓库),并复制仓库地址
- Typora自动编号设置
找到C:\Users\用户名\AppData\Roaming\Typora\themes\路径下的base.user.css,用文本编辑器打开(若不存在,则自己创建一个),将一下内容复制进去,然后重启 ...
- OHEM论文笔记
目录 引言 Fast R-CNN设计思路 一.动机 二.现有方案hard negative mining 及其窘境 hard negative mining实现 窘境 设计思路 OHEM步骤: 反向传 ...
- 初识OpenSSH--1
note:保护你的报文(communique)安全 ! 最安全!!! 简介:OpenSSH使用SSH协议进行远程登录的主要连接工具.它对传输数据进行加密,以消除窃听,连接劫持和其他攻击.此外,Ope ...
- open xml 导出excel遇到的问题
我有一个需求:使用ajax 下载excel文件 结果:失败 原因:下载文件是通过浏览器解析二级制流下载的,而ajax返回的是文本 方法: 1.如果你下载不需要参数,那直接写个a标签链接到地址就行 2. ...
- Java 8 Stream Api 中的 peek 操作
1. 前言 我在Java8 Stream API 详细使用指南[1] 中讲述了 [Java 8 Stream API]( "Java 8 Stream API") 中 map 操作 ...