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. Java反序列化修复方案

    1)下载与当前大版本相同的commons-collections包(原来是3.2.x就替换为3.2.2,原来是4.x就替换为4.4.1) 下载链接:http://commons.apache.org/ ...

  2. css 解决fixed 布局下不能滚动的问题

    如果我们布局的是后是fixed并且想要高度为100%的时候,我们一般会这样设置: div { display:fixed; height:%; overflow:scroll; } 但是这样并不会出现 ...

  3. 用mobiscroll.js如何简单使用日期控件

    首先,可以到官网学习,地址:https://docs.mobiscroll.com 第一步:引用js.css样式 1)mobiscroll.css 2)mobiscroll_date.css 3)jq ...

  4. Laravel 更新数据时在表单请求验证中排除自己,检查指定字段唯一性

    原文地址:https://moell.cn/article/24 不错的laravel网站 需求场景 修改用户信息时,在表单请求验证中排除当前邮箱所在的记录行,并检查邮箱的唯一性. Laravel版本 ...

  5. django 中下载文件与下载保存为excel

    一.django 中下载文件 在实际的项目中很多时候需要用到下载功能,如导excel.pdf或者文件下载,当然你可以使用web服务自己搭建可以用于下载的资源服务器,如nginx,这里我们主要介绍dja ...

  6. libiconv库的安装和使用

    1.libiconv包的下载路径 包的下载页面http://www.gnu.org/software/libiconv/ 2.编译安装libiconv库 $tar zxvf libiconv-1.14 ...

  7. vue-1-模板语法

    文本 <span>Message: {{ msg }}</span><span v-once>这个将不会改变: {{ msg }}</span> 原始 ...

  8. 协程(Coroutine)与多线程,多进程

    执行多个任务可以使用多线程或多进程. 多进程中,同一个变量,各自有一份拷贝存在于每个进程中,互不影响 多线程中,所有变量都由所有线程共享.而线程间的切换是系统进行调度,无法控制,所以可能 一个进程中的 ...

  9. Cracking The Coding Interview2.3

    #include <iostream> #include <string> using namespace std; class linklist { private: cla ...

  10. 5.3 C++用顶层函数重载操作符

    参考:http://www.weixueyuan.net/view/6381.html 总结: 可以将操作符重载函数声明为顶层函数. 如果以顶层函数的形式重载操作符时,二元操作符重载函数必须有两个参数 ...