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 ;
}

最新文章

  1. eclipse项目打包
  2. October 27th Week 44th Thursday 2016
  3. mysql-函数CASE WHEN 语句使用说明
  4. 示例-创建表格&amp;使用表格对象
  5. Cubieboard2裸机开发之(五)看门狗操作
  6. access的逻辑类型
  7. iOS基础 - 第三方网络框架
  8. Android 开发 监听back并且执行home键功能
  9. 一、selenium 环境搭建
  10. Xdebug在PHP中的安装配置
  11. redis : 桌面管理工具 redis-desktop-manager使用指南
  12. pyhton框架Django之cookie和session
  13. Python3.x文件处理详解
  14. [过程记录]Centos7 下 Hadoop分布式集群搭建
  15. ubuntu(14.04) 网路管理
  16. [LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming
  17. 【jQuery源码】preFilter
  18. Geeks : Kruskal’s Minimum Spanning Tree Algorithm 最小生成树
  19. 关于phpmailer邮件发送
  20. atitit.面向过程的编程语言异常处理 c语言 asp vbs 的try catch 实现

热门文章

  1. 模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie
  2. HDU 1223 打表 + 大数
  3. P1554 梦中的统计
  4. jQuery选择器之表单元素选择器
  5. Java报表之JFreeChart
  6. ceph脚本-自动部署计算机节点
  7. Selenium2(WebDriver)开发环境搭建(java版)
  8. VCS 查看代码覆盖率
  9. IOS之pageControl
  10. SQL系列学习 基础数据