[BZOJ4044]Virus synthesis 回文自动机的DP
4044: [Cerc2014] Virus synthesis
Time Limit: 20 Sec Memory Limit: 128 MB
Description
Input
Output
Sample Input
AAAA
AGCTTGCA
AAGGGGAAGGGGAA
AAACAGTCCTGACAAAAAAAAAAAAC
Sample Output
8
6
18
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=,inf=0x7fffffff;
char s[N];
struct Pam_Tree
{
int n,m,last,p,strategy[N];
int ch[N][],len[N],fail[N],f[N];
int q[N],hd,tl;
inline int id(char x)
{
switch(x)
{
case 'A':return ;
case 'C':return ;
case 'G':return ;
case 'T':return ;
}
return ;
}
inline int newnode(int l)
{
len[p]=l;memset(ch[p],,sizeof(ch[p]));
fail[p]=f[p]=strategy[p]=;
return p++;
}
inline int getfail(int x)
{
while(s[n-len[x]-]!=s[n])x=fail[x];
return x;
}
inline void work()
{
register int i,x,u;
hd=,tl=,q[++tl]=,f[]=;
int ans=m;
for(i=;i<p;++i)if(len[i]&)f[i]=i;
while(hd<=tl)
for(x=q[hd++],i=;i<;++i)
if((u=ch[x][i]))
q[++tl]=u,
f[u]=min(f[x]+,len[u]/+f[strategy[u]]-len[strategy[u]]+),
ans=min(ans,f[u]+m-len[u]);
printf("%d\n",ans);
}
inline void insert()
{
register int i,now,cur,d,x;
for(n=;n<=m;++n)
{
d=id(s[n]),cur=getfail(last);
if(!ch[cur][d])
{
now=newnode(len[cur]+),
fail[now]=ch[getfail(fail[cur])][d],
ch[cur][d]=now;
if(len[now]<=)strategy[now]=fail[now];
else
{
x=strategy[cur];
while(s[n-len[x]-]!=s[n]||(len[x]+)*>len[now])x=fail[x];
strategy[now]=ch[x][d];
}
}
last=ch[cur][d];
}
work();
}
inline void intn()
{
scanf("%s",s+),p=,newnode(),newnode(-);
s[]=,m=strlen(s+),last=,fail[]=,insert();
}
}PT;
int main()
{
int t;scanf("%d",&t);
while(t--)PT.intn();
}
[BZOJ4044]Virus synthesis 回文自动机的DP的更多相关文章
- BZOJ 4044 Luogu P4762 [CERC2014]Virus Synthesis (回文自动机、DP)
好难啊..根本不会做..基本上是抄Claris... 题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=4044 (luogu) ...
- bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp)
bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp) bzoj Luogu 你要用ATGC四个字母用两种操作拼出给定的串: 1.将其中一个字符 ...
- bzoj 4044: Virus synthesis 回文自动机
题目大意: 你要用ATGC四个字母用两种操作拼出给定的串: 将其中一个字符放在已有串开头或者结尾 将已有串复制,然后reverse,再接在已有串的头部或者尾部 一开始已有串为空.求最少操作次数. le ...
- BZOJ 4044 Virus synthesis (回文自动机+dp)
题目大意: 你可以在一个串的开头或者末尾加入一个字符,或者把当前整个串$reverse$,然后接在前面或者后面,求达到目标串需要的最少操作次数 对目标串建出$PAM$ 定义$dp[x]$表示当前在回文 ...
- bzoj 4044 Virus synthesis - 回文自动机 - 动态规划
题目传送门 需要高级权限的传送门 题目大意 要求用两种操作拼出一个长度为$n$的只包含'A','T','G','C'的字符串 在当前字符串头或字符串结尾添加一个字符 将当前字符串复制,将复制的串翻转, ...
- luogu P4762 [CERC2014]Virus synthesis (回文自动机)
大意: 初始有一个空串, 操作(1)在开头或末尾添加一个字符. 操作(2)在开头或末尾添加该串的逆串. 求得到串$S$所需最少操作数. 显然最后一定是由某个偶回文通过添加字符得到的, 那么只需要求出所 ...
- [加强版] Codeforces 835D Palindromic characteristics (回文自动机、DP)
题目链接: https://codeforces.com/contest/835/problem/D 题意: 一个回文串是\(1\)-回文的,如果一个回文串的左半部分和右半部分一样且都是\(k\)-回 ...
- [CERC2014]Virus synthesis【回文自动机+DP】
[CERC2014]Virus synthesis 初始有一个空串,利用下面的操作构造给定串 SS . 1.串开头或末尾加一个字符 2.串开头或末尾加一个该串的逆串 求最小化操作数, \(|S| \l ...
- 洛谷P4762 [CERC2014]Virus synthesis(回文自动机+dp)
传送门 回文自动机的好题啊 先建一个回文自动机,然后记$dp[i]$表示转移到$i$节点代表的回文串的最少的需要次数 首先肯定2操作越多越好,经过2操作之后的串必定是一个回文串,所以最后的答案肯定是由 ...
随机推荐
- C++实现从一个文件夹中读出所有txt文件
前段时间做项目需要读取一个文件夹里面所有的txt文件,查询资料后得到以下实现方法:首先了解一下这个结构体struct _finddata_t { unsigned attrib; t ...
- python log封装
# _*_ coding:utf-8 _*_ import logging import os import sys import time log_path = os.path.dirname(sy ...
- 小强版之无码理解C语言指针
1. 先从普通变量开始 2. 撸完变量撸指针 3. 故事情节进一步发展,此处少儿不宜 4. 奶茶妹妹捉奸,小强死定了 5. 源码欣赏 #include <stdio.h> ...
- docker server gave HTTP response to HTTPS client 问题处理办法
vi /etc/docker/daemon.json [root@localhost ~]# cat /etc/docker/daemon.json {"insecure-registrie ...
- 【Docker】第五篇 Docker 数据管理
一.基本介绍 数据管理的原因:Docker中的容器一旦删除,容器本身的rootfs文件系统就会被删除,容器中的所有数据就会被删除.为了对一些需要持久化的数据,不随容器删除而删除,所以我们可以通过多个容 ...
- gulp: Did you forget to signal async completion? 解决方案
背景 学习gulp的前端自动化构建,按照示例代码,跑了一个简单的task,控制台打出如下提示: The following tasks did not complete: testGulp Did y ...
- Vs2012 编写代码规则
FxCop编写规则 VS2012 下更方便,所需的DLL在: D:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Static ...
- fs - 文件系统
fs 模块提供了一些 API,用于以一种类似标准 POSIX 函数的方式与文件系统进行交互. 用法如下: const fs = require('fs'); 所有的文件系统操作都有异步和同步两种形式. ...
- js/jquery去掉空格,回车,换行示例代码
Jquery: $("#accuracy").val($("#accuracy").val().replace(/\ +/g,""));// ...
- python learning OOP2.py
class Student(object): pass s = Student() s.name = 'Chang' # 给一个实例动态绑定一个属性 print(s.name) def set_age ...