Bzoj4044 Virus synthesis
题意
你要用 \(ATGC\) 四个字母用两种操作拼出给定的串:
- 将其中一个字符放在已有串开头或者结尾
- 将已有串复制,然后 \(reverse\) ,再接在已有串的头部或者尾部
一开始已有串为空。求最少操作次数。
\(len\le100000\)
Sol
首先有个结论
每次形成偶数长度回文串的最后一步一定是操作 \(2\)
那么考虑一个 \(DP\)
设 \(f[i]\) 表示形成 \(i\) 表示的字符串需要的最少步数
可以去掉首和尾转移来,可以由它的一个前缀或者后缀转移来
如果是个偶数长度的字符串
可以由某个长度小于等于它一半的字符串增长到它的长度后翻倍而来
可以由它去掉首尾的串一步转移而来,因为去掉首位仍然是偶数长度而且形成偶数长度回文串的最后一步一定是操作 \(2\)
那么直接用回文树实现
求某个长度小于等于它一半的字符串直接建树的时候暴力跳一下(雾
# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
const int maxn(1e5 + 5);
int f[maxn], son[4][maxn], trans[666], half[maxn], len[maxn], fa[maxn], num[maxn], tot, last, pre[maxn];
char s[maxn];
IL void Init(){
for(RG int i = 0; i <= tot; ++i){
len[i] = fa[i] = half[i] = 0;
for(RG int j = 0; j < 4; ++j) son[j][i] = 0;
}
fa[0] = fa[1] = 1, len[1] = -1, tot = 1, last = 0;
}
IL void Extend(RG int pos, RG int c){
RG int p = last;
while(s[pos - len[p] - 1] != s[pos]) p = fa[p];
if(!son[c][p]){
RG int np = ++tot, q = fa[p];
while(s[pos - len[q] - 1] != s[pos]) q = fa[q];
len[np] = len[p] + 2, fa[np] = son[c][q];
son[c][p] = np, pre[np] = p;
if(s[pos - len[half[p]] - 1] == s[pos]) half[np] = son[c][half[p]];
else half[np] = fa[np];
while((len[half[np]] << 1) > len[np]) half[np] = fa[half[np]];
}
last = son[c][p];
}
int main(){
trans['C'] = 1, trans['G'] = 2, trans['T'] = 3;
for(RG int t = Input(), n = 0; t; --t){
Init(), scanf(" %s", s + 1), n = strlen(s + 1);
for(RG int i = 1; i <= n; ++i) Extend(i, trans[s[i]]);
RG int ans = n;
for(RG int i = 2; i <= tot; ++i){
f[i] = min(len[i], f[fa[i]] + len[i] - len[fa[i]]);
if(len[i] & 1) f[i] = min(f[pre[i]] + 2, f[i]);
else{
f[i] = min(f[i], pre[i] ? f[pre[i]] + 1 : 2);
f[i] = min(f[i], f[half[i]] + (len[i] >> 1) - len[half[i]] + 1);
}
ans = min(ans, f[i] + n - len[i]);
}
printf("%d\n", ans);
}
return 0;
}
Bzoj4044 Virus synthesis的更多相关文章
- [BZOJ4044]Virus synthesis 回文自动机的DP
4044: [Cerc2014] Virus synthesis Time Limit: 20 Sec Memory Limit: 128 MB Description Viruses are us ...
- bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp)
bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp) bzoj Luogu 你要用ATGC四个字母用两种操作拼出给定的串: 1.将其中一个字符 ...
- luogu_4762: [CERC2014]Virus synthesis
洛谷_4762:[CERC2014]Virus synthesis 题目描述: 初始有一个空串,利用下面的操作构造给定串\(S\).\(len(S)\leq10^5\) 1: 串开头或末尾加一个字符. ...
- LG4762 Virus synthesis
Virus synthesis 初始有一个空串,利用下面的操作构造给定串 S . 串开头或末尾加一个字符 串开头或末尾加一个该串的逆串 求最小化操作数, ∣S∣≤105 . 题解 显然应该多使用操作2 ...
- [CERC2014]Virus synthesis【回文自动机+DP】
[CERC2014]Virus synthesis 初始有一个空串,利用下面的操作构造给定串 SS . 1.串开头或末尾加一个字符 2.串开头或末尾加一个该串的逆串 求最小化操作数, \(|S| \l ...
- bzoj4044 [Cerc2014] Virus synthesis
回文自动机上dp f[x]表示形成x代表的回文串所需的最小步数, 若len[x]为奇数,f[x]=len[x],因为即使有更优的,也是直接添加,没有复制操作,那样就不用从x转移了. 若len[x]为偶 ...
- BZOJ4044: [Cerc2014] Virus synthesis(回文树+DP)
Description Viruses are usually bad for your health. How about fighting them with... other viruses? ...
- [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 题意 你可以对一个字符串进行以下两种操 ...
随机推荐
- 根据现有PDF模板填充信息(SpringBoot)
根据现有PDF模板填充信息(SpringBoot+maven) 首先得有一个pdf模板,建立pdf模板需要下载工具 红色框为文本框,filename为域名.java需要根据域名赋值 pom 文件配置 ...
- Python 的 GIL 是什么鬼,多线程性能究竟如何
作者:卢钧轶(cenalulu) 本文原文地址: http://cenalulu.github.io/python/gil-in-python/ 前言:博主在刚接触Python的时候时常听到GIL这个 ...
- JavaScript的几种(原型)继承
定义Foo,Bar 其中,Bar继承Foo a是Bar的实例,包含有Foo和Bar的函数和属性: function Foo(name) { this.name = name; } Foo.protot ...
- POJ 2192
#include <iostream> #include <string> #define MAXN 500 using namespace std; bool dp[MAXN ...
- MIME类型是什么?包含哪些类型?
摘自:http://www.tuidc.com/idczixun/newsx/newsidc/3479.html 问:MIME类型是什么? 答:MIME(Multipurpose Interne ...
- Hibernate3.3.2_JUnit_BoforeClass不报异常的Bug处理
假如你把配置文件写错了,myeclipse竟然不报错,只说sf空指针. <mapping class="com.oracle.hibernate.model."/> / ...
- JS检测数据类型
如果你要判断的是基本数据类型或JavaScript内置对象,使用toString: 如果要判断的时自定义类型,请使用instanceof. 1.typeof typeof操作符返回的是类型字符串,它的 ...
- tomcat监控工具probe
probe官网:http://www.lambdaprobe.org/ 但是已经链接至github了:https://github.com/psi-probe/psi-probe 下载psi-prob ...
- javac的访问者模式
这一篇重点来总结下javac的访问者模式,其定义的访问者接口为JCTree.Visitor,具体如下: /** A generic visitor class for trees. */ public ...
- JavaScript数据结构-10.字典
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...