【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碱基序列,输出最长的相同的碱基子序列.(保证在所有的序列中都有 ...
随机推荐
- 【ODT】cf896C - Willem, Chtholly and Seniorious
仿佛没用过std::set Seniorious has n pieces of talisman. Willem puts them in a line, the i-th of which is ...
- ABAP Table Control
SAP中,Table Control是在Screen中用的最广泛的控件之一了,可以实现对多行数据的编辑. 简单来说,Table Control是一组屏幕元素在Screen上的重复出现,这就是它与普通 ...
- Linux实战教学笔记05:远程SSH连接服务与基本排错
第1章 远程连接LInux系统管理 1.1 为什么要远程连接Linux系统 在实际的工作场景中,虚拟机界面或物理服务器本地的窗口都是很少能够接触到的,因为服务器装完系统后,都要拉到IDC机房托管,如果 ...
- DFS:C 小Y的难题(1)
解题心得: 1.在明确使用DFS之后一定要找到递归函数的出口.方向,以及递归的点(在某个情况下开始递归)(void 也可以return,但是没有返回值).递归时也要有递归的方向,最后都能够达到递归的出 ...
- Java 泛型 二
一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...
- ACE handle_timeout 事件重入
当多线程运行反应器事件时, 注意handle_timeout会重入,单独线程不存在下列问题! 1. 一个timer事件 // test_ace_timer.cpp : Defines the entr ...
- SLB 7层负载均衡“HUNG”问题追查
最近接到博客园的反馈,SLB 7层负载均衡的实例会不定期出现流量突跌的情况,突跌持续10s左右:同时,SLB自身监控也观察到了相同的现象: 针对该问题,我们进行了持续追查,最终定位到是nginx配置的 ...
- 有关ViewPager的使用及解决Android下ViewPager和PagerAdapter中调用notifyDataSetChanged失效的问题
ViewPager是android-support-v4.jar包中的一个系统控件,继承自ViewGroup,专门用以实现左右滑动切换View的效果,使用时需要首先在Project->prope ...
- script通过script标签跨域加载数据
/********************************************************** 说明:跨域请求数据Javascript组件 ------------------ ...
- chrome flash插件改为自动运行
1.情景展示 国内网页视频播放大部分用的都是flash插件,每次都要将默认改为允许,才能正常播放 能不能让flash插件在所有的网站上都能自动运行呢? 2.解决方案 第一步:打开fla ...