UVALive 6933 Virus synthesis(回文树)
Viruses are usually bad for your health. How about ghting them with... other viruses? In this problem,
you need to nd out how to synthesize such good viruses.
We have prepared for you a set of strings of the letters A, G, T and C. They correspond to the
DNA nucleotide sequences of viruses that we want to synthesize, using the following operations:
• Adding a nucleotide either to the beginning or the end of the existing sequence.
• Replicating the sequence, reversing the copied piece, and gluing it either to the beginning or to
the end of the original (so that e.g., AGTC can become AGTCCTGA or CTGAAGTC).
We're concerned about efficiency, since we have very many such sequences, some of them very long.
Find a way to synthesize them in a minimum number of operations.
Input
The rst line of input contains the number of test cases T. The descriptions of the test cases follow:
Each test case consists of a single line containing a non-empty string. The string uses only the
capital letters `A', `C', `G' and `T' and is not longer than 100 000 characters.
Output
For each test case, output a single line containing the minimum total number of operations necessary
to construct the given sequence.
Sample Input
4
AAAA
AGCTTGCA
AAGGGGAAGGGGAA
AAACAGTCCTGACAAAAAAAAAAAAC
Sample Output
3
8
6
18
参考大牛的博客
http://www.cnblogs.com/clrs97/p/4700658.html
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <map> using namespace std;
typedef long long int LL;
const int maxn=1e5+5;
map<char,int> m;
int n;
char str[maxn];
int f[maxn];
struct Tree
{
int next[maxn][4];
int fail[maxn];
int train[maxn];
int len[maxn];
int num[maxn];
int cnt[maxn];
int s[maxn];
int last;
int p;
int n;
int new_node(int x)
{
memset(next[p],0,sizeof(next[p]));
len[p]=x;
return p++;
}
void init()
{
p=0;
new_node(0);
new_node(-1);
last=0;n=0;
s[0]=-1;
fail[0]=1;
}
int get_fail(int x)
{
while(s[n-len[x]-1]!=s[n])
x=fail[x];
return x;
}
int get_fail2(int x,int pos)
{
while(s[n-len[x]-1]!=s[n]||(len[x]+2)*2>len[pos])
x=fail[x];
return x;
}
void add(char xx)
{
int x=m[xx];
s[++n]=x;
int cur=get_fail(last);
if(!(last=next[cur][x]))
{
int now=new_node(len[cur]+2);
fail[now]=next[get_fail(fail[cur])][x];
if(len[now]<=2)
train[now]=fail[now];
else
train[now]=next[get_fail2(train[cur],now)][x]; next[cur][x]=now;
last=now;
}
}
}tree;
int ans;
int q[maxn];
int rear,head;
int main()
{
int t;
scanf("%d",&t);
m['A']=0;m['C']=1;m['G']=2;m['T']=3;
while(t--)
{
scanf("%s",str);
int len=strlen(str);
tree.init();
ans=len;
for(int i=0;i<len;i++)
{
tree.add(str[i]);
}
memset(f,0,sizeof(f));
for(int i=2;i<tree.p;i++)
{
if(tree.len[i]&1)
f[i]=tree.len[i];
}
f[0]=1;
q[0]=0;
head=0;rear=1;
int x,y;
while(head<rear)
{
x=q[head++];
for(int i=0;i<4;i++)
{
if(tree.next[x][i])
{
y=tree.next[x][i];
f[y]=f[x]+1;
f[y]=min(f[y],tree.len[y]/2-tree.len[tree.train[y]]+f[tree.train[y]]+1);
ans=min(ans,len-tree.len[y]+f[y]);
q[rear++]=y;
} }
}
printf("%d\n",ans);
}
return 0;
}
UVALive 6933 Virus synthesis(回文树)的更多相关文章
- bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp)
bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp) bzoj Luogu 你要用ATGC四个字母用两种操作拼出给定的串: 1.将其中一个字符 ...
- BZOJ 4044 Virus synthesis (回文自动机+dp)
题目大意: 你可以在一个串的开头或者末尾加入一个字符,或者把当前整个串$reverse$,然后接在前面或者后面,求达到目标串需要的最少操作次数 对目标串建出$PAM$ 定义$dp[x]$表示当前在回文 ...
- [BZOJ4044]Virus synthesis 回文自动机的DP
4044: [Cerc2014] Virus synthesis Time Limit: 20 Sec Memory Limit: 128 MB Description Viruses are us ...
- BZOJ 4044 Luogu P4762 [CERC2014]Virus Synthesis (回文自动机、DP)
好难啊..根本不会做..基本上是抄Claris... 题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=4044 (luogu) ...
- bzoj 4044 Virus synthesis - 回文自动机 - 动态规划
题目传送门 需要高级权限的传送门 题目大意 要求用两种操作拼出一个长度为$n$的只包含'A','T','G','C'的字符串 在当前字符串头或字符串结尾添加一个字符 将当前字符串复制,将复制的串翻转, ...
- bzoj 4044: Virus synthesis 回文自动机
题目大意: 你要用ATGC四个字母用两种操作拼出给定的串: 将其中一个字符放在已有串开头或者结尾 将已有串复制,然后reverse,再接在已有串的头部或者尾部 一开始已有串为空.求最少操作次数. le ...
- luogu P4762 [CERC2014]Virus synthesis (回文自动机)
大意: 初始有一个空串, 操作(1)在开头或末尾添加一个字符. 操作(2)在开头或末尾添加该串的逆串. 求得到串$S$所需最少操作数. 显然最后一定是由某个偶回文通过添加字符得到的, 那么只需要求出所 ...
- 回文树(回文自动机PAM)小结
回文树学习博客:lwfcgz poursoul 边写边更新,大概会把回文树总结在一个博客里吧... 回文树的功能 假设我们有一个串S,S下标从0开始,则回文树能做到如下几点: 1.求串S前缀0~ ...
- HDU3948 & 回文树模板
Description: 求本质不同回文子串的个数 Solution: 回文树模板,学一学贴一贴啊... Code: /*================================= # Cre ...
随机推荐
- 【Android】12.6 利用Intent实现记事本功能(NotePad)
分类:C#.Android.VS2015: 创建日期:2016-02-23 一.简介 这个例子演示如何实现一个简单的记事本功能. 该例子提前使用了后面章节将要介绍的SQLLite数据库. 二.示例-c ...
- redis基础之基本键值操作和使用(三)
前言 redis安装完毕后开始使用redis,先熟悉命令行操作. redis数据的类型 键:redis的所有的键都是string类型: 值:五种类型 string:字符串类型:一个string最大可以 ...
- 李洪强iOS经典面试题37-解释垃圾回收的原理
李洪强iOS经典面试题37-解释垃圾回收的原理 问题 我们知道,Android 手机通常使用 Java 来开发,而 Java 是使用垃圾回收这种内存管理方式. 那么,ARC 和垃圾回收对比,有什么 ...
- Redis 面试题(持续更新)
前言 看了一圈,发现Redis的面试题主要问的是如下几块: 原理 用处(缓存/队列 包括Pub.Sub/计数器/排行榜等) 基本操作与数据类型 消息队列 且与其它消息队列的区别 主从备份 宕机如何处理 ...
- web.py+fastcgi+nginx 502错误解决
用web.py照着官网在服务器上搭好了后台.这次很奇怪地出现了一个Nginx 502 Bad Gateway的错误. 执行上面的kill `pgrep -f "python /path/to ...
- 思科ACL不连续通配符掩码的计算
access-list 120 permit ip 10.0.0.0 0.0.0.191 any 这条ACL看似简单,却又复杂,因为正常我们见到的通配符掩码都是诸如0.0.0.255(255. ...
- jquery 对table的一些操作 怎么获取tr下的第二个td元素?
1.HTML结构 <table id = "test"> <tr><td>1</td><td>1</td>& ...
- [C++]在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include StdAfx.h”
问题现象:在写好的.cpp文件后,编译报错.提示"你建立的工程使用了预编译功能, cpp最前边要留一行这样的内容:#include "StdAfx.h"问题原因:网上说是 ...
- AtomicReference与volatile的区别
首先volatile是java中关键字用于修饰变量,AtomicReference是并发包java.util.concurrent.atomic下的类.首先volatile作用,当一个变量被定义为vo ...
- zabbix监控数据库
Zabbix通过percona监控MySQL 因为Zabbix自带的MySQL监控没有提供可以直接使用的Key,所以一般不采用,业界的同学们都使用Percona Monitoring Plugin ...