BZOJ4044: [Cerc2014] Virus synthesis 回文自动机
来源:互联网 发布:mac 查看所有进程 编辑:程序博客网 时间:2024/06/07 18:52
你要用ATGC四个字母用两种操作拼出给定的串:
1.将其中一个字符放在已有串开头或者结尾
2.将已有串复制,然后reverse,再接在已有串的头部或者尾部
一开始已有串为空。求最少操作次数。
len<=100000
考虑最后一次2操作,那之后的字符都是暴力拼上去的。那么可以枚举每一个回文子串,算出拼出它的最少次数,然后加上总长度减它的长度来更新答案。
那么考虑怎么算出每一个回文子串的最少次数。
对于一个长度为偶数的回文串:可以用反证法证明最后一步一定是2操作。那么分两种情况:2操作前向外添加字符,可由这个串去掉两端+1得到。2操作前向内添加字符,可由最长的小于等于一半长度的回文后缀+(一半长度-该后缀长度)得到。既向内又向外可归类到只向外中。
对于长度为奇数的回文串:去掉两端+2,或最长的小于等于一半长度的回文后缀+(整个长度-该后缀长度)。
如何快速找到小于等于一半长度的最长回文后缀?它在回文树上的父亲已经找过了,在此基础上接着找即可。显然最多只可能有一次长度不符合要求,所以不影响复杂度。
#include<cstdio>#include<cstring>#define gm 100500using namespace std;struct node{ node *s[4],*fail,*mat; int len,val; node(int len):s(),fail(),mat(),len(len),val(){} void* operator new(size_t); void clear() { memset(s,0,sizeof s); }};char pool[sizeof(node)*gm];node *ptr;inline void* node::operator new(size_t){return ++ptr;}char map[128];int ans;inline int min(int a,int b){return a<b?a:b;}struct PAM{ node *r,*t,*last; char *s;int n,sz; PAM(char *s):r(new node(0)),t(new node(-1)),last(r),s(s),n(0) { r->fail=r->mat=t; r->val=1; ans=sz=strlen(s+1);*s='$'; } bool push_next() { if(n==sz) return 0; char c=s[++n]=map[s[n]]; node *p=last; while(s[n]!=s[n-(p->len)-1]) p=p->fail; if(!p->s[c]) { node *q=p->s[c]=new node(p->len+2); if(p==t) { q->fail=q->mat=r; } else { node *nq=p->fail; while(s[n]!=s[n-(nq->len)-1]) nq=nq->fail; q->fail=nq->s[c]; node *np=p->mat; while(s[n]!=s[n-(np->len)-1]||(np->len+2<<1)>q->len) np=np->fail; q->mat=np->s[c]; } if(q->len&1) q->val=min(p->len+2,q->mat->val+(q->len-q->mat->len)); else q->val=min(p->val+1,q->mat->val+((q->len>>1)-q->mat->len)+1); ans=min(ans,q->val+(sz-q->len)); } last=p->s[c]; return 1; }};int T;char s[gm];int main(){ map['A']=0,map['T']=1,map['G']=2,map['C']=3; scanf("%d",&T); while(T--) { scanf("%s",s+1); ptr=(node*)pool; PAM pam(s); while(pam.push_next()); printf("%d\n",ans); } return 0;}
0 0
- Bzoj4044:[Cerc2014] Virus synthesis:回文自动机+DP
- 【BZOJ4044】[Cerc2014] Virus synthesis【回文自动机】
- BZOJ4044: [Cerc2014] Virus synthesis 回文自动机
- 【CERC2014】bzoj4044 Virus synthesis
- [BZOJ 4044][Cerc2014]Virus synthesis
- [回文自动机优化转移] Central Europe Regional Contest 2014 G. Virus synthesis
- UVALive 6933 Virus synthesis(回文树)
- Codeforces Gym 100543G Virus synthesis
- 回文自动机
- 回文自动机
- 回文自动机(回文树)
- 回文树(回文自动机)
- 回文树(回文自动机)
- ZOJ 3430 Detect the Virus AC自动机
- 【ZOJ】3430 Detect the Virus AC自动机
- Zoj 3430 Detect the Virus (AC自动机)
- zoj 3430 Detect the Virus(AC自动机)
- ZOJ 3430 Detect the Virus AC自动机
- Centos部署ffmpeg
- 关于PyCharm导入自己写的module时报错:No Module Named XXX的解决方案
- 失败的sparkSql使用问题记录
- 堆溢出利用
- Metronic4.7.5 下载 HTML5 UI后台管理框架
- BZOJ4044: [Cerc2014] Virus synthesis 回文自动机
- 面向对象成员变量和局部变量的区别
- 矩阵——hdu3306 Another kind of Fibonacci
- 数据结构与算法分析 c++11 题2.27 找K是否在N*N 矩阵中
- C语言--从指针到二级指针(下)
- LIS小程序小bug
- 我眼中的csdn
- JS原型与原型链终极详解
- 27. Remove Element