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. 遍历目录大小——php经典实例

    遍历目录大小——php经典实例 <?php function dirSize($dir){ //定义大小初始值 $sum=; //打开 $dd=opendir($dir); //遍历 while ...

  2. Installation Guide for Appium 1.6.3

    A.) System Requirements : - Require node 4 or above Xcode 8 iOS 10 B.) Open terminal and type follow ...

  3. 面试中关于Redis的问题看这篇就够了

    昨天写了一篇自己搭建redis集群并在自己项目中使用的文章,今天早上看别人写的面经发现redis在面试中还是比较常问的(笔主主Java方向).所以查阅官方文档以及他人造好的轮子,总结了一些redis面 ...

  4. 74.VS2013和opencv3.1.0安装教程

    一.先下载文件 1.VS2013 VS2013有很多版本,专业版,旗舰版,中文英文之类的,所对应的密钥也不一样.我选择的是简体中文专业版.下载链接如下. http://www.musnow.com/t ...

  5. Windows 7 64 位操作系统安装 Ubuntu 17.10

    一.准备工作 1. DiskGenius:分区工具,为 Linux 建立单独的分区.(Linux 公社下载源) 2. UUI:Universal USB Installer,通用 U 盘安装器,用来制 ...

  6. openjudge-NOI 2.6基本算法之动态规划 专题题解目录

    1.1759 最长上升子序列 2.1768 最大子矩阵 3.1775 采药 4.1808 公共子序列 5.1944 吃糖果 6.1996 登山 7.2000 最长公共子上升序列 8.2718 移动路线 ...

  7. 非交互式shell脚本案例-实现自主从oracle数据库获取相关数据,并在制定目录生成相应规则的文件脚本

    get_task_id 脚本内容 #!/usr/bin/expect#配置登陆数据库的端口set port 22#配置登陆数据库的ip地址set oracleip 10.0.4.41#配置数据库实例名 ...

  8. 《深入理解Java虚拟机》笔记--第十二章、Java内存模型与线程

    主要内容:虚拟机如何实现多线程.多线程之间由于共享和竞争数据而导致的一系列问题及解决方案. Java内存模型:     Java内存模型的主要目标是定义程序中各个变量的访问规则,即在虚拟机中将变量存储 ...

  9. Delphi 中的自动释放策略

    来自万一老师的博客:http://www.cnblogs.com/del/archive/2011/12/21/2295794.html ------------------------------- ...

  10. 可图性判定--Havel-Hakimi定理

    两个概念 1.度序列 若把图G所有顶点的度数排成一个序列S,则称S为图G的度序列. 2.序列是可图的 一个非负整数组成的序列如果是某个无向图的度序列,则称该序列是可图的. Havel-Hakimi定理 ...