Virus synthesis

初始有一个空串,利用下面的操作构造给定串 S 。

  1. 串开头或末尾加一个字符
  2. 串开头或末尾加一个该串的逆串

求最小化操作数, ∣S∣≤105

题解

显然应该多使用操作2——翻转复制。

建出 S 的回文自动机,设 dp(i) 表示构造节点 i 表示回文串所需最少操作次数。

ans=min {dp(i)+n-leni}

若 i 能转移到 j,则 dp(j)=dp(i)+1。因为 i 是回文串,所以 i 一定是由翻转复制得到的。在这之前一步加上 j 的字符就是这个转移的意义。

回文自动机维护出 half,那么 dp(i)=dp(halfi)+leni/2-lenhalfi+1。这个转移意义很明显。

发现转移需要回文自动机的 DAG 的更新顺序,所以使用队列维护。

时间复杂度 \(O(|S|)\)。

co int N=100000+10;
char s[N];
il int idx(char c){
switch(c){
case 'A':return 0;
case 'G':return 1;
case 'C':return 2;
default:return 3;
}
} int last,tot;
int ch[N][4],fa[N],len[N],half[N]; int get_fa(int x,int i){
while(s[i-len[x]-1]!=s[i]) x=fa[x];
return x;
}
void extend(int i){
int p=get_fa(last,i);
int x=ch[p][idx(s[i])];
if(!x){
x=++tot,memset(ch[x],0,sizeof ch[x]);
fa[x]=ch[get_fa(fa[p],i)][idx(s[i])];
len[x]=len[p]+2;
ch[p][idx(s[i])]=x;
if(len[x]==1) half[x]=0;
else{
int q=half[p];
while(s[i-len[q]-1]!=s[i]||(len[q]+2)<<1>len[x]) q=fa[q];
half[x]=ch[q][idx(s[i])];
}
}
last=x;
} int dp[N];
void real_main(){
last=tot=1;
memset(ch[0],0,sizeof ch[0]),memset(ch[1],0,sizeof ch[1]);
fa[0]=fa[1]=1,len[1]=-1; scanf("%s",s+1);int n=strlen(s+1);
for(int i=1;i<=n;++i) extend(i); dp[0]=1; // ""->"a,a" : cost=2
for(int i=2;i<=tot;++i) dp[i]=len[i];
deque<int> q(1,0);
int ans=n;
while(q.size()){
int p=q.front();q.pop_front();
for(int c=0;c<4;++c){
int x=ch[p][c];
if(!x) continue;
dp[x]=dp[p]+1;
int y=half[x];
dp[x]=min(dp[x],dp[y]+(len[x]>>1)-len[y]+1);
ans=min(ans,dp[x]+n-len[x]);
q.push_back(x);
}
}
printf("%d\n",ans);
}
int main(){
for(int T=read<int>();T--;) real_main();
return 0;
}

LG4762 Virus synthesis的更多相关文章

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

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

  2. luogu_4762: [CERC2014]Virus synthesis

    洛谷_4762:[CERC2014]Virus synthesis 题目描述: 初始有一个空串,利用下面的操作构造给定串\(S\).\(len(S)\leq10^5\) 1: 串开头或末尾加一个字符. ...

  3. [CERC2014]Virus synthesis【回文自动机+DP】

    [CERC2014]Virus synthesis 初始有一个空串,利用下面的操作构造给定串 SS . 1.串开头或末尾加一个字符 2.串开头或末尾加一个该串的逆串 求最小化操作数, \(|S| \l ...

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

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

  5. bzoj4044 [Cerc2014] Virus synthesis

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

  6. [CERC2014] Virus synthesis

    设f[i]为形成极长回文串i的最小操作数.答案为min f[i]+n-len[i]. 在不形成偶回文的情况下形成奇回文的最小操作数为该串长度.可以不考虑(但ans赋为len). 正确性基于: 1)奇. ...

  7. Codeforces Gym100543G Virus synthesis 字符串 回文自动机 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF-100543G.html 题目传送门 - CF-Gym100543G 题意 你可以对一个字符串进行以下两种操 ...

  8. bzoj 4044 Virus synthesis - 回文自动机 - 动态规划

    题目传送门 需要高级权限的传送门 题目大意 要求用两种操作拼出一个长度为$n$的只包含'A','T','G','C'的字符串 在当前字符串头或字符串结尾添加一个字符 将当前字符串复制,将复制的串翻转, ...

  9. Bzoj4044 Virus synthesis

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

随机推荐

  1. docker+k8s基础篇二

    Docker+K8s基础篇(二) docker的资源控制 A:docker的资源限制 Kubernetes的基础篇 A:DevOps的介绍 B:Kubernetes的架构概述 C:Kubernetes ...

  2. 【转帖】漏洞数量242:15,英特尔和AMD CPU谁更安全?

    漏洞数量242:15,英特尔和AMD CPU谁更安全? http://www.eetop.cn/cpu_soc/6946340.html 越来越多的用户开始怀疑哪种处理器可以最好地保护他们的计算机,数 ...

  3. Prometheus入门到放弃(5)之AlertManager部署

    alertmanager与exporters.cadvisor一样,都是独立于prometheus项目,这里我们也使用docker方式部署alertmanager. 1.下载镜像 镜像地址:https ...

  4. unexpected end of file while looking for precompiled headerdirective Add directive to 'stdafx.h' or rebuild precompiled header错误

    解决方式: 项目工程右键->propertity(属性),选择不用于预编译头 原因: C++的编译过程如下: 当头文件很多时,预编译过程需要耗费大量时间,为了减少重复编译的次数,C和C++提供了 ...

  5. day46——特殊符号、标签分类、标签

    day46 特殊符号  --空格 >大于号 <小于号 ... 找HTML特殊符号 标签分类 块级标签(行外标签):独占一行,可以包含内敛标签和某些块级标签,div,p,h1-h6,hr,f ...

  6. mysql删除字符串的前后的空格

    update table set field = replace(replace(replace(field,char(9),''),char(10),''),char(13),'');

  7. Elasticsearch-6.7.0系列(六)ES设置集群密码

    感谢此老兄:<手把手教你搭建一个 Elasticsearch 集群> 前提准备 安装kibana-6.7.0: <Elasticsearch-6.7.0系列(三)5601端口 kib ...

  8. Exception: HTTP 599: SSL certificate problem: unable to get local issuer certificate 解决办法

    使用Pyspider中报此错误. 错误原因: 这个错误会发生在请求 https 开头的网址,SSL 验证错误,证书有误. 解决方法: 使用self.crawl(url, callback=self.i ...

  9. [洛谷P3227][HNOI2013]切糕

    题目大意:有一个$n\times m$的切糕,每一个位置的高度可以在$[1,k]$之间,每个高度有一个代价,要求四联通的两个格子之间高度最多相差$D$,问可行的最小代价.$n,m,k,D\leqsla ...

  10. c# NPOI文件操作

    public static Byte[] RenderDataToExcel<T>(List<T> SourceList, List<String> filter) ...