bzoj4044
这题简直了…………
首先根据操作可知,我们肯定是先造出某个偶数长度的回文串,然后添加若干字符得到设回文串长为len[x]
则ans=min(n-len[x]+f[x]);
那么问题就是制造这个串的回文串,最少的操作次数
对于回文串x,我们有这种情况
f[x]=min(f[y]+1,(y是x去掉首尾),f[y]+len[x]-len[y](y是最长回文前缀,(其实这个可以不考虑,因为这就相当于直接制造y)
=(f[y]+len[x] div 2-len[y]+1,y是长度不超过x一半的最长回文后缀)
dp大家都会,但状态怎么表示呢?………………
显然一个串回文串不超过n,但是怎么表示之间的关系呢?
我曾yy了一种manacher+后缀自动机的做法,结果是又WA又T一时爽……
无奈学了一下回文树,感觉是非常简洁明快的
学习可以转到:http://pan.baidu.com/s/1hqzRlvm
感觉有点像ac自动机,还是比较好懂的,这样就简单了
但我pascal本地AC交上去不停RE不知为何……
var go:array[..,..] of longint;
len,fa,sub,a,q,f:array[..] of longint;
last,t,ans,i,tt,n:longint;
s:ansistring; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; function what(ch:char):longint;
begin
if ch='A' then exit();
if ch='T' then exit();
if ch='C' then exit();
if ch='G' then exit();
exit(-);
end; procedure getchar;
var i:longint;
begin
readln(s);
n:=length(s);
a[n+]:=-;
for i:= to n do
a[i]:=what(s[i]);
end; function getf(x:longint):longint;
begin
while a[i-len[x]-]<>a[i] do x:=fa[x];
exit(x);
end; procedure newnode(l:longint);
begin
inc(t);
fillchar(go[t],sizeof(go[t]),);
len[t]:=l;
fa[t]:=;
end; procedure add(c:longint);
var now,cur,x:longint;
begin
cur:=getf(last);
if go[cur,c]= then
begin
newnode(len[cur]+); now:=t;
fa[now]:=go[getf(fa[cur]),c];
go[cur,c]:=now;
if len[now]<= then f[now]:=len[now]
else begin
x:=sub[cur];
while (a[i-len[x]-]<>a[i]) or (*(len[x]+)>len[now]) or (len[x] mod =) do x:=fa[x];
sub[now]:=go[x,c];
end;
end;
last:=go[cur,c];
end; procedure bfs;
var h,r,x,y,i,w:longint;
begin
h:=;
r:=;
q[]:=;
f[]:=;
while h<=r do
begin
x:=q[h];
for i:= to do
begin
y:=go[x,i];
if y<> then
begin
inc(r);
q[r]:=y;
if len[y]> then
begin
f[y]:=min(f[x]+,len[y] div -len[sub[y]]+f[sub[y]]+);
ans:=min(ans,n-len[y]+f[y]);
end;
end;
end;
inc(h);
end;
end; begin
readln(tt);
while tt> do
begin
dec(tt);
getchar;
t:=-;
newnode();
newnode(-);
fa[]:=;
last:=;
for i:= to n do
add(a[i]);
ans:=n;
bfs;
writeln(ans);
end;
end.
bzoj4044的更多相关文章
- bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp)
bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp) bzoj Luogu 你要用ATGC四个字母用两种操作拼出给定的串: 1.将其中一个字符 ...
- bzoj4044 [Cerc2014] Virus synthesis
回文自动机上dp f[x]表示形成x代表的回文串所需的最小步数, 若len[x]为奇数,f[x]=len[x],因为即使有更优的,也是直接添加,没有复制操作,那样就不用从x转移了. 若len[x]为偶 ...
- [BZOJ4044]Virus synthesis 回文自动机的DP
4044: [Cerc2014] Virus synthesis Time Limit: 20 Sec Memory Limit: 128 MB Description Viruses are us ...
- Bzoj4044 Virus synthesis
题意 你要用 \(ATGC\) 四个字母用两种操作拼出给定的串: 将其中一个字符放在已有串开头或者结尾 将已有串复制,然后 \(reverse\) ,再接在已有串的头部或者尾部 一开始已有串为空.求最 ...
- BZOJ4044: [Cerc2014] Virus synthesis(回文树+DP)
Description Viruses are usually bad for your health. How about fighting them with... other viruses? ...
- 【XSY2534】【CF835D】Palindromic characteristics 回文自动机
题目大意 一个字符串\(s\)是\(1\)−回文串当且仅当这个串是回文串. 一个串\(s\)是\(k\)−回文串\((k>1)\)当且仅当\(s\)的前一半与后一半相同且\(s\)的前一 ...
随机推荐
- 关于12306登陆页面dynamicJs的获取
今天帮与一个朋友探讨此事,刚开始一直是以为访问404,但是发现返回为200,没有问题,后来才知道朋友想了解的是为何浏览器可以获取到/otn/dynamicJs,但是自己手动获取就获取不到了 找了很久r ...
- ubuntu中磁盘挂载与卸载
问题描述: ubuntu中磁盘的挂载和卸载 问题解决: (1)ubuntu中磁盘挂载 注: 如上所示,使用命令df查看磁盘使用情况 ...
- Linux 命令整理 —— 用户管理
Linux用户管理以读.写.执行动作为权限,以用户组为单位,限制用户行为.对于文件的的操作,可以限制读.写.执行中的哪一种,也可以限制文件所有者.组用户.组外用户相应的权限. 所以,要建立用户,最好先 ...
- poj 2362
回溯加剪枝 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> # ...
- linux上部署应用
1.编写traffic.sh 引入相关的jar包及java环境路径 2.crontab -e 加入: */10 * * * * cd /opt/sys/traffic_version/bin & ...
- ADO.NET EF实体框架
ADO.NET 实体框架概述 随着.NET Framework 3.5 SP1和Visual Studio 2008 SP1的正式发布.ADO.NET 实体框架正式来到开发人员的面前,它使开发人员可以 ...
- 一个Java程序员应该掌握的10项技能
1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项,命令至少需要熟悉:a ...
- POJ 1417 True Liars(种类并查集+dp背包问题)
题目大意: 一共有p1+p2个人,分成两组,一组p1,一组p2.给出N个条件,格式如下: x y yes表示x和y分到同一组,即同是好人或者同是坏人. x y no表示x和y分到不同组,一个为好人,一 ...
- 嘿嘿,JAVA里第一次运行单元测试成功,立存
按书上写的单元测试. 居然一次过,爽!!! package org.smart4j.chapter2.test; import java.util.HashMap; import java.util. ...
- hadoop2.2.0集群搭建与部署
原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/3818908.html 一.安装环境 1.系统环境 CentOS 6.4 2.集群机器节点ip 节点一i ...