A string is palindrome, if the string reads the same backward and forward. For example, strings like "a", "aa", "appa", "queryreuq" are all palindromes.

For given empty string S, you should process following two queries :

  1. Add a lower case alphabet at the back of S.
  2. Remove a character at the back of S.

After processing a query, you should count the number of palindrome substring in S. For string S and integers i,  j (1 ≤ i ≤ j ≤ |S|), represents a substring from ith to jth character of S. You should print out the number of integer pairs (i,  j) where is palindrome.

Input

Input consists of two lines.

In the first line, Q, the number of queries is given. (1 ≤ Q ≤ 10, 000)

In the second line, the query is given as string of length Q. ith character Ki denotes the ith query.

Ki is '-' or lower case alphabet ('a', 'b', ..., 'z') (without quotes).

If the character is '-', you should remove a character at the back of S. If the character is lower case alphabet, you should add a character Ki at the back of S.

It is guaranteed that length of S is always positive after the query.

Output

Print out Q space-separated integers in the first line. i-th integer should be the answer of the ith query.

Example

Input
17
qu-uer-ryr-reu-uq
Output
1 2 1 2 3 4 3 4 5 7 5 7 9 11 9 11 13 

题意:现在有一个空的字符串S,S每次在末尾添加一个字符,或者删去末尾的字符(输入'-'号表示)。现在让你求每次操作后字符串S的回文串个数。

思路:mad,队友写了一发回文树,但是他是每次操作都暴力建树,暴力求的,所以为了在线求而不是每次重新建树,我还现学了一下回文树,挺简单的。

就是每次fail指针跑就完事了。 比后缀自动机好理解多了。 那么这个题,每次我们加入一个字符,那就加到回文树里,如果删去,则在fail链上的所有点都-1,如果-1后变为0,则删去那个点(sz标记为-1)。

但是在全部都是aaaaaaa...这种情况下,复杂度还是有点高,和暴力的复杂度差不多,复杂度小于(N^2)/4,不过N为10000可以过。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
char c[maxn],s[maxn]; int fcy[maxn],pos[maxn];
struct Palindromic_Tree
{
struct Node
{
int son[];
int fail,len;
}t[maxn];
int last,tot,sz[maxn];
void init()
{
memset(sz,-,sizeof(sz));
t[++tot].len=-; //-1是因为后面找fail是时候可以break
t[].fail=t[].fail=; //奇偶两棵树
}
int del(int Now)
{
int res=;
while(Now>){
if(sz[Now]==-) continue;
if(sz[Now]==) sz[Now]=-;
else sz[Now]--;
res++; Now=t[Now].fail;
} return res;
}
void add(int c,int n)
{
int p=pos[n-];
while(s[n-t[p].len-]!=s[n]) p=t[p].fail;
if(!t[p].son[c])
{
int v=++tot,k=t[p].fail;
t[v].len=t[p].len+;
while(s[n-t[k].len-]!=s[n]) k=t[k].fail;
t[v].fail=t[k].son[c];
t[p].son[c]=v;
sz[v]=;
}
last=t[p].son[c];
int Now=last,res=;
while(Now>){
if(sz[Now]!=-) sz[Now]++,res++;
else sz[Now]=,res++;
Now=t[Now].fail;
}
fcy[n]=res; pos[n]=last;
}
}T;
int main()
{
T.init(); int N,L=,ans=;
scanf("%d%s",&N,c+);
pos[]=;
rep(i,,N){
if(c[i]=='-'){
ans-=T.del(pos[L]); L--;
}
else {
L++; s[L]=c[i];
T.add(c[i]-'a',L);
ans+=fcy[L];
}
printf("%d ",ans);
}
return ;
}

