题目描述

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](回文树)的更多相关文章

  1. Victor and String HDU - 5421 双向回文树

    题意: 有n种操作,开始给你一个空串,给你4中操作 1 c  在字符串的首部添加字符c 2 c  在字符串的尾部添加字符c 3  询问字符中的本质不同的回文串的个数 4 询问字符串中回文串的个数 思路 ...

  2. 【HDU5421】Victor and String(回文树)

    [HDU5421]Victor and String(回文树) 题面 Vjudge 大意: 你需要支持以下操作: 动态在前端插入一个字符 动态在后端插入一个字符 回答当前本质不同的回文串个数 回答当前 ...

  3. HDU 5421 Victor and String(回文树)

    Victor and String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/262144 K (Java/Othe ...

  4. 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 题意:多组输入,开始字符 ...

  5. hdu5421 Victor and String 回文树(前后插入)

    题目传送门 题意:对一个字符串支持四种操作,前插入字符,后插入字符,询问本质不同的回文串数量和所有回文串的数量. 思路: 就是在普通回文树的基础上,维护suf(最长回文后缀)的同时再维护一个pre(最 ...

  6. HDU 5157 Harry and magic string(回文树)

    Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  7. The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树

    签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...

  8. HDU 6599 I Love Palindrome String (回文树+hash)

    题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...

  9. 2019 徐州网络赛 G Colorful String 回文树

    题目链接:https://nanti.jisuanke.com/t/41389 The value of a string sss is equal to the number of differen ...

随机推荐

  1. 总结 jion,group join 基于方法的查询与查询表达式 对比

    数据源: 代码: using (tempdbEntities context = new tempdbEntities()) { #region 基于方法的查询 Console.WriteLine(& ...

  2. windows ,linux永久和临时修改pip源

    临时修改(建议)pypi镜像源方法:如果有untrust 报错,可使用https开头的网站,或加上--trusted 例如: pip install pywin32 -i http://mirrors ...

  3. MySQL死锁1

    MySQL行级排他锁的使用及死锁解除技巧 这篇笔记存粹是做学习记录之用,方便将来查阅,老鸟请跳过.关于MySQL排他锁的具体使用. 使用排他锁 假设有一张user表如下: id name age 1 ...

  4. 操作系统OS - 阻塞(Blocking)非阻塞(Non-Blocking)与同步(Synchronous)异步(Asynchronous)

    参考: http://blog.jobbole.com/103290/ https://www.zhihu.com/question/19732473/answer/23434554 http://b ...

  5. Java单例和多例

    背景:最近在学习韩老师的笔记时候发现不是很了解单例和多例,于是通过网上查找资料的方式去学习. 设计模式:最佳的实践,是软件开发人员在软件开发过程中面临一般解决方案,也就是开发的经验总结. 单例模式(S ...

  6. 激活win10企业版,亲测可用,(win7步骤相同,请自行测试)

    其他版本我没试过,亲们可以尝试! win7神key win7神key1:2HYJ4-V71WM-BAF6Y-G2BTH-X8QOD win7神key2:9LM54-Z3LQ1-5IRAN-T4JNI- ...

  7. 【洛谷P3500】TES-Intelligence Test

    前言 先是这位神仙写了这道题 \(O(n\log n)\) 的做法.然后去他的博客上恰了一波. 然后发现这道题有 \(O(n)\) 的做法的.其实也不难. 题目 题目链接:https://www.lu ...

  8. @ResponseBody是如何起作用的

    前言 最近参与的项目中,接口中返回的日期格式不对,发现项目中配置了fastjson作为spring的数据转换器,于是使用了fastjson的字段格式化转换注解 发现不起作用.这让我很疑惑,然后在fas ...

  9. 导入jeesite 项目

    1:从开源中国用git方式下载jeesite源码 链接https://gitee.com/thinkgem/jeesite    gti 地址:https://gitee.com/thinkgem/j ...

  10. Nginx Rewrite域名及资源重定向!(重点)

    第一步:搭建Nginx服务 第二步:修改主配置文件 [root@ns2 ~]# vim /usr/local/nginx/conf/nginx.conf user  nginx nginx; work ...