题目传送门

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

思路:

  就是在普通回文树的基础上,维护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. 嵌套循环结合修改IFS环境变量遍历文件数据中IFS的修改一致性

    以下这个脚本使用了两个不同的IFS值来解析数据.第一个IFS值解析出/etc/passwd文件中的单独的行.内部for循环接着将IFS值改为冒号,云溪你从/etc/passwd的行中解析出单独的值. ...

  2. goland 实用键

    代码补全 option + command + v

  3. JavaScript 绑定事件时传递数据

    var data = { name: 'Ruchee', email: 'my@ruchee.com' }; data.handleEvent = function (e) { console.log ...

  4. springMVC+freemarker整合

    转自:http://angelbill3.iteye.com/blog/1980904 在springMVC的项目中,加入freemarker 1.首先导入springMVC-webmvc所需的JAR ...

  5. 第2篇Kubernetes架构

      一.Kubernetes 架构: Kubernetes Cluster 由 Master 和 Node 组成,节点上运行着若干 Kubernetes 服务. Master 节点 Master 是 ...

  6. Nodejs进阶:密码加盐

    原理:就是在密码特定位置插入特定字符串后,再对修改后的字符串进行md5运算. demo var crypto=require("crypto"); function cryptPw ...

  7. vue 数字输入组件

    index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...

  8. react-native run-android出现红屏错误

    react-native run-android出现 unable to load script from assets 'index.android.bundle'.Make sure your b ...

  9. laravel将数组转换成集合

    $myArray = collect($this -> menuPermissionTypes); //$this -> menuPermissionTypes是数组! dd($myArr ...

  10. Delphi 常用API 函数列表

    Delphi 常用API 函数 AdjustWindowRect 给定一种窗口样式,计算获得目标客户区矩形所需的窗口大小AnyPopup 判断屏幕上是否存在任何弹出式窗口ArrangeIconicWi ...