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种,这些由这些 ...
随机推荐
- Java迭代器的用法【转】
迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为“轻量级”对象,因为创建它的代价小. Java中的I ...
- Unity项目学习笔记
1.TCP和IP IP:主要作用是在复杂的网络环境中将数据包发送给的最终的目标地址. 端口号:系统会分给系统端口号 一般知名的端口号在0-1023之间,而我们经常使用的自定义/动态分配的端口号则一般 ...
- poj2677 Tour
题意: 双调欧几里得旅行商问题. 思路: dp.定义dp[i][j](i <= j)为从点j从右向左严格按照x坐标递减顺序走到点1,之后再从点1从左向右严格按照x坐标递增的顺序走到点i,并且在此 ...
- canvas绘制基础
初始接口 <body> <canvas id=“canvas”></canvas> <script> var canvas = document.get ...
- jdk1.8新日期时间类(DateTime、LocalDateTime)demo代码
//获取当前时间 LocalDateTime d0 = LocalDateTime.now(); System.out.println(DataConvertUtil.localDateTimeToS ...
- Eclipse 闪退/无法启动/一闪而过打解决办法
解决方法 删除文件:/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi 经过实际应用真实有效.
- jdbc获取数据具体过程
下面是个最简单的使用jdbc取得数据的应用.在例子之后我将分成4步,分别是①取得连接,②创建PreparedStatement,③设置参数,④执行查询,来分步分析这个过程.除了设置参数那一步之外,其他 ...
- iTOP-iMX6开发板Android系统下LVDS和HDMI双屏异显方法
迅为iMX6 开发板 android 系统下 LVDS 和 HDMI 双屏异显的使用过程. 注意,iTOP-iMX6 开发板的 android 系统想要实现对 LVDS 和 HDMI 双屏异显功能的支 ...
- SQL使用exists时的多种写法
from test; go from test; go 下面这种效率明显高不少.
- QT+创建两个不相干的窗口实现一个显示一个不显示
因为两个窗口互不相干,所以需要重新创建一个窗口类subWidget subWidget.cpp文件 #ifndef SUBWIDGET_H #define SUBWIDGET_H #include & ...