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\ ...
随机推荐
- python socketserver框架解析
socketserver框架是一个基本的socket服务器端框架, 使用了threading来处理多个客户端的连接, 使用seletor模块来处理高并发访问, 是值得一看的python 标准库的源码之 ...
- js定义数组的方法
1.定义时赋值 var mycars=new Array("a","b","c") 2.new一个数组对象 var mycars=new A ...
- Error filterStart的问题
今天出现这个问题 严重: Error filterStart org.apache.catalina.core.StandardContext start 严重: Context startup fa ...
- pslq常用操作
1,登录后默认自动选中My Objects 默认情况下,PLSQL Developer登录后,Brower里会选择All objects,如果你登录的用户是dba,要展开tables目录,正常情况都 ...
- memocache 分布式搭建
memcached+magent实现memcached集群 首先说明下memcached存在如下问题 本身没有内置分布式功能,无法实现使用多台Memcache服务器来存储不同的数据,最大程度的使用 ...
- 为何90%的IT技术人员不适合做老大
老男孩老师终于写了一篇让我觉得不错的文章,哈哈,转一下. 什么是格局? 格局就是能够很好的平衡短期利益和长期利益. 过分注重短期利益的人必然会失去长期利益,到头来一定会很普通. 例如:跳槽不断,可能短 ...
- AngularJS:何时应该使用Directive、Controller、Service?
AngularJS:何时应该使用Directive.Controller.Service? (这篇文章你们一定要看,尤其初学的人,好吗亲?) 大漠穷秋 译 AngularJS是一款非常强大的前端MVC ...
- Nodejs经验谈
前言 这里主要说一下之前使用Nodejs开发踩过的坑,只说坑不填坑,那就是赤裸地耍流氓,文中有大量的说明及填坑方法,了解的看官可以直接跳过. PS:说实话,Nodejs的坑确实蛮多的:但是上手简单,扩 ...
- Storyboard中ViewController加载的四种方式
这个总结来自于<Programming iOS 10>一书: 1.storyboard的初始化ViewController,通过方法instantiateInitialViewContro ...
- Android Studio集成Lombok Plugin
Lombok是Android Studio名列前茅的插件,有啥用,看这个:http://www.blogjava.NET/fancydeepin/archive/2012/07/12/lombok.h ...