bzoj4044 [Cerc2014] Virus synthesis
回文自动机上dp
f[x]表示形成x代表的回文串所需的最小步数,
若len[x]为奇数,f[x]=len[x],因为即使有更优的,也是直接添加,没有复制操作,那样就不用从x转移了。
若len[x]为偶数,f[x]=min(f[fa[x]]+1,len[x]/2-len[fro[x]]+f[fro[x]]+1),因为fa[x]肯定是最后复制更优,那么将复制前的子串末尾加一个字符即可,fro则是len不大于len[x]/2的最长的x的fail.
memsetT死了,改成每次只memset用的就过了
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100050
using namespace std;
int len[N],ch[N][],nxt[N],fro[N],f[N];
int T,n,ans,last,tot;
char s[N];
inline void init(){
ans=0x7fffffff;
last=;tot=;
memset(ch[],,sizeof ch[]);
memset(ch[],,sizeof ch[]);
nxt[]=nxt[]=;
len[]=-;
}
inline int getfail(int x,int en){
while(s[en]!=s[en-len[x]-])x=nxt[x];
return x;
}
inline void insert(int t,int en){
int now=getfail(last,en);
if(!ch[now][t]){
int x=++tot;
memset(ch[x],,sizeof ch[x]);
len[x]=len[now]+;
int fa=getfail(nxt[now],en);
nxt[x]=ch[fa][t];
fa=fro[now];
if(len[x]<=)fro[x]=;
else{
while(s[en]!=s[en-len[fa]-]||(len[fa]+)*>len[x])
fa=nxt[fa];
fro[x]=ch[fa][t];
}
if(len[x]&||len[x]<=)f[x]=len[x];
else f[x]=min(f[now]+,len[x]/-len[fro[x]]+f[fro[x]]+);
ans=min(ans,n-len[x]+f[x]);
ch[now][t]=x;
}
last=ch[now][t];
}
int hash(char x){
if(x=='A')return ;
if(x=='T')return ;
if(x=='C')return ;
return ;
}
void work(){
init();
scanf("%s",s);
n=strlen(s);
for(int i=;i<n;i++)
insert(hash(s[i]),i);
printf("%d\n",ans);
}
int main(){
scanf("%d",&T);
while(T--)work();
return ;
}
bzoj4044 [Cerc2014] Virus synthesis的更多相关文章
- BZOJ4044: [Cerc2014] Virus synthesis(回文树+DP)
Description Viruses are usually bad for your health. How about fighting them with... other viruses? ...
- bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp)
bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp) bzoj Luogu 你要用ATGC四个字母用两种操作拼出给定的串: 1.将其中一个字符 ...
- luogu_4762: [CERC2014]Virus synthesis
洛谷_4762:[CERC2014]Virus synthesis 题目描述: 初始有一个空串,利用下面的操作构造给定串\(S\).\(len(S)\leq10^5\) 1: 串开头或末尾加一个字符. ...
- [CERC2014]Virus synthesis【回文自动机+DP】
[CERC2014]Virus synthesis 初始有一个空串,利用下面的操作构造给定串 SS . 1.串开头或末尾加一个字符 2.串开头或末尾加一个该串的逆串 求最小化操作数, \(|S| \l ...
- [CERC2014] Virus synthesis
设f[i]为形成极长回文串i的最小操作数.答案为min f[i]+n-len[i]. 在不形成偶回文的情况下形成奇回文的最小操作数为该串长度.可以不考虑(但ans赋为len). 正确性基于: 1)奇. ...
- 洛谷P4762 [CERC2014]Virus synthesis(回文自动机+dp)
传送门 回文自动机的好题啊 先建一个回文自动机,然后记$dp[i]$表示转移到$i$节点代表的回文串的最少的需要次数 首先肯定2操作越多越好,经过2操作之后的串必定是一个回文串,所以最后的答案肯定是由 ...
- bzoj 4044: [Cerc2014] Virus synthesis【回文自动机+dp】
建回文自动机,注意到一个回文串是可以通过一个长度小于等于这个串长度的一半的回文串添上一些字符然后复制得到的,也就是在自动机上向fa走,相当于treedp 每次都走显然会T,记录一个up,指向祖先中最下 ...
- BZOJ 4044 Luogu P4762 [CERC2014]Virus Synthesis (回文自动机、DP)
好难啊..根本不会做..基本上是抄Claris... 题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=4044 (luogu) ...
- P4762 [CERC2014]Virus synthesis
题意 真是道回文自动机好题. 首先考虑答案必定是一个回文串+剩余部分的形式,因此可以建出回文自动机,之后考虑每个长度为偶数的回文串. 对于一个长度为偶数的回文串,设它在回文自动机上对应的节点为\(x\ ...
随机推荐
- 初步认识thymeleaf:简单表达式和标签(二)
1.th:each:循环,<tr th:each="user,userStat:${users}">,userStat是状态变量,有 index,count,size, ...
- 2.3MySQL 自带工具使用介绍
1.mysql 首先看看“-e, --execute=name”参数,这个参数是告诉mysql,我只要执行“-e”后面的某个命令,而不是要通过mysql 连接登录到MySQL Server 上面.此参 ...
- python---数据类型---列表
#列表: name = ["lc","pxm","pt"] print('------------',name[2],"----- ...
- centos 7查看防火墙报错(已解决,之前安装过python3)
[root@localhost ~]# service firewalld restartRedirecting to /bin/systemctl restart firewalld.service ...
- 网站内容禁止复制和粘贴、另存为的js代码
1.使右键和复制失效 方法1: 在网页中加入以下代码: 复制代码代码如下: <script language="Javascript"> document.oncont ...
- Spring Cache 笔记
@(Java ThirdParty)[Spring Cache] Spring Cache Abstraction 简介 Spring Cache提供了对底层缓存使用的抽象,通过注解的方式使用缓存,减 ...
- JS windows对象的top属性
原博文:http://www.jb51.net/article/44078.htm 本文为大家介绍下JS window对象的top.parent.opener含义,不了解的朋友可以参考下,希望对大 ...
- ArcCore重构-Platform_Types.h实现辨析
AUTOSAR定义了一系列PlatformTypes,如uint8/uint16/uint32等等基本类型. It contains all platform dependent types and ...
- Xamarin.Android 使用百度地图获取定位信息
最近做一个项目,web端使用百度地图,PDA使用手持机自带的GPS定位系统获取经纬度,然后再百度地图上显示该经纬度会有一定距离的差异,这里就像可乐的瓶子拧上雪碧的盖子,能拧的上却不美观.所以为了数据的 ...
- 在C++98基础上学习C++11新特性
自己一直用的是C++98规范来编程,对于C++11只闻其名却没用过其特性.近期因为工作的需要,需要掌握C++11的一些特性,所以查阅了一些C++11资料.因为自己有C++98的基础,所以从C++98过 ...