HDU 5421 Victor and String
Victor and String
This problem will be judged on HDU. Original ID: 5421
64-bit integer IO format: %I64d Java class name: Main
Victor loves to play with string. He thinks a string is charming as the string is a palindromic string.
Victor wants to play n times. Each time he will do one of following four operations.
Operation 1 : add a char c to the beginning of the string.
Operation 2 : add a char c to the end of the string.
Operation 3 : ask the number of different charming substrings.
Operation 4 : ask the number of charming substrings, the same substrings which starts in different location has to be counted.
At the beginning, Victor has an empty string.
Input
The input contains several test cases, at most 5 cases.
In each case, the first line has one integer n means the number of operations.
The first number of next n line is the integer op, meaning the type of operation. If op=1 or 2, there will be a lowercase English letters followed.
1≤n≤100000.
Output
For each query operation(operation 3 or 4), print the correct answer.
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
Source
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int maxn = ;
struct PalindromicTree {
int son[maxn][],fail[maxn],num[maxn],s[maxn],len[maxn];
int tot,L,R,last[];
LL ret;
int newnode(int slen = ) {
num[tot] = ;
memset(son[tot],,sizeof son[tot]);
len[tot] = slen;
return tot++;
}
void init() {
ret = tot = last[] = last[] = ;
L = maxn>>;
R = L - ;
newnode();
newnode(-);
fail[] = fail[] = ;
memset(s,-,sizeof s);
}
int getFail(int kd,int x) {
if(kd) while(s[R - len[x] -] != s[R]) x = fail[x];
else while(s[L + len[x] + ] != s[L]) x = fail[x];
return x;
}
void extend(int kd,int c) {
if(kd) s[++R] = c;
else s[--L] = c;
int cur = getFail(kd,last[kd]);
if(!son[cur][c]) {
int x = newnode(len[cur] + );
fail[x] = son[getFail(kd,fail[cur])][c];
son[cur][c] = x;
num[x] = num[fail[x]] + ;
}
last[kd] = son[cur][c];
if(len[last[kd]] == R - L + ) last[kd^] = last[kd];
ret += num[last[kd]];
}
} pt;
int main() {
int n,op;
char str[];
while(~scanf("%d",&n)) {
pt.init();
while(n--) {
scanf("%d",&op);
if(op <= ) {
scanf("%s",str);
pt.extend(op-,str[] - 'a');
} else if(op == ) printf("%d\n",pt.tot - );
else if(op == ) printf("%I64d\n",pt.ret);
}
}
return ;
}
HDU 5421 Victor and String的更多相关文章
- 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 (回文自动机)
题目大意:让你维护一个字符串,支持在开头结尾插入字符,以及查询本质不同的回文串数量以及回文串总数量 开头结尾都维护一个$last$指针,如果插入新字符后,整个串是一个回文串,就把另一个$last$赋值 ...
- HDOJ 5421 Victor and String 回文串自己主动机
假设没有操作1,就是裸的回文串自己主动机...... 能够从头部插入字符的回文串自己主动机,维护两个last点就好了..... 当整个串都是回文串的时候把两个last统一一下 Victor and S ...
- ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩
HDU 5418 Victor and World Time Limit:2000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
- HDU 5842 Lweb and String(Lweb与字符串)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- hdu 4850 Wow! Such String! 欧拉回路
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4080264.html 题目链接:hdu 4850 Wow! Such String! 欧拉回 ...
- hdu 3553 Just a String (后缀数组)
hdu 3553 Just a String (后缀数组) 题意:很简单,问一个字符串的第k大的子串是谁. 解题思路:后缀数组.先预处理一遍,把能算的都算出来.将后缀按sa排序,假如我们知道答案在那个 ...
- 【HDU5421】Victor and String(回文树)
[HDU5421]Victor and String(回文树) 题面 Vjudge 大意: 你需要支持以下操作: 动态在前端插入一个字符 动态在后端插入一个字符 回答当前本质不同的回文串个数 回答当前 ...
- HDU 4850 Wow! Such String!(欧拉道路)
HDU 4850 Wow! Such String! 题目链接 题意:求50W内的字符串.要求长度大于等于4的子串,仅仅出现一次 思路:须要推理.考虑4个字母的字符串,一共同拥有26^4种,这些由这些 ...
随机推荐
- Ubuntu 安装 node
ubuntu安装node和npm的命令行命令: sudo apt install nodejs-legacy sudo apt install npm 最新版本安装方法 1.安装npm sudo ap ...
- C# HashSet 用法[转]
原文链接 .NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>.这个集合类包含不重复项的无序列表.这种集合称为“集(se ...
- 基于Java实现的冒泡排序算法
冒泡排序是一种简单基础的排序算法,相信在大学课堂里老师已经讲过了,现在我基于Java来实现一遍. 简述 冒泡排序正如其关键词一样,杂乱的气泡经过浮动,最后大的气泡飘到了上面而小的气泡在下面,无序的元素 ...
- 前端的百度地图的api的使用
1.打开百度地图官方api网页 http://lbsyun.baidu.com/ 2.点击开发文档 3.选择对应的api 4.点击DEMO详情 5.得到源码复制到你的代码中 <!DOCTYPE ...
- P3371 【模板】单源最短路径
题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出发点的编号. 接下来M行每行包含三 ...
- 网页尺寸scrollHeight/offsetHeight
scrollHeight和scrollWidth,获取网页内容高度和宽度. 一.针对IE.Opera: scrollHeight 是网页内容实际高度,可以小于 clientHeight. 二.针对NS ...
- es6语法错误
哇,今天折腾了好久解决了一个问题,记录一下. 错误: 解决方法:配置babel,将es6语法转换成es5语法 1. 全局安装babel: npm install babel-cli -g 2. 本地安 ...
- [Github筆記] 清除所有 Commit 紀錄
# 把原來的 git 移除掉 sudo rm .git -r # 初始化 git init git remote add origin https://github.com/username/repo ...
- css-test
transition-content See the Pen NLOgVR by nakata139@gmail.com (@deepblue1982) on CodePen.
- 推荐一个以动画效果显示github提交记录的黑科技工具:Gource
程序员每天都会使用到git的一系列命令.其中用git log命令可以查看提交历史记录: 今天Jerry给大家推荐一款视觉效果非常酷炫的工具,名叫Gource,是一个能够将git代码仓库的提交历史以动画 ...