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 序列自动机其实就是每个位置记录一下某字母后面第一个出现位置,为了子序列能尽量长. 对字 ...
随机推荐
- JS几个常用的工具函数
一个项目中JS也不可避免会出现重用,所以可以像Java一样抽成工具类,下面总结了几个常用的函数: 1.日期处理函数 将日期返回按指定格式处理过的字符串: function Format(now,mas ...
- 执行impdp时出现的各种问题
1.不同的表空间,不同的用户,不同的表名 impdp ODS_YYJC_BUF_ZB/ODS_YYJC_BUF_ZB job_name=bs3 directory=EXPDMP exclude=OBJ ...
- Python Random模块生成伪随机数字
This module implements pseudo-random number generators for various distributions. 对于整数,有一个范围的均匀选择: 对 ...
- web deploy 安装失败解决
单独运行安装包,提示脚本运行失败. VS安装提示解包失败. 解决:检查Windows Management Instrumentation服务状态.需要非禁用.
- 出现ERROR: While executing gem ... (Gem::FilePermissionError)这种错误的解决办法
重新安装ruby即可解决 brew install ruby
- Codeforces 799B - T-shirt buying(STL)
题目链接:http://codeforces.com/problemset/problem/799/B 题目大意:有n件T恤,每件T体恤都分别有价格(每件衣服的价格不重复).前面的颜色.背部的颜色三种 ...
- mac os版本Intellij IDEA 搭建spring mvc的maven工程(新手教学)
由于近期换了新公司,又换mac pro作为新电脑,打算把用了很多年的eclipse换成IDEA(IDEA比eclipse的好处我就不多说了),由于mac os和IDEA刚开始用不久,所以专门用一篇博客 ...
- webpack2.0构建vue2.0超详细精简版
原文地址:http://blog.csdn.net/dahuzix/article/details/55549387 npm init -y 初始化项目 安装各种依赖项 npm install --s ...
- JavaScript之setInterval() 函数
定义和用法 setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被 ...
- poj2253 Frogger(Floyd)
题目链接 http://poj.org/problem?id=2253 题意 给出青蛙A,B和若干石头的坐标,现在青蛙A要跳到青蛙B所在的石头上,求出所有路径中最远那一跳的最小值. 思路 Floyd算 ...