题目传送门

题意:对一个字符串支持四种操作,前插入字符,后插入字符,询问本质不同的回文串数量和所有回文串的数量。

思路:

  就是在普通回文树的基础上,维护suf(最长回文后缀)的同时再维护一个pre(最长回文前缀),即可完成以上操作。

代码基本是学习巨佬yyb

#pragma GCC optimize (2)
#pragma G++ optimize (2)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<cstdio>
#include<vector>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,b,a) for(int i=b;i>=a;i--)
#define clr(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define pii pair<int,int >
using namespace std;
typedef long long ll;
ll rd()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int maxn=;
int size[maxn];
char s[maxn];
int l,r,pre,suf,n;
ll ans;
struct Palindromic_Tree
{
struct Node
{
int son[];
int ff,len,dep;
}t[maxn];
int last,tot;
void init()
{
l=1e5,r=l-;
tot=;
clr(s,'\0');
clr(t,);
t[++tot].len=-;
t[].ff=t[].ff=;
}
void extend(int c,int n,int &last,int op)
{
int p=last;
while(s[n-op*t[p].len-op]!=s[n])p=t[p].ff;
if(!t[p].son[c])
{
int v=++tot,k=t[p].ff;
t[v].len=t[p].len+;
while(s[n-op*t[k].len-op]!=s[n])k=t[k].ff;
t[v].ff=t[k].son[c];
t[p].son[c]=v;
t[v].dep=t[t[v].ff].dep+;
}
last=t[p].son[c];
ans+=t[last].dep;
if(t[last].len==r-l+)pre=suf=last;
}
}a;
int main(){
while(cin>>n){
a.init();
ans=;
while(n--){
int op=rd();
if(op<=){
char c=getchar();
if(op==)s[--l]=c,a.extend(c-,l,pre,-);
else s[++r]=c,a.extend(c-,r,suf,);
}else if(op==)printf("%d\n",a.tot-);
else printf("%lld\n",ans);
}
}
}

hdu5421 Victor and String 回文树(前后插入)的更多相关文章

  1. HDU 5157 Harry and magic string(回文树)

    Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  2. The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树

    签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...

  3. HDU 6599 I Love Palindrome String (回文树+hash)

    题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...

  4. 2019 徐州网络赛 G Colorful String 回文树

    题目链接:https://nanti.jisuanke.com/t/41389 The value of a string sss is equal to the number of differen ...

  5. HDOJ 5421 Victor and String 回文串自己主动机

    假设没有操作1,就是裸的回文串自己主动机...... 能够从头部插入字符的回文串自己主动机,维护两个last点就好了..... 当整个串都是回文串的时候把两个last统一一下 Victor and S ...

  6. HDU 5421 Victor and String (回文自动机)

    题目大意:让你维护一个字符串,支持在开头结尾插入字符,以及查询本质不同的回文串数量以及回文串总数量 开头结尾都维护一个$last$指针,如果插入新字符后,整个串是一个回文串,就把另一个$last$赋值 ...

  7. 【HDU5421】Victor and String(回文树)

    [HDU5421]Victor and String(回文树) 题面 Vjudge 大意: 你需要支持以下操作: 动态在前端插入一个字符 动态在后端插入一个字符 回答当前本质不同的回文串个数 回答当前 ...

  8. HDU 5421 Victor and String(回文树)

    Victor and String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/262144 K (Java/Othe ...

  9. HDU5421 Victor and String 和 APIO2014 回文串

    两道差不多的题,都是回文自动机right集合处理相关. Victor and String Victor loves to play with string. He thinks a string i ...

随机推荐

  1. Apache Solr远程命令执行

    简介 Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引:也可以通过Http G ...

  2. Nginx+PHP-FPM远程命令执行_CVE-2019-11043

    漏洞描述 此次漏洞主要由于 PHP-FPM 中 sapi/ fpm/ fpm/ fpm_main.c 文件内的 env_path_info 下溢导致,攻击者可以使用换行符 %0a 破坏 Nginx 中 ...

  3. 前端用vue,后端需要nodejs吗?

    其实用脚趾头想想也知道:后端用不用node,和前端用不用vue,二者完全没关系. 那为何用vue和vue插件开发前端的时候,经常需要各种npm install呢?为何要在本地安装node环境? 本地需 ...

  4. DLL 本地定义符号*****在函数****中导入

    把_DLL_ELOGEVENT  定义到你的工程预编译宏中 IPCLASSDLL_DLL https://bbs.csdn.net/topics/300140279

  5. 解决ubuntu终端路劲显示过长问题

    1 找到配置文件先进行备份:  cp  ~/.bashrc  ~/.bashrc-bak 2 找到配置文件修改: vi  ~/.bashrc 备份是为了防止配置修改出错,可以还原 3 在以下的红色位置 ...

  6. elasticsearch学习笔记001

    <Elasticsearch 核心技术与实战>课程Github代码 https://github.com/onebirdrocks/geektime-ELK 运行的环境: windows ...

  7. CleanMyMac x如何关闭自动更新,CleanMyMac关闭自动更新的方法

    在众多Mac清理软件中,CleanMyMac x应该是大多数Mac用户的选择,因为它不仅可以帮助我们清理系统垃圾,还可以帮助我们彻底卸载Mac上不需要的应用程序.今天要给大家介绍的是CleanMyMa ...

  8. vue 项目 多选 问题

    近期 vue 项目用到多选功能,引用的elementUI的级联多选 Cascader,但是没有效果. 后来发现是elementUI版本问题,我们项目用的是2.7.2版本,版本太低. 后来 卸载了 重新 ...

  9. Kubernetes 弹性伸缩HPA功能增强Advanced Horizontal Pod Autoscaler -介绍部署篇

    背景 WHAT(做什么) Advanced Horizontal Pod Autoscaler(简称:AHPA)是kubernetes中HPA的功能增强. 在兼容原生HPA功能基础上,增加预测.执行模 ...

  10. PHP FILTER_VALIDATE_BOOLEAN 过滤器

    定义和用法 FILTER_VALIDATE_BOOLEAN 过滤器把值作为布尔选项来验证. Name: "boolean" ID-number: 258 可能的返回值: 如果是 & ...