LG4762 Virus synthesis
Virus synthesis
初始有一个空串,利用下面的操作构造给定串 S 。
- 串开头或末尾加一个字符
- 串开头或末尾加一个该串的逆串
求最小化操作数, ∣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的更多相关文章
- [BZOJ4044]Virus synthesis 回文自动机的DP
4044: [Cerc2014] Virus synthesis Time Limit: 20 Sec Memory Limit: 128 MB Description Viruses are us ...
- luogu_4762: [CERC2014]Virus synthesis
洛谷_4762:[CERC2014]Virus synthesis 题目描述: 初始有一个空串,利用下面的操作构造给定串\(S\).\(len(S)\leq10^5\) 1: 串开头或末尾加一个字符. ...
- [CERC2014]Virus synthesis【回文自动机+DP】
[CERC2014]Virus synthesis 初始有一个空串,利用下面的操作构造给定串 SS . 1.串开头或末尾加一个字符 2.串开头或末尾加一个该串的逆串 求最小化操作数, \(|S| \l ...
- 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]为偶 ...
- [CERC2014] Virus synthesis
设f[i]为形成极长回文串i的最小操作数.答案为min f[i]+n-len[i]. 在不形成偶回文的情况下形成奇回文的最小操作数为该串长度.可以不考虑(但ans赋为len). 正确性基于: 1)奇. ...
- Codeforces Gym100543G Virus synthesis 字符串 回文自动机 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/CF-100543G.html 题目传送门 - CF-Gym100543G 题意 你可以对一个字符串进行以下两种操 ...
- bzoj 4044 Virus synthesis - 回文自动机 - 动态规划
题目传送门 需要高级权限的传送门 题目大意 要求用两种操作拼出一个长度为$n$的只包含'A','T','G','C'的字符串 在当前字符串头或字符串结尾添加一个字符 将当前字符串复制,将复制的串翻转, ...
- Bzoj4044 Virus synthesis
题意 你要用 \(ATGC\) 四个字母用两种操作拼出给定的串: 将其中一个字符放在已有串开头或者结尾 将已有串复制,然后 \(reverse\) ,再接在已有串的头部或者尾部 一开始已有串为空.求最 ...
随机推荐
- Spring中的乱码问题
最近发现一个问题, 中文编码保存到数据库里显示正确, 打印出来却是一串问号, 然后怀疑是平台默认编码的问题, locale命令显示是UTF-8正常, 然后单独编写一个java文件, 编译然后Java命 ...
- watchdog监控文件变化使用总结——转载
原文链接地址:https://blog.csdn.net/xufive/article/details/93847372 概述 首先声明,本文讨论的 watchdog,不是单片机里的 watchdog ...
- js光标定位操作
1. 自动选中区域内容 <html> <meta http-equiv="Content-Type" content="text/html; chars ...
- flink linux安装 单机版
1.下载二进制的Flink,根据你喜欢的Hadoop/Scala版本选择对应的Flink版本. https://flink.apache.org/downloads.html2.选择存放目录 解压 f ...
- 【LEETCODE】59、数组分类,适中级别,题目:39、48、64
package y2019.Algorithm.array.medium; import java.util.*; /** * @ProjectName: cutter-point * @Packag ...
- RequestBody只能读取一次的问题
一.为什么只能读一次 原因很简单:因为是流.想想看,java中的流也是只能读一次,因为读完之后,position就到末尾了. 二.解决办法 思路:第一次读的时候,把流数据暂存起来.后面需要的时候,直接 ...
- vue 项目中assets 和static的区别
一.Webpacked Assets 为了回答这个问题,我们首先需要了解Webpack如何处理静态资产.在 *.vue 组件中,所有模板和CSS都会被 vue-html-loader 及 css-lo ...
- WCF NetTcpBinding
服务端: <system.serviceModel> <bindings> <netTcpBinding> <binding portSharingEnabl ...
- NameError: name “ ” is not defined
NameError: name “ ” is not defined 问题一:name ‘name’ is not defined "name"两端是双下划线"_&quo ...
- day31-python之内置函数
1.udp多线程 import socketserver class MyServer(socketserver.BaseRequestHandler): def handle(self): prin ...