kmp(暴力匹配)
http://poj.org/problem?id=3080
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 23415 | Accepted: 10349 |
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
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
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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
#define INF 10000000
using namespace std;
char a[][] , b[], str[];
int next[];
int ans = ; void getnext(char *b , int len , int *next)
{
next[] = - ;
int j = , k = -;
while(j < len)
{
if(k == - || b[j] == b[k])
{
k++;
j++;
next[j] = k ;
}
else
{
k = next[k];
}
}
} int main()
{
int n ;
scanf("%d" , &n);
while(n--)
{
int m ;
memset(b , '\0' , sizeof(b));
memset(str , '\0' , sizeof(str));
scanf("%d" , &m);
for(int i = ; i < m ; i++)
{
scanf("%s" , a[i]);
}
int x = , flag = , ans = ;
while(x <= )
{
for(int i = ; i <= - x ; i ++)
{
int jj = i ;
for(int j = ; j < x ; j++)
{
b[j] = a[][jj++];
}
getnext(b , x , next);
for(int j = ; j < m ; j++)
{
int ii = , k = ;
while(ii < && k < x)
{
if(k == - || a[j][ii] == b[k])
{
k++;
ii++;
}
else
{
k = next[k];
}
}
if(k == x)
{
flag = ;
}
else
{
flag = ;
break ;
}
}
if(flag == )
{
if(ans < x)
{
ans = x ;
strcpy(str , b);
}
else if(ans == x) //如果长度相等,输出字典序小的序列,我还以为是第一出现的序列,害我wa了这么久
{
if(strcmp(b , str) <)
{
strcpy(str , b);
}
}
}
if(i == - x)
x++ ;
}
}
if(ans == )
printf("no significant commonalities\n");
else
{
printf("%s\n" , str);
} } return ;
}
kmp(暴力匹配)的更多相关文章
- 字符串查找算法总结(暴力匹配、KMP 算法、Boyer-Moore 算法和 Sunday 算法)
字符串匹配是字符串的一种基本操作:给定一个长度为 M 的文本和一个长度为 N 的模式串,在文本中找到一个和该模式相符的子字符串,并返回该字字符串在文本中的位置. KMP 算法,全称是 Knuth-Mo ...
- HDU 5510 Bazinga 暴力匹配加剪枝
Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...
- python opencv3 基于ORB的特征检测和 BF暴力匹配 knn匹配 flann匹配
git:https://github.com/linyi0604/Computer-Vision bf暴力匹配: # coding:utf-8 import cv2 """ ...
- HDU4300-Clairewd’s message(KMP前缀匹配后缀)
Clairewd's message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 字符串匹配算法--暴力匹配(Brute-Force-Match)C语言实现
一.前言 暴力匹配(Brute-Force-Match)是字符串匹配算法里最基础的算法,虽然效率比较低,但胜在方便理解,在小规模数据或对时间无严格要求的情况下可以考虑. 二.代码 #include & ...
- 从暴力匹配到KMP算法
前言 现在有两个字符串:\(s1\)和\(s2\),现在要你输出\(s2\)在\(s1\)当中每一次出现的位置,你会怎么做? 暴力匹配算法 基本思路 用两个指针分别指向当前匹配到的位置,并对当前状态进 ...
- poj-3080(kmp+暴力枚举)
题意:给你多个字符串,问你这几个字符串的最长公共子串是哪个,如果有多个,输出字典序最大的那个,如果最长的公共子串长度小于3,输出一个奇怪的东西: 解题思路:首先看数据,数据不大,开始简单快乐的暴力之路 ...
- POJ-3080-Blue jeans(KMP, 暴力)
链接: https://vjudge.net/problem/POJ-3080#author=alexandleo 题意: 给你一些字符串,让你找出最长的公共子串. 思路: 暴力枚举第一个串的子串,挨 ...
- 【poj 3080】Blue Jeans(字符串--KMP+暴力枚举+剪枝)
题意:求n个串的字典序最小的最长公共子串. 解法:枚举第一个串的子串,与剩下的n-1个串KMP匹配,判断是否有这样的公共子串.从大长度开始枚举,找到了就break挺快的.而且KMP的作用就是匹配子串, ...
随机推荐
- wepy的第一个demo
一.在node中安装相应的模块文件,查看文档 二.案例 json部分 { "usingComponents": { "van-button": ".. ...
- MySQL系列理论知识
内容: 1.视图 2.触发器 3.事务 4.存储过程 5.内置函数 6.流程控制 7.索引与慢查询优化 —————————————————————————————— 1.视图: 1.视图是什么: 视图 ...
- Centos安装ifstat统计网络流量
原文地址: http://www.winvps.org/post/504.html 下载 ifstat , http://gael.roualland.free.fr/ifstat/ifstat-1 ...
- 【错误】Publishing to Tomcat'has encountered a problem
tomcat 启动工程时候出现 Publishing to Tomcat'has encountered a problem错误 解决方案 之后重启tomcat 就可以正常启动了
- 搭建Keepalived+LNMP架构web动态博客 实现高可用与负载均衡
环境准备: 192.168.193.80 node1 192.168.193.81 node2 关闭防火墙 [root@node1 ~]# systemctl stop firewalld #两台都 ...
- 模块(os模块)
一.模块 一个python文件就是一个模块. 模块可分为: 1.标准模块:python自带的模块是标准模块,可以直接import进行使用的. eg:import json.import random. ...
- Python not and or
刷题时候,有道题目的答案是 return(num and (num % 9 or 9)) 看的有点懵逼,看来解释如下: 1.首先,’and’.’or’.’not’的优先级是not>and> ...
- mycat权威指南阅读笔记--序言1
前言 mycat官方地址http://www.mycat.io/,mycat是关系数据库的中间件,也就是说它可以把后端的多个数据库,抽象成一个关系数据库. mycat能干啥 官方文档介绍,主要是用来做 ...
- Oracle Internals Notes Redo Write Triggers
There are four conditions that cause LGWR to perform a redo write. When LGWR is idle, it sleeps on a ...
- 第三代DRDS分布式SQL引擎全新发布
DRDS (阿里云分布式关系型数据库服务,https://www.aliyun.com/product/drds)于 4 月 30 号发布了 5.3 版本,这是一个年度大更新.主要带来了以下特性: 性 ...