题目传送门

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

思路:

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

代码基本是学习巨佬yyb

  1. #pragma GCC optimize (2)
  2. #pragma G++ optimize (2)
  3. #pragma comment(linker, "/STACK:102400000,102400000")
  4. #include<bits/stdc++.h>
  5. #include<cstdio>
  6. #include<vector>
  7. #define rep(i,a,b) for(int i=a;i<=b;i++)
  8. #define dep(i,b,a) for(int i=b;i>=a;i--)
  9. #define clr(a,b) memset(a,b,sizeof(a))
  10. #define pb push_back
  11. #define pii pair<int,int >
  12. using namespace std;
  13. typedef long long ll;
  14. ll rd()
  15. {
  16. ll x=,f=;char ch=getchar();
  17. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  18. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  19. return x*f;
  20. }
  21. const int maxn=;
  22. int size[maxn];
  23. char s[maxn];
  24. int l,r,pre,suf,n;
  25. ll ans;
  26. struct Palindromic_Tree
  27. {
  28. struct Node
  29. {
  30. int son[];
  31. int ff,len,dep;
  32. }t[maxn];
  33. int last,tot;
  34. void init()
  35. {
  36. l=1e5,r=l-;
  37. tot=;
  38. clr(s,'\0');
  39. clr(t,);
  40. t[++tot].len=-;
  41. t[].ff=t[].ff=;
  42. }
  43. void extend(int c,int n,int &last,int op)
  44. {
  45. int p=last;
  46. while(s[n-op*t[p].len-op]!=s[n])p=t[p].ff;
  47. if(!t[p].son[c])
  48. {
  49. int v=++tot,k=t[p].ff;
  50. t[v].len=t[p].len+;
  51. while(s[n-op*t[k].len-op]!=s[n])k=t[k].ff;
  52. t[v].ff=t[k].son[c];
  53. t[p].son[c]=v;
  54. t[v].dep=t[t[v].ff].dep+;
  55. }
  56. last=t[p].son[c];
  57. ans+=t[last].dep;
  58. if(t[last].len==r-l+)pre=suf=last;
  59. }
  60. }a;
  61. int main(){
  62. while(cin>>n){
  63. a.init();
  64. ans=;
  65. while(n--){
  66. int op=rd();
  67. if(op<=){
  68. char c=getchar();
  69. if(op==)s[--l]=c,a.extend(c-,l,pre,-);
  70. else s[++r]=c,a.extend(c-,r,suf,);
  71. }else if(op==)printf("%d\n",a.tot-);
  72. else printf("%lld\n",ans);
  73. }
  74. }
  75. }

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. JPA接口整理归纳方法规则

    Keyword Sample JPQL snippet And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ...

  2. springboot中的mybatis是如果使用pagehelper的

    springboot中使用其他组件都是基于自动配置的AutoConfiguration配置累的,pagehelper插件也是一样的,通过PageHelperAutoConfiguration的,这个类 ...

  3. 7、服务发现&服务消费者Ribbon

    公众号: java乐园 在<服务注册&服务提供者>这一篇可能学习了这么开发一个服务提供者,在生成上服务提供者通常是部署在内网上,即是服务提供者所在的服务器是与互联网完全隔离的.这篇 ...

  4. MyBatis笔记二:配置

    MyBatis笔记二:配置 1.全局配置 1.properites 这个配置主要是引入我们的 properites 配置文件的: <properties resource="db.pr ...

  5. springMVC的数据封装

    编写实体类: package cn.mepu.domain; /** * @User 艾康 * @create 2019-11-12 13:56 */ public class User { priv ...

  6. su - 运行替换用户和组标识的shell

    SYNOPSIS(总览) su [OPTION]... [-] [USER [ARG]...] DESCRIPTION(描述) 修改有效用户标识和组标识为USER的. -, -l, --login 使 ...

  7. htop资源管理器

    htop是linux资源管理器,安装后界面如下图: 当我们用安装yum -y htop时,会报错,这是因为需要安装扩展源 yum -y epel 扩展源 安装完扩展源之后,就可以安装了

  8. 手机号码格式验证和 FASTDFS 工具类

    常见大陆和香港号码格式验证 import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex ...

  9. 【TCP】TCP状态

    下图所示,TCP通信过程包括三个步骤:建立TCP连接通道(三次握手).数据传输.断开TCP连接通道(四次挥手). 这里进一步探究TCP三路握手和四次挥手过程中的状态变迁以及数据传输过程.先看TCP状态 ...

  10. Service7

    在真机上,利用clone-vm7新建一台虚拟机,名字:PXE-Server     1.设置防火墙为trusted   2.当前及永久关闭SELinux   3.配置IP地址:192.168.4.16 ...