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 ...
随机推荐
- suffix word cy dom faction ful fold form out1
1★cy ﹎﹎﹎﹎性质,状态 2★ dom ﹎﹎﹎﹎领域,范围 1★ faction ﹎﹎﹎﹎达到什么样的状态 2★ ful ﹎﹎﹎﹎满,量 *******有~的 3★ fold ﹎﹎﹎﹎双, ...
- beamer template
\setbeamercolor{postit}{fg=black,bg=white} \begin{beamercolorbox}[rounded=true,shadow=true, sep=0em, ...
- codeigniter在nginx下返回404 not found
codeigniter框架需要path_info的支持,Apache默认支持path_info,但是nginx默认不支持,我们需要设置nginx,使得nginx支持path_info 网上试了好多方法 ...
- zabbix3.4.7监控linux进程
利用zabbix proc.num方法监控Linux服务进程 proc.num[<name>,<user>,<state>,<cmdline>] 监控用 ...
- Java Web(八) 事务,安全问题及隔离级别
事务 什么是事务? 事务就是一组原子性的SQL查询,或者说是一个独立的工作单元. 事务的作用 事务在我们平常的CRUD(增删改查)操作当中也许不太常用, 但是如果我们有一种需求,一组操作中必须全部成功 ...
- CAD绘制扶手5.6
用PL命令绘制出扶手的位置,如图 , “楼梯其他”“添加扶手”选择这条线,扶手宽度60,高度900,中间对齐.生成如图: 三维:
- learning ddr mode reigster set command cycle time tMRD and tMOD
tMRD: tMOD:
- Django+Xadmin+Echarts动态获取数据legend颜色显示灰色问题已解决
前段时间做的使用Django的Xadmin后台和百度Echarts进行后台数据可视化,功能虽然实现,展示出来的legend图例,都是灰色的,只有鼠标放上去才会显示彩色的.百度都快被我刨穿了,看到有类似 ...
- Linux运维工程师需要掌握什么才能胜任工作呢
万丈高楼平地起,所有一切的高深的技术都离不开最基本的技术,那么作为运维工程师的你,什么是最基本的技术呢,毫无疑问是Linux,Linux 是你所有一切技术的根源,试想一下如果你连基础的操作命令都不知道 ...
- nginx+tomcat集群
参考: 简单:http://blog.csdn.net/wang379275614/article/details/47778201 详细:http://www.jb51.net/article/77 ...