http://poj.org/problem?id=3080

Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23415   Accepted: 10349

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

题意:给你几段长为60的碱基序列,让你找出最长相同的碱基序列,如果有多个最长相同序列,则输出字典序最小的碱基序列
思路:kmp暴力或strstr函数暴力
#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(暴力匹配)的更多相关文章

  1. 字符串查找算法总结(暴力匹配、KMP 算法、Boyer-Moore 算法和 Sunday 算法)

    字符串匹配是字符串的一种基本操作:给定一个长度为 M 的文本和一个长度为 N 的模式串,在文本中找到一个和该模式相符的子字符串,并返回该字字符串在文本中的位置. KMP 算法,全称是 Knuth-Mo ...

  2. HDU 5510 Bazinga 暴力匹配加剪枝

    Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...

  3. python opencv3 基于ORB的特征检测和 BF暴力匹配 knn匹配 flann匹配

    git:https://github.com/linyi0604/Computer-Vision bf暴力匹配: # coding:utf-8 import cv2 """ ...

  4. HDU4300-Clairewd’s message(KMP前缀匹配后缀)

    Clairewd's message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. 字符串匹配算法--暴力匹配(Brute-Force-Match)C语言实现

    一.前言 暴力匹配(Brute-Force-Match)是字符串匹配算法里最基础的算法,虽然效率比较低,但胜在方便理解,在小规模数据或对时间无严格要求的情况下可以考虑. 二.代码 #include & ...

  6. 从暴力匹配到KMP算法

    前言 现在有两个字符串:\(s1\)和\(s2\),现在要你输出\(s2\)在\(s1\)当中每一次出现的位置,你会怎么做? 暴力匹配算法 基本思路 用两个指针分别指向当前匹配到的位置,并对当前状态进 ...

  7. poj-3080(kmp+暴力枚举)

    题意:给你多个字符串,问你这几个字符串的最长公共子串是哪个,如果有多个,输出字典序最大的那个,如果最长的公共子串长度小于3,输出一个奇怪的东西: 解题思路:首先看数据,数据不大,开始简单快乐的暴力之路 ...

  8. POJ-3080-Blue jeans(KMP, 暴力)

    链接: https://vjudge.net/problem/POJ-3080#author=alexandleo 题意: 给你一些字符串,让你找出最长的公共子串. 思路: 暴力枚举第一个串的子串,挨 ...

  9. 【poj 3080】Blue Jeans(字符串--KMP+暴力枚举+剪枝)

    题意:求n个串的字典序最小的最长公共子串. 解法:枚举第一个串的子串,与剩下的n-1个串KMP匹配,判断是否有这样的公共子串.从大长度开始枚举,找到了就break挺快的.而且KMP的作用就是匹配子串, ...

随机推荐

  1. Array数组的使用

    public class ArrayDemo { public static void main(String[] args) { int[] arr = {13,44,55,667,67,78}; ...

  2. [好好学习]在VMware中安装Oracle Enterprise Linux (v5.7) - (5/5)

  3. [好好学习]在VMware中安装Oracle Enterprise Linux (v5.7) - (4/5)

  4. 【转载】JDK8 特性 stream(),lambda表达式,

    Stream()表达式 虽然大部分情况下stream是容器调用Collection.stream()方法得到的,但stream和collections有以下不同: 无存储.stream不是一种数据结构 ...

  5. Linux性能优化从入门到实战:14 文件系统篇:Linux 文件系统基础

      磁盘为系统提供了最基本的持久化存储.   文件系统则在磁盘的基础上,提供了一个用来管理文件的树状结构. 文件系统:索引节点和目录项   文件系统是对存储设备上的文件,进行组织管理的机制.组织方式不 ...

  6. 13Ajax和JQuery

    1.Ajax 1.1是什么? “Asynchronous Javascript And XML”(异步JavaScript和XML), 并不是新的技术,只是把原有的技术,整合到一起而已. 1.使用CS ...

  7. 前端每日实战:30# 视频演示如何用纯 CSS 创作一个晃动的公告板

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/wjZoGV 可交互视频教程 此视频 ...

  8. hdu 6044 : Limited Permutation (2017 多校第一场 1012) 【输入挂 组合数学】

    题目链接 参考博客: http://blog.csdn.net/jinglinxiao/article/details/76165353 http://blog.csdn.net/qq_3175920 ...

  9. MySql不区分大小写。

    解决方案: 1:给相关字段添加上让其区分大小写. alter table 表名 modify column 字段名 varchar(100) binary character set utf8

  10. php similar_text()函数 语法

    php similar_text()函数 语法 作用:计算两个字符串的相似度,并返回匹配字符的数目大理石平台厂家 语法:similar_text(string1,string2,percent) 参数 ...