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. V8引擎回收机制、 内存泄露

     一.垃圾回收:将内存不在使用的数据进行清理,释放内存空间   v8将内存分为新生代空间和老生代的空间   新生代空间:用于存活较短的对象   :又分为二个空间:from空间和to空间   :Scav ...

  2. idea 创建java web项目ssm-gradle

    环境准备:jdk1.8+tomcat8+idea+gradle 1.创建项目SSM 使用gradle创建项目,按照提示如下   image.png 输入项目名称,组名   image.png   im ...

  3. Sql在Group by的select中包含多列

    SELECT A , B , COUNT(Id) AS '数量' FROM dbo.[Table] GROUP BY A , B

  4. Debug to add expression

    Debug expression

  5. rmdir 删除空目录

    1. 命令功能 rmdir:删除空目录,当目录不为空时,命令不能起作用. 2. 语法格式 rmdir  [option]  directory rmdir  选项  空目录 参数 参数说明 -p 递归 ...

  6. 共享OrCAD9.2pSpice9.2+multisim下载地址

    http://pan.baidu.com/s/1dDcfiH7ewb9绿色版,解压后即可用!http://pan.baidu.com/s/1kTG43WFMultisim v11绿色版.7zhttp: ...

  7. man VGCREATE

    VGCREATE(8)                                                        VGCREATE(8) NAME/名称       vgcreat ...

  8. Test 6.29 T2 染色

    问题描述 于是 CJK 轻轻松松就切了第一题."好,那么来看看第二题吧." JesseLiu 大手一挥,CJK 眼前立刻出现了一棵有 n 个节点的树."现在,你将要为这颗 ...

  9. 父工程 pom版本

    <!-- 集中定义依赖版本号 --> <properties> <junit.version>4.12</junit.version> <spri ...

  10. django的安装和初步使用

    安装参考:步骤也可以参考这个 很详细 https://blog.csdn.net/zww1984774346/article/details/54408759 如果想在终端查看项目结构 需要用到tre ...