poj3080 Blue Jeans【KMP】【暴力】
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions:21746 | Accepted: 9653 |
Description
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
- 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
Sample Input
3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities
AGATAC
CATCATCAT
Source
题意:
给定$m$个场长度为$60$的字符串,问他们的最长公共子串。
思路:
因为$m$和长度都很小,所以可以暴力枚举一个串的所有子串,$KMP$进行匹配。
等之后【后缀数组】刷熟练一点了再用后缀数组做一次。奇怪的是hdu2328明明是一样的题意,怎么就总是WA
#include<iostream>
//#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<climits>
#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define pi 3.1415926535
#define inf 0x3f3f3f3f const int maxn = ;
int n, m;
char str[][maxn];
int nxt[maxn]; void getnxt(char *s)
{
int len = strlen(s);
nxt[] = -;
int k = -;
int j = ;
while(j < len){
if(k == - || s[j] == s[k]){
++k;++j;
if(s[j] != s[k]){
nxt[j] = k;
}
else{
nxt[j] = nxt[k];
}
}
else{
k = nxt[k];
}
}
} bool kmp(char *s, char *t)
{
getnxt(s);
int slen = strlen(s), tlen = strlen(t);
int i = , j = ;
while(i < slen && j < tlen){
if(j == - || s[i] == t[j]){
j++;
i++;
}
else{
j = nxt[j];
}
}
if(j == tlen){
return true;
}
else{
return false;
}
} int main()
{
scanf("%d", &n);
while(n--){
scanf("%d", &m);
for(int i = ; i < m; i++){
scanf("%s", str[i]);
} char ans[maxn];
int anslen = -;
for(int i = ; i < ; i++){
for(int j = i; j < ; j++){
char t[maxn];
memcpy(t, str[] + i, j - i + );
t[j - i + ] = '\0';
bool flag = true;
for(int k = ; k < m; k++){
if(!kmp(str[k], t)){
flag = false;
break;
}
}
if(flag){
if(j - i + > anslen){
anslen = j - i + ;
strcpy(ans, t);
}
else if(j - i + == anslen){
if(strcmp(ans, t) > ){
strcpy(ans, t);
}
}
}
else{
break;
}
}
} if(anslen < ){
printf("no significant commonalities\n");
}
else{
//cout<<ans<<endl;
printf("%s\n", ans);
} }
return ;
}
poj3080 Blue Jeans【KMP】【暴力】的更多相关文章
- POJ3080 - Blue Jeans(KMP+二分)
题目大意 求N个字符串的最长公共字串 题解 和POJ1226做法一样...注意是字典序最小的...WA了一次 代码: #include <iostream> #include <cs ...
- 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 ...
- POJ 3080 Blue Jeans(Java暴力)
Blue Jeans [题目链接]Blue Jeans [题目类型]Java暴力 &题意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 规定: 1. 最长公共 ...
- kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans
The Genographic Project is a research partnership between IBM and The National Geographic Society th ...
- poj3080 Blue Jeans(暴枚+kmp)
Description The Genographic Project is a research partnership between IBM and The National Geographi ...
- POJ3080 Blue Jeans 题解 KMP算法
题目链接:http://poj.org/problem?id=3080 题目大意:给你N个长度为60的字符串(N<=10),求他们的最长公共子串(长度>=3). 题目分析:KMP字符串匹配 ...
- POJ-3080-Blue jeans(KMP, 暴力)
链接: https://vjudge.net/problem/POJ-3080#author=alexandleo 题意: 给你一些字符串,让你找出最长的公共子串. 思路: 暴力枚举第一个串的子串,挨 ...
- Blue Jeans---poj3080(kmp+暴力求子串)
题目链接:http://poj.org/problem?id=3080 题意就是求n个长度为60的串中求最长公共子序列(长度>=3):如果有多个输出字典序最小的: 我们可以暴力求出第一个串的所有 ...
随机推荐
- Quality of Service 0, 1 & 2
来自:http://www.hivemq.com/blog/mqtt-essentials-part-6-mqtt-quality-of-service-levels Quality of Servi ...
- Class.forName和ClassLoader.loadClass的区别
Class的装载分了三个阶段,loading,linking和initializing,分别定义在The Java Language Specification的12.2,12.3和12.4. Cla ...
- testng.xml 配置大全
1.TestNG的运行方式如下: 1 With a testng.xml file 直接run as test suite 2 With ant 使用ant 3 From the command li ...
- 你可能不知道的51个Linux经典命令
1.删除0字节文件 代码如下: find -type f -size 0 -exec rm -rf {} \; 2.查看进程按内存从大到小排列 代码如下: ps -e -o "%C : %p ...
- [转] Dangers of using dlsym() with RTLD_NEXT
There are times when you want to wrap a library function in order to provide some additional functio ...
- +: indexed part-select
That syntax is called an indexed part-select. The first term is the bit offset and the second term i ...
- Socket网络编程--网络爬虫(4)
上一小节我们已经实现了获取博客园最近博客的200页里面的用户名,并保存在一个map中.一开始是想通过这个用户名然后构造一个博客地址.然后在这个地址中查找心得用户名,但是后来发现这个的效率不是很高,虽然 ...
- PaaS 应用引擎
这里主要是梳理一下应用引擎(XXXX App Engine),它一般被归类到PaaS领域.应用引擎即提供了各种编程语言开发的应用所需的一整套运行环境:它开箱即用,你只需部署应用的代码即可,无需前期的环 ...
- 教你一招:Microsoft Office Word已停止工作
1/按组合键WIN+R打开运行对话框 2/在打开框中键入%USERPROFILE%\AppData\Roaming\Microsoft\Templates,单击“确定”按钮 3/在打开的窗口鼠标右键删 ...
- 瀑布 敏捷 精益 devops
敏捷: 分工角色 大项目分小项目 每个节点时间设置里程碑 Scrum实施的核心可以概括为“化繁为简”,从几个维度解释下: 团队角色的定义,将团队人员定义为三个角色,Scrum Master(主 ...