Gym - 101806Q:QueryreuQ(回文树)
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 :
- Add a lower case alphabet at the back of S.
- 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 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
17
qu-uer-ryr-reu-uq
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(回文树)的更多相关文章
- 回文树&后缀自动机&后缀数组
KMP,扩展KMP和Manacher就不写了,感觉没多大意思. 之前感觉后缀自动机简直可以解决一切,所以不怎么写后缀数组. 马拉车主要是通过对称中心解决问题,有的时候要通过回文串的边界解决问题 ...
- 回文树 Palindromic Tree
回文树 Palindromic Tree 嗯..回文树是个什么东西呢. 回文树(或者说是回文自动机)每个节点代表一个本质不同的回文串. 首先它类似字典树,每个节点有SIGMA个儿子,表示对应的字母. ...
- HDU3948 & 回文树模板
Description: 求本质不同回文子串的个数 Solution: 回文树模板,学一学贴一贴啊... Code: /*================================= # Cre ...
- 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 ...
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
- 【CF17E】Palisection(回文树)
[CF17E]Palisection(回文树) 题面 洛谷 题解 题意: 求有重叠部分的回文子串对的数量 所谓正难则反 求出所有不重叠的即可 求出以一个位置结束的回文串的数量 和以一个位置为开始的回文 ...
- 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)
[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...
- 【BZOJ2160】拉拉队排练(回文树)
[BZOJ2160]拉拉队排练(回文树) 题面 BZOJ 题解 看着题目, 直接构建回文树 求出每个回文串的出现次数 直接按照长度\(sort\)一下就行了 然后快速幂算一下答案就出来了 这题貌似可以 ...
- 【CF932G】Palindrome Partition(回文树,动态规划)
[CF932G]Palindrome Partition(回文树,动态规划) 题面 CF 翻译: 给定一个串,把串分为偶数段 假设分为了\(s1,s2,s3....sk\) 求,满足\(s_1=s_k ...
随机推荐
- Pl/sql 如何将oracle的表数据导出成excel文件?
oracle将表数据导出成excel文件的方法 1)在SQL窗体上,查询需要导出的数据 --查询数据条件-- ; 结果视图 2)在查询结果的空白处,右键选择Copy to Excel 3) 查看导出e ...
- 【基础】iframe之间的切换(四)
案例: 打开http://mail.126.com/,定位登录输入框时,却总是定位不到元素,后来发现,登录的内容在一个iframe中. 一.由主页面切换至iframe dr.switchTo().fr ...
- laravel的工厂模式数据填充:
数据表post中的字段结构. database\factory\UserFactory.php $factory->define(App\Post::class,function (Faker ...
- Win10系列:JavaScript图形
在页面中添加canvas元素会在页面上生成一个矩形的位图画布,可以使用JavaScript在画布上实时绘制图形图像.在绘制图形时,需要先调用画布的getContext函数获取与该画布相关的用于绘制图形 ...
- 输出链表的倒数第K个值
题目描述 输入一个链表,输出该链表中倒数第k个结点. 思路一:链表不能向前遍历,只能向后遍历.因此倒数第K个结点就是 正序的 :len(链表)-1-K的下一个. 注意,此处的思路与代码中具体实 ...
- java⑩
1.for循环: for循环语法 for(表达式1;表达式2;表达式3){ 循环体4} 表达式1:初始化变量 只执行一次!表达式2:循环条件 满足条件进入循环体4表达式3:迭代变量 如果循环体 中只有 ...
- 戴尔poweredge r730服务器配置以及系统安装
第一次给服务器安装的是ubantu系统: 首先我们开机进入小型BIOS设置一下RAID,或者进入服务器管理系统,在系统的BIOS中进行RAID设置: 开机后当看到出现< Ctrl > &l ...
- GDI中StretchBlt或Blt缩放图片失真问题
在用Scrollview控件的缩放图片时,严重失真: 解决方法:dc.SetStretchBltMode(COLORONCOLOR). 参考文章:https://blog.csdn.net/m3728 ...
- htmlayout 最简单的实践,用于理解实现原理。
/ testHtmlayout.cpp : 定义应用程序的入口点. // #include "stdafx.h" #include "testHtmlayout.h&qu ...
- matlabR2017安装
安装教程参考: https://blog.csdn.net/m0_37638031/article/details/78982498