Victor and String[Bestcoder #52 1004](回文树)
题目描述
Victor喜欢玩字符串。他认为一个字符串是迷人的,当且仅当字符串是回文的。
Victor想玩n次。每次他都会做以下四种操作中的一种。
操作1:在字符串的开头添加一个字符 c。
操作2:在字符串的末尾添加一个字符 c。
操作3:询问不同的迷人子字符串的数量。
操作4:询问迷人子字符串的数量,必须计算从不同位置开始的相同子字符串的数量。
在一开始Victor有一个空字符串。
输入格式
输入包含几个测试用例,最多5个用例。 在每个测试用例中,第一行一个整数n表示操作的数量。
下面n行的第一个数字是整数op,表示操作的类型。如果op=1或2,后面会有一个小写的英文字母。
输出格式
对于每个查询操作(操作3或4),输出正确的答案。
样例
输入样例1
6
1 a
1 b
2 a
2 c
3
4
8
1 a
2 a
2 a
1 a
3
1 b
3
4
输出样例2
4
5
4
5
11
这是一道模板题,直接上代码。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int N = ;
struct Plalindrome_Tree{
int go[], len, fail;
ll fail_len;
}pt[N];
int pre, suf, tot;
ll ans;
int q;
int l, r;
char s[N];
void init() {
memset(s, -, sizeof(s));
memset(pt, , sizeof(pt));
tot = ans = ;
pre = suf = ;
l = ;
r = ;
}
void build() {
pt[++tot].len = -;
pt[].fail = pt[].fail = ;
pt[].fail_len = ;
pt[].fail_len = ;
}
void add_back(int c) {
int p = suf;
while (s[r - pt[p].len - ] != s[r]) p = pt[p].fail;
if (!pt[p].go[c]) {
int v = ++tot, k = pt[p].fail;
pt[v].len = pt[p].len + ;
while (s[r - pt[k].len - ] != s[r]) k = pt[k].fail;
pt[v].fail = pt[k].go[c];
pt[v].fail_len = pt[pt[k].go[c]].fail_len + ;
pt[p].go[c] = v;
}
suf = pt[p].go[c];
}
void add_front(int c) {
int p = pre;
while (s[l + pt[p].len + ] != s[l]) p = pt[p].fail;
if (!pt[p].go[c]) {
int v = ++tot, k = pt[p].fail;
pt[v].len = pt[p].len + ;
while (s[l + pt[k].len + ] != s[l]) k = pt[k].fail;
pt[v].fail = pt[k].go[c];
pt[v].fail_len = pt[pt[k].go[c]].fail_len + ;
pt[p].go[c] = v;
}
pre = pt[p].go[c];
}
int main() {
while (cin >> q) {
init();
build();
while (q--) {
int opt;
char ch;
cin >> opt;
switch (opt) {
case :
cin >> ch;
s[--l] = ch;
add_front(ch - 'a');
ans += pt[pre].fail_len - ;
if (pt[pre].len == r - l + ) suf = pre;
break;
case :
cin >> ch;
s[++r] = ch;
add_back(ch - 'a');
ans += pt[suf].fail_len - ;
if (pt[suf].len == r - l + ) pre = suf;
break;
case :
cout << tot - ;
puts("");
break;
case :
cout << ans;
puts("");
break;
default:
puts("Wrong Input");
break;
}
} }
return ;
}
Victor and String[Bestcoder #52 1004](回文树)的更多相关文章
- Victor and String HDU - 5421 双向回文树
题意: 有n种操作,开始给你一个空串,给你4中操作 1 c 在字符串的首部添加字符c 2 c 在字符串的尾部添加字符c 3 询问字符中的本质不同的回文串的个数 4 询问字符串中回文串的个数 思路 ...
- 【HDU5421】Victor and String(回文树)
[HDU5421]Victor and String(回文树) 题面 Vjudge 大意: 你需要支持以下操作: 动态在前端插入一个字符 动态在后端插入一个字符 回答当前本质不同的回文串个数 回答当前 ...
- HDU 5421 Victor and String(回文树)
Victor and String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/262144 K (Java/Othe ...
- HDU - 5421:Victor and String (回文树,支持首尾插入新字符)
Sample Input 6 1 a 1 b 2 a 2 c 3 4 8 1 a 2 a 2 a 1 a 3 1 b 3 4 Sample Output 4 5 4 5 11 题意:多组输入,开始字符 ...
- hdu5421 Victor and String 回文树(前后插入)
题目传送门 题意:对一个字符串支持四种操作,前插入字符,后插入字符,询问本质不同的回文串数量和所有回文串的数量. 思路: 就是在普通回文树的基础上,维护suf(最长回文后缀)的同时再维护一个pre(最 ...
- HDU 5157 Harry and magic string(回文树)
Harry and magic string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树
签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...
- HDU 6599 I Love Palindrome String (回文树+hash)
题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...
- 2019 徐州网络赛 G Colorful String 回文树
题目链接:https://nanti.jisuanke.com/t/41389 The value of a string sss is equal to the number of differen ...
随机推荐
- Python中的浅复制、深复制
参考 https://docs.python.org/3/library/copy.html?highlight=copy%20copy#copy.copy https://en.wikipedia. ...
- XMPPFramework 框架
https://blog.csdn.net/qq_29846663/article/details/70170646 2017-04-14 11:37:02 于海明 阅读数 478更多 分类专栏: i ...
- JavaSE复习~开发环境的搭建 与 HelloWorld
JDK的下载 访问Oracle官网,下载jdk,目前来说用的最多的是 8 版本 https://www.oracle.com/technetwork/java/javase/downloads/ind ...
- 创业学习---今日头条创业过程分析---HHR计划
本文搜集和整理了今日头条创业的一些关键点的资料------by 春跃(本文的主要观点都是搜集整理,所以不得本人同意不得转载) 一,18年之前的今日头条创业时间表: 1,张一鸣参与创业的履历:酷讯,饭否 ...
- 【转】弹出USB大容量存储设备时出问题的解决方法
原文链接 如下图所示,这个问题,相信很多人都有遇到过,而且经常难以解决,试了很多方法都无效.到最后,只能抱着侥幸的心理直接拔出,如果运气好,可能没有事,如果运气不好,你的U盘或者移动硬盘就要从此报废了 ...
- 分布式应用监控:SkyWalking 快速接入实践
分布式应用,会存在各种问题.而要解决这些难题,除了要应用自己做一些监控埋点外,还应该有一些外围的系统进行主动探测,主动发现. APM工具就是干这活的,SkyWalking 是国人开源的一款优秀的APM ...
- 神奇的 SQL 之 联表细节 → MySQL JOIN 的执行过程
问题背景 对于 MySQL 的 JOIN,不知道大家有没有去想过他的执行流程,亦或有没有怀疑过自己的理解(自信满满的自我认为!):如果大家不知道怎么检验,可以试着回答如下的问题 驱动表的选择 MySQ ...
- jqgrid自适应宽度
https://blog.csdn.net/duzhanxiaosa/article/details/78922660
- iOS之Xcode提交App中断出现:Cannot proceed with delivery: an existing transporter instance is currently uploading this package
https://www.jianshu.com/p/6d465a0ea58e 这句英文翻译过来就是: 无法继续交付:现有的传输程序实例目前正在上载此包 原因:上传的动作被记录在UploadToken中 ...
- P1017进制转化
P1017进制转化 也不知道为啥,这么简单的题困扰了我这么长时间 #include<cstdio> using namespace std; int m; //被除数= 除数*商 + 余数 ...