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 ...
随机推荐
- Qt 积累
总结(-) 1> 定时器的使用 QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(u ...
- libvirt网络过滤规则简单总结
libvirt网络过滤规则, 一个过滤规则定义的示例: < filter name='no-ip-spoold'chain='ipv4' > < uuid >fce8ae33 ...
- iPhone4 降级6.12教程 无须SHSH 不装插件 不睡死[转载] by 轻鸢
无shsh降级电脑系统,细节操作等其它影响因素较多,不确保每个人都能成功,楼主发帖前刷机几十次均成功.步骤有些繁琐,按照步骤每一步都正确可保证最后不睡死 注意一下,无SHSH降级都是不完美的,开机需要 ...
- 一款纯html5实现的时钟
今天给大家分享一款非常漂亮的纯html5实现的时钟.整个界面都由html5绘制而成.一起看下效果图: 在线预览 源码下载 实现的代码. html代码: <div class="co ...
- 正则 群组 Group
static void Main(string[] args) { string strUrl = "<a href=\"user.php?act=order_detail& ...
- gpio 灯的对应关系
1 点灯验证通过: GPIO160 TX1-LED GPIO161 RX1-LED GPIO163 TX2-LED GPIO164 RX2-LED GPIO ...
- etl工具,kettle实现循环
Kettle是一款国外开源的ETL工具,纯Java编写,可以在Window.Linux.Unix上运行,绿色无需安装,数据抽取高效稳定. 业务模型: 在关系型数据库中有张很大的数据存储表,被设计成奇偶 ...
- 数学 - Codeforces Round #319 (Div. 1)A. Vasya and Petya's Game
Vasya and Petya's Game Problem's Link Mean: 给定一个n,系统随机选定了一个数x,(1<=x<=n). 你可以询问系统x是否能被y整除,系统会回答 ...
- selenium运行火狐报错FirefoxDriver : Unable to connect to host 127.0.0.1 on port 7055
摘要: 这是个常见的启动firefoxdriver的问题,具体的错误日志如下,其实原因很简单,就是你的Selenium版本和firefox 不兼容了. Firefox 版本太高了, 请及时查看你安装的 ...
- JSP 页面中用绝对路径显示图片
首先,图片和工程不在一个盘符下.图片也不能放到工程下. 在JSP 文件中 <img src="E:/图片/1.jpg"/> 这样是引不到图片的.因为,JSP页面在引 ...