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\)的前一 ...
随机推荐
- error:LNK2005 已经在*.obj中定义
为什么会出现这个错误??“error LNK2005: 已经在*.obj中定义” 编程中经常能遇到LNK2005错误——重复定义错误,其实LNK2005错误并不是一个很难解决的错误,弄清楚它形成的原 ...
- 输入adb shell 时 提示error: more than one device and emulator
第一种情况:确实用多个设备或者模拟器 解决办法:(指定连接某一个设备或者模拟器) 1.获取模拟器/设备列表 adb devices 2.指定device来执行adb shell adb -s ...
- WPFMediaKit照相功能
最近写的一个WPF照相功能,往各位吐槽,提供优化 在WPF 设计器中添加如下代码 xmlns:wpfmedia="clr-namespace:WPFMediaKit.DirectShow.C ...
- Virtualbox虚拟机设置不完全笔记
先说说我想实现的东西,我想在虚拟机安装各种开发环境,我个人在学习Node.然后我装了一个Ubuntu Server,所以我又想共享一个windows下的文件夹,这样可以让我在windows下开发,在L ...
- 1067: [SCOI2007]降雨量 - BZOJ
Description 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例如2002,2003,2 ...
- Eclipse maven工程 Missing artifact com.sun:tools:jar:1.5.0:system 解决方法
今天同事在使用eclipse,引入一个新的maven工程时报错: Missing artifact com.sun:tools:jar:1.6.0:system 这个问题很奇怪,相同的代 ...
- 为什么数据可以从pl/sql查出来而使用ado.net查询,结果却是空?
1.背景 一条记录(如select * from A where a='1'),使用pl/sql作为条件可以查询出记录,但用ado.net sql查询结果却是空. 2.原因 a字段的数据类型的char ...
- BestCoder Round #2
TIANKENG’s restaurant http://acm.hdu.edu.cn/showproblem.php?pid=4883 竟然暴力1.44*10^7 还要*T=100 竟然过了 # ...
- 使用文本文件(.txt)进行数据存取的技巧总结(相当的经典)
使用文本文件(.txt)进行数据存取的技巧总结(相当的经典) 使用文本文件(.txt)进行数据存取的技巧总结 由于本帖内容较多,部分转自他人的心得,因此,凡转贴的地方仅用“----转----”标注,原 ...
- .net web部署(IIS Express && Nancy Self-Hosting)
http://d.hatena.ne.jp/fkmt5/20140330/1396195246 [1]Nancy Web配置注意事项 添加url:netsh http add urlacl url=h ...