Gym - 101806Q:QueryreuQ(回文树)的更多相关文章

  1. 回文树&后缀自动机&后缀数组

    KMP,扩展KMP和Manacher就不写了,感觉没多大意思.   之前感觉后缀自动机简直可以解决一切,所以不怎么写后缀数组.   马拉车主要是通过对称中心解决问题,有的时候要通过回文串的边界解决问题 ...

  2. 回文树 Palindromic Tree

    回文树 Palindromic Tree 嗯..回文树是个什么东西呢. 回文树(或者说是回文自动机)每个节点代表一个本质不同的回文串. 首先它类似字典树,每个节点有SIGMA个儿子,表示对应的字母. ...

  3. HDU3948 & 回文树模板

    Description: 求本质不同回文子串的个数 Solution: 回文树模板,学一学贴一贴啊... Code: /*================================= # Cre ...

  4. 2014-2015 ACM-ICPC, Asia Xian Regional Contest G The Problem to Slow Down You 回文树

    The Problem to Slow Down You Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjud ...

  5. 【CF245H】Queries for Number of Palindromes(回文树)

    [CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...

  6. 【CF17E】Palisection(回文树)

    [CF17E]Palisection(回文树) 题面 洛谷 题解 题意: 求有重叠部分的回文子串对的数量 所谓正难则反 求出所有不重叠的即可 求出以一个位置结束的回文串的数量 和以一个位置为开始的回文 ...

  7. 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

  8. 【BZOJ2160】拉拉队排练(回文树)

    [BZOJ2160]拉拉队排练(回文树) 题面 BZOJ 题解 看着题目, 直接构建回文树 求出每个回文串的出现次数 直接按照长度\(sort\)一下就行了 然后快速幂算一下答案就出来了 这题貌似可以 ...

  9. 【CF932G】Palindrome Partition(回文树,动态规划)

    [CF932G]Palindrome Partition(回文树,动态规划) 题面 CF 翻译: 给定一个串,把串分为偶数段 假设分为了\(s1,s2,s3....sk\) 求,满足\(s_1=s_k ...

随机推荐

  1. InnoDB支持的最大事务数量

    Innodb存储引擎有rollback segment,每个rollback segment中记录了1024个undo log segment,在每个undo log segment中进行undo页的 ...

  2. python3+paramiko实现ssh客户端

    一.程序说明 ssh客户端实现主要有以下四个问题: 第一个问题是在python中ssh客户端使用哪个包实现----我们这里使用的是paramiko 第二个问题是怎么连接服务器----连接服务器直接使用 ...

  3. Vue.js 引入外部js方法

    1.外部文件config.js 第一种写法 //常量的定义 const config = { baseurl:'http://172.16.114.5:8088/MGT2' } //函数的定义 fun ...

  4. textarea输入框限制字数

    <textarea onkeyup="checkLen(this)"></textarea> <div>您还可以输入 <span id=& ...

  5. laravel获取当前的url以及当前的基础域名方法汇总

    原文地址:https://phpartisan.cn/news/58.html 来源于:laravel获取当前的url以及当前的基础域名方法汇总 - Laravel学习网 laravel中我们常常需要 ...

  6. jps -- process information unavailable

    在之前停止java进程时,使用了 kill -9,结果进程未正常退出. 之后每次执行 jps 命令时都会打印出 -- process information unavailable 在ls /tmp/ ...

  7. JAVA设计模式(一)单例模式

    单例设计模式 Singleton是一种创建型模式,指某个类采用Singleton模式,则在这个类被创建后,只可能产生一个实例供外部访问,并且提供一个全局的访问点. 核心知识点如下: (1) 将采用单例 ...

  8. 【转载二】Grafana系列教程–Grafana的下载及安装

    本篇教程,waitig 来为大家介绍一下Grafana的安装及运行的方式. 更多Grafana技术请加入<InfluxDB&Grafana技术交流群:580487672(点击加入)> ...

  9. 5.9 C++重载转型操作符

    参考:http://www.weixueyuan.net/view/6387.html 注意: 转型构造函数可以将其它类型的参数转换为类类型,如果我们要进行相反的转换过程,将类类型转换为其它数据类型, ...

  10. Linux7 下重新安装YUM

    所有操作均在ROOT用户下,系统版本是Linux7.0 X86_64: 一.删除原有YUM # rpm -aq|grep yum|xargs rpm -e --nodeps 二.下载yum,注意自己的 ...