题目传送门

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

思路:

  就是在普通回文树的基础上,维护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);
}
}
}

最新文章

  1. 使用 ServiceStack 构建跨平台 Web 服务
  2. 简单研究Android View绘制三 布局过程
  3. Web设计之网页布局CSS技巧
  4. JQuery拖拽排序
  5. CentOS 安装Zookeeper-3.4.6 单节点
  6. [Tomcat] Tomcat远程调试
  7. [转]10分钟入门python
  8. Oracle RAC中的投票算法
  9. Oracle 中的Top写法
  10. Express ( MiddleWare/中间件 路由 在 Express 中使用模板引擎 常用API
  11. node-Telnet
  12. js原生获取元素的css属性
  13. Microsoft Visual Studio 2012 添加实体数据模型
  14. 第二章 函数编程&amp;常用标准库
  15. TreeGrid 控件集 :delphi 学习群 ---- 166637277 (Delphi学习交流与分享)
  16. shell脚本实例-case 删除用户判断的小案例
  17. HGOI NOIP模拟4 题解
  18. 2018年牛客网NOIP赛前训练营游记
  19. (转载)设计模式之-策略模式(Strategy)
  20. shell中date使用总结-基于自动定期备份mysql实践

热门文章

  1. ubuntu18.4 搭建lamp环境
  2. Schema约束与DTD约束
  3. Fatal error compiling: invalid target release: 11 -&gt; [Help 1]
  4. Python文件路径操作
  5. php重定向说明
  6. vue 实现分页
  7. Yii2 Composer
  8. 吉首大学校赛 I 滑稽树上滑稽果 (Lucas + 莫队)
  9. Codeforces Round #568 (Div. 2) G1. Playlist for Polycarp (easy version) (状压dp)
  10. Android Runnable 运行在那个线程