Victor and String

Time Limit: 1000ms
Memory Limit: 262144KB

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

 
解题:Palindromic Tree
 #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的更多相关文章

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

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

  2. HDU 5421 Victor and String (回文自动机)

    题目大意:让你维护一个字符串,支持在开头结尾插入字符,以及查询本质不同的回文串数量以及回文串总数量 开头结尾都维护一个$last$指针,如果插入新字符后,整个串是一个回文串,就把另一个$last$赋值 ...

  3. HDOJ 5421 Victor and String 回文串自己主动机

    假设没有操作1,就是裸的回文串自己主动机...... 能够从头部插入字符的回文串自己主动机,维护两个last点就好了..... 当整个串都是回文串的时候把两个last统一一下 Victor and S ...

  4. ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩

    HDU 5418 Victor and World Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%I64d & ...

  5. HDU 5842 Lweb and String(Lweb与字符串)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  6. hdu 4850 Wow! Such String! 欧拉回路

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4080264.html 题目链接:hdu 4850 Wow! Such String! 欧拉回 ...

  7. hdu 3553 Just a String (后缀数组)

    hdu 3553 Just a String (后缀数组) 题意:很简单,问一个字符串的第k大的子串是谁. 解题思路:后缀数组.先预处理一遍,把能算的都算出来.将后缀按sa排序,假如我们知道答案在那个 ...

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

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

  9. HDU 4850 Wow! Such String!(欧拉道路)

    HDU 4850 Wow! Such String! 题目链接 题意:求50W内的字符串.要求长度大于等于4的子串,仅仅出现一次 思路:须要推理.考虑4个字母的字符串,一共同拥有26^4种,这些由这些 ...

随机推荐

  1. 求n的因子个数与其因子数之和

    方法一:朴素算法:O(n). #include<bits/stdc++.h> using namespace std; int get_num(int n){ ; ;i<=n;++i ...

  2. 基于坐标的自动化测试神器---Total Control快速入门

    1.Total Control简单介绍 一款能够在PC上控制手机的软件,同时可以使用PC 触摸屏.鼠标.键盘, 全面操控 Android 手机,只需通过 USB 或 WiFi 连接手机至电脑,即可随时 ...

  3. A8ERP管理系统(采购单管理)

    花了一个星期的时间终于把采购模块完成了. 最近新开发的采购单管理,供大家参考学习,软件一步一步来.

  4. Masonry自动布局与UIScrolView适配

    Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS 和 Max OS X.可以通过cocoa ...

  5. iOS 自己手动添加编译警告

    文/青花瓷的平方(简书作者)原文链接:http://www.jianshu.com/p/b2e30cad2a0d著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 缘由 上一次生产环境我们 ...

  6. 8 Explicit Animations 指明的动画 笔记

    8 Explicit Animations 指明的动画 笔记     If you want something done right, do it yourself. 如果你想让事情做好,那就自动来 ...

  7. 洛谷 P1507 NASA的食物计划

    题目背景 NASA(美国航空航天局)因为航天飞机的隔热瓦等其他安 全技术问题一直大伤脑筋,因此在各方压力下终止了航天 飞机的历史,但是此类事情会不会在以后发生,谁也无法 保证,在遇到这类航天问题时,解 ...

  8. mysql查询-从表1中查询出来的结果重新插入到表1

    原有表结构 CREATE TABLE `t_card_user` ( `id` varchar(32) NOT NULL, `card_user_id` bigint(20) DEFAULT NULL ...

  9. Vue项目结构梳理

    Vue项目结构图: 简单介绍目录结构 build目录是一些webpack的文件,配置参数什么的,一般不用动 config是vue项目的基本配置文件 node_modules是项目中安装的依赖模块 sr ...

  10. Oracle————存储过程与函数

    存储过程存储过程参数模式包括IN.OUT. IN OUT. IN(默认参数模式):表示当存储过程别调用时,实参值被传递给形参:形参起变量作用,只能读该参数,而不能修改该参数.IN模式参数可以是变量或表 ...