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{ // 求解两个字符号的最长 ...
随机推荐
- 旋度定理(Curl Theorem)和散度定理(Divergence theorem)
原文链接 首先说说格林公式(Green's theorem).对于一段封闭曲线,若其围城的区域D为单连通区域(内部任意曲线围城的区域都属于院区域),则有如下公式: 其中其中L为D的边界,取正方向.如果 ...
- SpringBoot学习14:springboot异常处理方式4(使用SimpleMappingExceptionResolver处理异常)
修改异常处理方法3中的全局异常处理Controller即可 package bjsxt.exception; import org.springframework.context.annotation ...
- Friendly Date Ranges-freecodecamp算法题目
Friendly Date Ranges 1.要求 把常见的日期格式如:YYYY-MM-DD 转换成一种更易读的格式. 易读格式应该是用月份名称代替月份数字,用序数词代替数字来表示天 (1st 代替 ...
- HDU.2561 第二小整数(water)
题目来源:2561 题意分析:找出一堆数中第二小的整数,和题目说的一样 我的思路:冒泡或者sort()一下就ok了,但是我因为没看到多个测试用例还是吃了几记WA . ┭┮﹏┭┮ 完整代码: #incl ...
- 多任务版udp聊天器
import socket import threading def send_msg(udp_socket): """获取键盘数据,并将其发送给对方"&quo ...
- 千锋教育Vue组件--vue基础的方法
课程地址: https://ke.qq.com/course/251029#term_id=100295989 <!DOCTYPE html> <html> <head& ...
- 【转载】java 客户端链接不上redis解决方案 (jedis)
本文出自:http://blog.csdn.net/lulidaitian/article/details/51946169 出现问题描述: 1.Could not get a resource fr ...
- 裸机——210SD卡启动
1.通过阅读iROM_Application_note可以获取关于启动的全部信息 2.记录下代码 制作SD卡启动的代码,即添加校验和的 #include <strings.h> #incl ...
- 微信小程序 | 49,小程序入门集锦系列文章20篇
以下20篇文章,都是关于微信小程序的文章,以入门常见问题为主.如发现谬误,请与笔者联系. [小程序入门集锦]1,微信小程序在哪里打开 [小程序入门集锦]2,小程序商店 [小程序入门集锦]3,微信小程序 ...
- SparkSteaming中直连与receiver两种方式的区别
SparkStreaming的Receiver方式和直连方式有什么区别? Receiver接收固定时间间隔的数据(放在内存中的),使用高级API,自动维护偏移量,达到固定的时间才去进行处理,效率低并且 ...