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\)的前一 ...
随机推荐
- 升级iOS10之后调用摄像头/麦克风等硬件程序崩溃闪退的问题
在升级到iOS10之后, 开发过程中难免会遇到很多的坑, 下面是一些常见的坑, 我做了一些整理, 希望对大家开发有帮助: &1. 调用视频,摄像头, 麦克风,等硬件程序崩溃闪退的问题: 要注意 ...
- c语言编程之二叉树
利用链表建立二叉树,完成前序遍历.中序遍历.后序遍历. 建立二叉树用的是前序遍历建立二叉树: #include<stdio.h> #include<stdlib.h> #inc ...
- 2659: [Beijing wc2012]算不出的算式 - BZOJ
最近有点颓废,刷水题,数学题(根本不会做啊) 题意:求 q,p是两个奇质数 网上题解就直接说是几何意义,问了别人才知道 我们在坐标轴上画出来就是在线段y=(q/p)x下方的格点和y=(p/q)x下方的 ...
- Unsupervised Learning: Use Cases
Unsupervised Learning: Use Cases Contents Visualization K-Means Clustering Transfer Learning K-Neare ...
- unit3d 4.6 document open solution
发现4.6 的 本地 文档字体解析采用 fonts.googleapis.com ,可是google 自古就与天朝不在一个鼻孔,所以你打开往往需要半天. 网上查了一下,把所有本地的*.html 文档 ...
- jstl删除session,choose,动态获取request当前工程路径
1.jstl标签c:remove删除session request.getSession().setAttribute("ssmsg", "修改成功"); &l ...
- POJ2586Y2K Accounting Bug
http://poj.org/problem?id=2586 Description Accounting for Computer Machinists (ACM) has sufferred fr ...
- LINQ——语言级集成查询入门指南(1)
本文主要是对语言级集成查询或简称为LINQ做一个介绍,包括LINQ是什么,不是什么,并对它在语言特性方面做一个简短的回顾,然后举一些使用LINQ的实际例子进行说明. 语言级集成查询是什么? 在我过去写 ...
- Unity打包APK横屏时的注意事项
由于你在Unity设置了横屏. 所以也需要在安卓的AndroidManifest.xml文件中, application/activity下声明为横屏.否则会黑屏,根本不给你报错,愁死你. 加上这一句 ...
- Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序(<order-by>\<sort>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...