poj 3080 kmp求解多个字符串的最长公共字串,(数据小,有点小暴力 16ms)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 14113 | Accepted: 6260 |
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
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
char test[][];
int next[];
int len1;
char head[];
int n;
int ma;
char result[];
void getnext(){
memset(next,,sizeof(next));
int i=,j=-;
next[]=-;
while(i<len1){
if(j==-||head[i]==head[j]){
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
} int kmp(){
ma=;
for(int ti=;ti<n;ti++){
int i=,j=,m=;
while(i<){
if(j==-||test[ti][i]==head[j]){
i++;
j++;
}
else
j=next[j];
if(j>m)
m=j;
}
if(m<ma)
ma=m; }
return ma;
} int main(){
int T;
scanf("%d",&T);
while(T--){
memset(test,,sizeof(test));
memset(result,,sizeof(result));
scanf("%d",&n);
getchar();
for(int i=;i<n;i++){
cin>>test[i];
}
int ans=;
for(int i=;i<=;i++){
memset(head,,sizeof(head));
len1=-i;
strcpy(head,test[]+i); /// 枚举第一个串的所有后缀串(当然最后2个可以省去)
head[len1]='\0';
getnext();
int temp=kmp(); /// KMP求出这个后缀串与其余所有串的最大匹配。
if(temp>ans){
ans=temp;
strncpy(result,test[]+i,ans);
}
else if(temp==ans){ /// 存在多个最长公共子串,输出字典序最小的,WA了一次。
if(strcmp(test[]+i,result)<)
strncpy(result,test[]+i,ans); /// 复习: strncpy()没有复制最后的'\0'。
} }
if(ans<)
printf("no significant commonalities\n");
else
printf("%s\n",result);
}
return ;
}
poj 3080 kmp求解多个字符串的最长公共字串,(数据小,有点小暴力 16ms)的更多相关文章
- (字符串)最长公共字串(Longest-Common-SubString,LCS)
题目: 给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa].二者的最长公共子串为[aba],长度为3. 子序列是不要求连续的,字串必须是连续的. 思路与代码: 1.简 ...
- java_基础知识_字符串练习题_计算两个字符串的最长公共字串长度
package tek; Java算法——求出两个字符串的最长公共字符串 /** * @Title: 问题:有两个字符串str1和str2,求出两个字符串中最长公共字符串. * @author 匹夫( ...
- poj 2774 后缀数组 两个字符串的最长公共子串
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 31904 Accepted: 12 ...
- POJ 2774 (后缀数组 最长公共字串) Long Long Message
用一个特殊字符将两个字符串连接起来,然后找最大的height,而且要求这两个相邻的后缀的第一个字符不能在同一个字符串中. #include <cstdio> #include <cs ...
- POJ - 2774~POJ - 3415 后缀数组求解公共字串问题
POJ - 2774: 题意: 求解A,B串的最长公共字串 (摘自罗穗骞的国家集训队论文): 算法分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长 公共子串等价于求 ...
- Python-求解两个字符串的最长公共子序列
一.问题描述 给定两个字符串,求解这两个字符串的最长公共子序列(Longest Common Sequence).比如字符串1:BDCABA:字符串2:ABCBDAB.则这两个字符串的最长公共子序列长 ...
- POJ - 3294~Relevant Phrases of Annihilation SPOJ - PHRASES~Substrings POJ - 1226~POJ - 3450 ~ POJ - 3080 (后缀数组求解多个串的公共字串问题)
多个字符串的相关问题 这类问题的一个常用做法是,先将所有的字符串连接起来, 然后求后缀数组 和 height 数组,再利用 height 数组进行求解. 这中间可能需要二分答案. POJ - 3294 ...
- C++求解汉字字符串的最长公共子序列 动态规划
近期,我在网上看了一些动态规划求字符串最长公共子序列的代码.可是无一例外都是处理英文字符串,当处理汉字字符串时.常常会出现乱码或者不对的情况. 我对代码进行了改动.使用wchar_t类型存储字 ...
- 求两个字符串的最长公共子串——Java实现
要求:求两个字符串的最长公共子串,如“abcdefg”和“adefgwgeweg”的最长公共子串为“defg”(子串必须是连续的) public class Main03{ // 求解两个字符号的最长 ...
随机推荐
- Oracle字符编码与汉字存储长度的处理
执行如下语句,查看汉字在数据库中所占的字节: select vsize('汉') from dual; 一般情况下,得到的结果大部分为值:2 或 3 一般linux下安装oracle数据库,默认字符编 ...
- Python测量时间,用time.time还是time.clock
在计算机领域有多种时间.第一种称作CPU时间或执行时间,用于测量在执行一个程序时CPU所花费的时间.第二种称作挂钟时间,测量执行一个程序时的总时间.挂钟时间也被称作流逝时间或运行时间.与CPU时间相比 ...
- 使用session处理用户搜索后数据的上一页和下一页跳转
搜索语句界面: /*单一检索:此处为一个下拉列表的检索*/ if(isset($_POST['submit']) && $_POST['submit'] == '点击搜索') { if ...
- using namespace std 是什么意思?
摘录CSDN上面大牛的回答简要意思就是使用标准库,想知道更清楚的继续读下面的. using namespace std 意思: using 和namespace都是C++的关键词. ...
- Linux实战教学笔记16:磁盘原理
第十五节 磁盘原理 标签(空格分隔): Linux实战教学笔记 1,知识扩展 非脚本方式的一条命令搞定批量创建用户并设置随机10位字母数字组合密码. 1.1 sed的高级用法 [root@chensi ...
- 最小化 Java 镜像的常用技巧
背景 随着容器技术的普及,越来越多的应用被容器化.人们使用容器的频率越来越高,但常常忽略一个基本但又非常重要的问题 - 容器镜像的体积.本文将介绍精简容器镜像的必要性并以基于 spring boot ...
- java常量池概念【转】
http://blog.csdn.net/olanlanxiari/article/details/8104505
- 在ubuntu上安装subline
Sublime Text is a most popular, lightweight and smart cross-platform text and source code editor wit ...
- python学习之数据类型与运算符号
python版本:3.6 python编辑器:pycharm 最新版本 整理成代码如下: #!/usr/bin/env python #-*- coding: utf-8 -*- # 数学操作符 pr ...
- MyFirstDay_答案_1.**猫(自己整理)
1>***猫: python基础类: 字符串反转的常用处理方式: # 方法一:使用字符串切片 s = "hello python" result = s[::-1] prin ...