这题简直了…………

首先根据操作可知,我们肯定是先造出某个偶数长度的回文串,然后添加若干字符得到设回文串长为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的更多相关文章

  1. bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp)

    bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp) bzoj Luogu 你要用ATGC四个字母用两种操作拼出给定的串: 1.将其中一个字符 ...

  2. bzoj4044 [Cerc2014] Virus synthesis

    回文自动机上dp f[x]表示形成x代表的回文串所需的最小步数, 若len[x]为奇数,f[x]=len[x],因为即使有更优的,也是直接添加,没有复制操作,那样就不用从x转移了. 若len[x]为偶 ...

  3. [BZOJ4044]Virus synthesis 回文自动机的DP

    4044: [Cerc2014] Virus synthesis Time Limit: 20 Sec  Memory Limit: 128 MB Description Viruses are us ...

  4. Bzoj4044 Virus synthesis

    题意 你要用 \(ATGC\) 四个字母用两种操作拼出给定的串: 将其中一个字符放在已有串开头或者结尾 将已有串复制,然后 \(reverse\) ,再接在已有串的头部或者尾部 一开始已有串为空.求最 ...

  5. BZOJ4044: [Cerc2014] Virus synthesis(回文树+DP)

    Description Viruses are usually bad for your health. How about fighting them with... other viruses? ...

  6. 【XSY2534】【CF835D】Palindromic characteristics 回文自动机

    题目大意 ​ 一个字符串\(s\)是\(1\)−回文串当且仅当这个串是回文串. ​ 一个串\(s\)是\(k\)−回文串\((k>1)\)当且仅当\(s\)的前一半与后一半相同且\(s\)的前一 ...

随机推荐

  1. Daily Scrum 11.12

    摘要:本次会议继续讨论程序的问题以及单元测试和集成测试,本次测试为终审,并且本次得到的为alpha版本的最终版本.本次的Task列表如下: Task列表 出席人员 Today's Task Tomor ...

  2. DB天气app冲刺二阶段第十一天(完结)

    今天最后一天冲刺了,明天就不再冲刺了..已经把所有的技术的问题还有设计的问题都弄好了吧应该说 至少目前来说是的.因为有的实现不了的或者需要耗费时间的已经果断舍弃了,然后需要完善的也都基本完善了. 现在 ...

  3. 在SQL SErver中实现数组功能

    T-SQL象数组一样处理字符串.分割字符串    在日常的编程过程中,数组是要经常使用到的.在利用SQL对数据库进行操作时,有时就想在SQL使用数组,比如将1,2,3,4,5拆分成数组.可惜的是在T- ...

  4. C# type - IsPrimitive

    Type t = typeof(string); if (t.IsPrimitive)//not { Console.WriteLine("string is a Primitive&quo ...

  5. 1067: [SCOI2007]降雨量 - BZOJ

    Description 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例如2002,2003,2 ...

  6. shell dev null 是什么

    1:在不想把标准输出和标准出错信息输出到控制台,也不想重定向到文件时经常使用 2:不能忽略其读入功能.从/dev/null读入时都是0 3:系统的垃圾桶,类似于Windows的回收站,不同的是这个设备 ...

  7. ASP.NET MVC 4 插件化架构简单实现-实例篇

    先回顾一下上篇决定的做法: 1.定义程序集搜索目录(临时目录). 2.将要使用的各种程序集(插件)复制到该目录. 3.加载临时目录中的程序集. 4.定义模板引擎的搜索路径. 5.在模板引擎的查找页面方 ...

  8. JNI中使用cl命令生成DLL文件

    问题描述:     在使用JNI调用DLL时,首先需要生成DLL文件 问题解决:     (1)现在使用VS2008的cl.exe程序,生成DLL文件 (1.1)cl.exe环境搭建 注:   cl. ...

  9. win下Maven安装和基本设置

    注:本文介绍 Windows 平台上 Maven 的安装.Maven 3 需要运行在 JDK1.4 以上的版本上. 非原创:原创地址 http://www.ibm.com/developerworks ...

  10. GetSurfaceLevel

      if( SUCCEEDED( g_pTexture->GetSurfaceLevel( 0, &pSurface) ) )    {        pd3dDevice->Se ...