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 最长公共子串 后缀自动机的更多相关文章

  1. codevs 3160 最长公共子串 后缀自动机

    http://codevs.cn/problem/3160/ 后缀自动机板子题,匹配的时候要注意如果到一个点失配向前匹配到一个点时,此时的tmp(当前匹配值)为t[j].len+1而不是t[t[j]. ...

  2. poj 2774 最长公共子串 后缀数组

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 25752   Accepted: 10 ...

  3. bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp)

    bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp) bzoj Luogu 题解时间 给两个小写字母串 $ A $ , $ B $ ,请你计算: ...

  4. CODE【VS】 3160 最长公共子串 (后缀数组)

    3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Outp ...

  5. Codevs 3160 最长公共子串(后缀数组)

    3160 最长公共子串 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长 ...

  6. POJ 2774 Long Long Message [ 最长公共子串 后缀数组]

    题目:http://poj.org/problem?id=2774 Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total ...

  7. 【codevs3160】最长公共子串 后缀数组

    题目描述 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入 读入两个字符串 输出 输出最长公共子串的长度 样例输入 yeshowmuchiloveyoumydearmotherrea ...

  8. BZOJ 4032: [HEOI2015]最短不公共子串 后缀自动机 暴力

    4032: [HEOI2015]最短不公共子串 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4032 Description 在虐各种最 ...

  9. bzoj 4032 [ HEOI 2015 ] 最短不公共子串 —— 后缀自动机+序列自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4032 序列自动机其实就是每个位置记录一下某字母后面第一个出现位置,为了子序列能尽量长. 对字 ...

随机推荐

  1. 笔记本自开wifi设置

    笔记本自开wifi设置 是这样的有些笔记本他自身就可以放出热点供其他的小伙伴们连接,不用非得去下专门的工具有些笔记本的网卡是自带支持双收发的(这里注意我指的是有些笔记本不是全部) 命令我已经写出来了  ...

  2. python基础===对字符串进行左右中对齐

    例如,有一个字典如下: >>> dic = { "name": "botoo", "url": "http:// ...

  3. 在新版linux上编译老版本的kernel出现kernel/timeconst.h] Error 255

    在使用ubuntu16.4编译Linux-2.6.31内核时出现这样的错误 可以修改timeconst.pl的内容后正常编译. 以下是编译错误提示的内容: Can't use 'defined(@ar ...

  4. input只读属性 设置和移除 选择数字

    设置只读属性 $('#stage').attr("readonly", "readonly"); 移除 只读属性  $("input").r ...

  5. oracle 一个网站

    http://www.oracle.com/technetwork/cn/articles/11g-pivot-101924-zhs.html

  6. Codeforces 798D - Mike and distribution(二维贪心、(玄学)随机排列)

    题目链接:http://codeforces.com/problemset/problem/798/D 题目大意:从长度为n的序列A和序列B中分别选出k个下表相同的数要求,设这两个序列中k个数和分别为 ...

  7. Spring Boot Admin Quick Start

    Quick Start 1. Spring Boot Admin是干什么的? 用来监控Spring Boot项目,可以通过Spring Boot Admin Client(via Http) 或者 使 ...

  8. Django之进阶相关操作

    一.QuerySet的特点 1.可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. 1 >>> Entry.objec ...

  9. Sublime Text 2.0.2,Build 2221注册码

    Help ->Enter License,输入如下序列号: ----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-855 ...

  10. 翻译:MLAPP(2.1节 概率概述)

    笔者:尝试翻译MLAPP(Machine Learning: a Probabilistic Perspective)一书,供机器学习的学者参考,如有错误理解之处请指出,不胜感激!(如需转载,请联系本 ...