【POJ 3080 Blue Jeans】
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 19026
Accepted: 8466
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
South Central USA 2006
【翻译】给出一些字符串,找出最长公共连续子串,并且输出字典序最小的那个,如果最优解长度小于3,输出no significant commonalities。
题解:
①串长度不超过60,因此考虑暴力枚举第一个串的子串。
②在上文的情况下,将当前枚举的子串与剩下的字符串匹配就可以了。
③为了找到字典序最小,这里直接用STL-string操作比较方便。
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define ro(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=100;string ans,tmp;
int t,n,m,C,len,ok,OK,f[N];
char s[15][N],T[N],P[N];
bool KMP_Matching()
{
int j;f[0]=f[1]=0;
go(i,1,m-1){j=f[i];while(j&&P[i]!=P[j])j=f[j];f[i+1]=P[i]==P[j]?j+1:0;}j=0;
go(i,0,n-1){while(j&&P[j]!=T[i])j=f[j];if((j+=P[j]==T[i])==m)return 1;}
return 0;
}
int main()
{
scanf("%d",&C);
while(C--&&scanf("%d",&t))
{
go(i,1,t)scanf("%s",s[i]);
ans="";len=strlen(s[1]);
ro(k,len,1)
{
OK=0;
go(i,0,len-k)
{
ok=1;m=0;
go(u,i,i+k-1)P[m++]=s[1][u];
go(j,2,t)
{
n=strlen(s[j]);
go(u,0,n-1)T[u]=s[j][u];
if(!(ok&=KMP_Matching()))break;
}
if(ok)
{
OK=1;tmp.clear();go(u,0,m-1)tmp+=P[u];
if(ans==""||tmp<ans)ans=tmp;
}
}
if(OK)
{
if(ans.length()>=3)cout<<ans<<endl;
else puts("no significant commonalities");goto Judy;
}
}
puts("no significant commonalities");Judy:;
}
return 0;
}//Paul_Guderian
谁能告诉我那奔腾的迷惘与骄傲,
是否就是我心底永隔一世的河流。——————汪峰《河流》
【POJ 3080 Blue Jeans】的更多相关文章
- POJ 3080 Blue Jeans (求最长公共字符串)
POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...
- POJ 3080 Blue Jeans(Java暴力)
Blue Jeans [题目链接]Blue Jeans [题目类型]Java暴力 &题意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 规定: 1. 最长公共 ...
- 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: ...
- POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20966 Accepted: 9279 Descr ...
- poj 3080 Blue Jeans【字符串处理+ 亮点是:字符串函数的使用】
题目:http://poj.org/problem?id=3080 Sample Input 3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCA ...
- POJ - 3080 Blue Jeans 【KMP+暴力】(最大公共字串)
<题目链接> 题目大意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 限制条件: 1. 最长公共串长度小于3输出 no significant co ...
- POJ 3080 Blue Jeans(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=3080 [题目大意] 求k个串的最长公共子串,如果存在多个则输出字典序最小,如果长度小于3则判断查找失败. [题解] 将所有字符串通 ...
- poj 3080 Blue Jeans 解题报告
题目链接:http://poj.org/problem?id=3080 该题属于字符串处理中的串模式匹配问题.题目要求我们:给出一个DNA碱基序列,输出最长的相同的碱基子序列.(保证在所有的序列中都有 ...
随机推荐
- spring-传统AOP
Spring传统AOP AOP的增强类型 AOP联盟定义了Advice(org.aopalliance.aop.Interface.Advice) 五类(目标类方法的连接点): 1. 前置通知(or ...
- spring-bean(注解方式-管理及依赖注入)
Bean管理(注解方式) 1.添加注解的依赖包:Spring-aop.jar 2.配置spring的XML文件的引入(查官方源码) 3.开启注解的扫描 <context:component-sc ...
- Shell学习——终端打印
1.echo1.1.默认情况下,echo在每次调用后会添加一个换行符1.2.待打印的内容,可以用单引号.双引号或者直接打印,不同的方式,有各自的限制1.2.1.使用不带引号的echo时,没法打印分好( ...
- (转)零基础学习 Hadoop 该如何下手?
推荐一些Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, Zookeeper, Avro, Amb ...
- 理解volatile与synchronized
用 volatile 修饰的变量可以保证线程的"可见性",也就是,任何线程修改了这个 volatile 修饰的值都会通知其他线程来主缓存中重新读取值. 下面通过例子加以说明: pu ...
- web worker,SSE,WebSocket,AJAX 与后端交互的方式
一 web worker web worker 是运行在后台的 JavaScript,独立于其他脚本,不会影响页面的性能.您可以继续做任何愿意做的事情:点击.选取内容等等,而此时 web worker ...
- 裸机——RTC
1. 首先晓得RTC的基本知识 RTC被划分到timer,但RTC是面向时间点的. 如果按照定时器的思路去思考,那么应该考虑 时间周期 和 计数值. RTC 不是面向时间点的,所以略有不同, 时间周期 ...
- 一些 Markdown 语法
参考于: https://www.jianshu.com/p/b03a8d7b1719 [先挖个坑,来日再填]
- cf978E Bus Video System
The busses in Berland are equipped with a video surveillance system. The system records information ...
- 用swoole实现nginx日志解析
1.原技术路线解析 在nging配置中将日志信息交给syslog处理,rsyslog配置中将数据传递给了514端口解析,然后将解析好的数据传入elasticsearch中. nginx配置 serve ...