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 题意 你可以对一个字符串进行以下两种操 ...
随机推荐
- jQuery的隔行换色
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 「Neerc2016」Expect to Wait
题目描述 ls最近开了一家图书馆,大家听说是ls开的,纷纷过来借书,自然就会出现供不应求的情况, 并且借书的过程类 似一个队列,每次有人来借书就将它加至队尾,每次有人来还书就把书借给队头的若干个人,定 ...
- 2018青岛网络赛G - Couleur 区间上的启发式合并
题意:给出\(a[1...n]\),共\(n\)次操作,每次删除一个位置\(p_i\)(强制在线),此时区间会变为两个分离的区间,求每次操作的最大区间逆序对 首先要知道必要的工具,按权值建立的主席树可 ...
- MySQL自定义排序
存在表A 按名字倒序排 SELECT * FROM A ORDER BY name DESC 结果如下: 若需要按照王五.张三.李四的顺序排序,使用自定义排序:FIELD() SELEC ...
- (转)linux运维必会MySQL企业面试题
linux运维必会MySQL企业面试题 老男孩教育运维班全体学员MySQL必会企业面试题最实战.最细致.最落地的运维实训基地,老男孩教育连续多年国内平均就业工资最高! 数据库的重要性是所有技术里最核 ...
- 【Ubuntu】安装配置apahce
安装apachesudo apt-get install apache2 安装目录 /etc/apache2配置文件 /etc/apache2/apache2.conf默认网站根目录 /var/www ...
- php 如何截取中文字符串
在网站应用中时常需要对相应的字符串进行截取.最常用的是使用substr函数对字符串进行截取. 然而,substr和strlen函数只在处理英文字符串时可以正确使用,在截取中文字符时,时常出现乱码.这时 ...
- echart 时间轴、以及y轴值过大但是变化不大显示感觉不出变化的问题+弹出框拖动div事件
1.时间轴 echart 提供了一种图表,如果x轴是一个时间范围,并且是连续的,如果用传统的数据驱动会很慢,所以用时间轴的方式 function initCurve(_data){ var resul ...
- manjaro 清理系统
sudo pacman -Rsn $(pacman -Qdtq) sudo pacman -Scc sudo rm /var/lib/systemd/coredump/. sudo journalct ...
- python2和python3中列表推导式的变量泄露问题
Python 2.x 中,在列表推导中 for 关键词之后的赋值操作可能会影响列表推导上下文中的同名变量.像下面这个 Python 2.7 控制台对话: Python 2.7.15 (default, ...