SCOJ 4493: DNA 最长公共子串 后缀自动机
4493: DNA
题目连接:
http://acm.scu.edu.cn/soj/problem.action?id=4493
Description
Deoxyribonucleic acid (DNA) is a molecule that carries most of the genetic instructions used in the development,
functioning and reproduction of all known living organisms and many viruses.
Most DNA molecules consist of two biopolymer strands coiled around each other to form a double helix.
The two DNA strands are known as polynucleotides since they are composed of simpler units called nucleotides.
Each nucleotide is composed of a nitrogen-containing nucleobase—either cytosine (C), guanine (G), adenine (A), or thymine (T)—
as well as a monosaccharide sugar called deoxyribose and a phosphate group. According to base pairing rules (A with T, and C with G),
hydrogen bonds bind the nitrogenous bases of the two separate polynucleotide strands to make double-stranded DNA.
We define the length of a strand as the number of its nucleobases. Given a bunch of different DNA strands, for each strand,
find the length of the longest common pieces between the two complementary strands.
Input
The first line is the number of test cases, T, where 0 < T<=100.
Each line followed represents a DNA strand, whose length is no more than 5000.
Output
For each strand, print a number, indicating the answer illustrated above.
Sample Input
3
A
AT
ATAT
Sample Output
0
1
3
Hint
题意
给你一个DNA序列,然后问他的互补串和他本身的最长公共子串是多少
题解:
后缀自动机/后缀数组/hash都可以过这道题
都是一个比较裸的题
下面代码是后缀自动机的
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5010;
char s1[maxn], s2[maxn];
struct SAM {
struct {
int len, f, ch[26];
void init() {
len = 0, f = -1;
memset(ch, 0xff, sizeof (ch));
}
} e[maxn<<1];
int idx, last;
void init() {
idx = last = 0;
e[idx++].init();
}
int newnode() {
e[idx].init();
return idx++;
}
void add(int c) {
int end = newnode();
int tmp = last;
e[end].len = e[last].len + 1;
for (; tmp != -1 && e[tmp].ch[c] == -1; tmp = e[tmp].f) {
e[tmp].ch[c] = end;
}
if (tmp == -1) e[end].f = 0;
else {
int nxt = e[tmp].ch[c];
if (e[tmp].len + 1 == e[nxt].len) e[end].f = nxt;
else {
int np = newnode();
e[np] = e[nxt];
e[np].len = e[tmp].len + 1;
e[nxt].f = e[end].f = np;
for (; tmp != -1 && e[tmp].ch[c] == nxt; tmp = e[tmp].f) {
e[tmp].ch[c] = np;
}
}
}
last = end;
}
};
SAM sam;
void solve()
{
scanf("%s",s1);
int len = strlen(s1);
for(int i=0;i<len;i++)
{
if(s1[i]=='A')s2[i]='T';
if(s1[i]=='T')s2[i]='A';
if(s1[i]=='C')s2[i]='G';
if(s1[i]=='G')s2[i]='C';
}
sam.init();
for(int i=0;i<len;i++)
sam.add(s1[i]-'A');
int p = 0,ans = 0,Len = 0;
for(int i=0;i<len;i++)
{
int id = s2[i]-'A';
if(sam.e[p].ch[id]!=-1)
Len++,p=sam.e[p].ch[id];
else
{
for(;p!=-1&&sam.e[p].ch[id]==-1;p=sam.e[p].f);
if(p==-1)Len=0,p=0;
else
{
Len = sam.e[p].len+1;
p = sam.e[p].ch[id];
}
}
ans = max(ans,Len);
}
printf("%d\n",ans);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)solve();
return 0;
}
SCOJ 4493: DNA 最长公共子串 后缀自动机的更多相关文章
- codevs 3160 最长公共子串 后缀自动机
http://codevs.cn/problem/3160/ 后缀自动机板子题,匹配的时候要注意如果到一个点失配向前匹配到一个点时,此时的tmp(当前匹配值)为t[j].len+1而不是t[t[j]. ...
- poj 2774 最长公共子串 后缀数组
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 25752 Accepted: 10 ...
- bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp)
bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp) bzoj Luogu 题解时间 给两个小写字母串 $ A $ , $ B $ ,请你计算: ...
- CODE【VS】 3160 最长公共子串 (后缀数组)
3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Outp ...
- Codevs 3160 最长公共子串(后缀数组)
3160 最长公共子串 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长 ...
- POJ 2774 Long Long Message [ 最长公共子串 后缀数组]
题目:http://poj.org/problem?id=2774 Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total ...
- 【codevs3160】最长公共子串 后缀数组
题目描述 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入 读入两个字符串 输出 输出最长公共子串的长度 样例输入 yeshowmuchiloveyoumydearmotherrea ...
- BZOJ 4032: [HEOI2015]最短不公共子串 后缀自动机 暴力
4032: [HEOI2015]最短不公共子串 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4032 Description 在虐各种最 ...
- bzoj 4032 [ HEOI 2015 ] 最短不公共子串 —— 后缀自动机+序列自动机
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4032 序列自动机其实就是每个位置记录一下某字母后面第一个出现位置,为了子序列能尽量长. 对字 ...
随机推荐
- 黑色的网站后台管理系统ui界面——后台
链接:http://pan.baidu.com/s/1pLffwE3 密码:m4v6
- 二. Jmeter--关联
1. 首先建立一个线程组(Thread Group),为什么所有的请求都要加入线程组这个组件呢?不加不行吗?答案当然是不行的.因为jmeter的所有任务都必须由线程处理,所有任务都必须在线程组下面创建 ...
- Sqlmap与burpsuite动态更新某些参数
有如下注入点: http://localhost/id=1&order_nu=1 情况说明: id为注入点, 每一次注入时, order_nu不能跟上次的一样(假说这个order_nu为一个 ...
- $fhqTreap$
- $fhqTreap$与$Treap$的差异 $fhqTreap$是$Treap$的非旋版本,可以实现一切$Treap$操作,及区间操作和可持久化 $fhqTreap$非旋关键在于分裂与合并$(Sp ...
- apache 软件历史版本查询
http://archive.apache.org/dist/ http://archive.apache.org/dist/struts/binaries/
- 20165301 2017-2018-2 《Java程序设计》第二周学习总结
20165301 2017-2018-2 <Java程序设计>第二周学习总结 教材学习内容总结 第二章:基本数据类型与数组 标识符 第一个字符不能是数字 不能是关键字 不能是true.fa ...
- 简单优化:Zipalign
Android SDK中包含一个“zipalign”的工具,它能够对打包的应用程序进行优化.在你的应用程序上运行zipalign,使得在运行时Android与应用程序间的交互更加有效率.因此,这种方式 ...
- a:hover伪类在ios移动端浏览器内无效的解决方法
a:hover 设置的样式在ios系统的浏览器内显示不出来,看来在iOS系统的移动设备中,需要在按钮元素或body/html上绑定一个touchstart事件才能激活:active状态. 方法 一: ...
- mysql 5.7 表名大小写不敏感
修改 my.cnf 添加 [mysqld] lower_case_table_names=1 重启MySQL service mysqld restart
- 快速开发window服务器程序
import service; service.startDispatchThread( 服务名 = function(serviceName,argv){ import service; var s ...