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\ ...
随机推荐
- 阅读源码(IV)
往期系列: <由阅读源码想到> <由阅读源码想到 | 下篇> <阅读源码(III)> Eric S.Raymond的写于2014年的<How to learn ...
- PhpStorm php配置环境
如果你需要在Windows上安装PHP环境,并使用PhpStorm进行脚本编写进行编译,不需要WEB环境展示,那么本小结适合. 软件环境:Windows10+PHP7.1+PhpStorm2018 0 ...
- CRM客户关系管理系统(十二)
十二章.学员报名流程开发 2 12.1.学员报名合同和证件信息上传 功能: 必须勾选报名合同协议 必须上传个人证件信息 最多只能上传三个文件 文件大小2M以内 列出已上传文件 (1)crm/urls ...
- minimun path sum(最小路径和)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- profile bashrc bash_profile之间的区别和联系
profile bashrc bash_profile之间的区别和联系 博客分类: Linux 执行顺序为:/etc/profile -> (~/.bash_profile | ~/.bas ...
- left join 原理分析
left join 原理分析 [转贴 2006-11-15 16:19:50] 字号:大 中 小 案例分析 user表: id | name --------- 1 | libk ...
- linux 下查看wwn号
PC server主机与FC存储进行连接时,一般需要加装HBA卡,两者之间衔接的一个重要参数就是wwn号.redhat或suse下查看wwn号的方法如下.一.SuSE Linux 9查看 /proc/ ...
- postgresql to_char 问题
select create_time from xxx; select to_char(create_time,'yyyy-MM-dd HH24:mm:ss') as create_time fr ...
- Web安全学习计划
http://cisps.org/bbs/viewtopic.php?f=71&t=26125 标题为Web安全学习计划,实属我的愿望:将下面这份Web学习清单完善成为一个Web安全,学习计划 ...
- C#文件和字节流的转换方法
1.读取文件,并转换为字节流 FileStream fs = new FileStream(filename,FileMode.Open,FileAccess.Read); byte[] infbyt ...