hdu5421 Victor and String 回文树(前后插入)
题意:对一个字符串支持四种操作,前插入字符,后插入字符,询问本质不同的回文串数量和所有回文串的数量。
思路:
就是在普通回文树的基础上,维护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 回文树(前后插入)的更多相关文章
- HDU 5157 Harry and magic string(回文树)
Harry and magic string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树
签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...
- HDU 6599 I Love Palindrome String (回文树+hash)
题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...
- 2019 徐州网络赛 G Colorful String 回文树
题目链接:https://nanti.jisuanke.com/t/41389 The value of a string sss is equal to the number of differen ...
- HDOJ 5421 Victor and String 回文串自己主动机
假设没有操作1,就是裸的回文串自己主动机...... 能够从头部插入字符的回文串自己主动机,维护两个last点就好了..... 当整个串都是回文串的时候把两个last统一一下 Victor and S ...
- HDU 5421 Victor and String (回文自动机)
题目大意:让你维护一个字符串,支持在开头结尾插入字符,以及查询本质不同的回文串数量以及回文串总数量 开头结尾都维护一个$last$指针,如果插入新字符后,整个串是一个回文串,就把另一个$last$赋值 ...
- 【HDU5421】Victor and String(回文树)
[HDU5421]Victor and String(回文树) 题面 Vjudge 大意: 你需要支持以下操作: 动态在前端插入一个字符 动态在后端插入一个字符 回答当前本质不同的回文串个数 回答当前 ...
- HDU 5421 Victor and String(回文树)
Victor and String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/262144 K (Java/Othe ...
- HDU5421 Victor and String 和 APIO2014 回文串
两道差不多的题,都是回文自动机right集合处理相关. Victor and String Victor loves to play with string. He thinks a string i ...
随机推荐
- JPA接口整理归纳方法规则
Keyword Sample JPQL snippet And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ...
- springboot中的mybatis是如果使用pagehelper的
springboot中使用其他组件都是基于自动配置的AutoConfiguration配置累的,pagehelper插件也是一样的,通过PageHelperAutoConfiguration的,这个类 ...
- 7、服务发现&服务消费者Ribbon
公众号: java乐园 在<服务注册&服务提供者>这一篇可能学习了这么开发一个服务提供者,在生成上服务提供者通常是部署在内网上,即是服务提供者所在的服务器是与互联网完全隔离的.这篇 ...
- MyBatis笔记二:配置
MyBatis笔记二:配置 1.全局配置 1.properites 这个配置主要是引入我们的 properites 配置文件的: <properties resource="db.pr ...
- springMVC的数据封装
编写实体类: package cn.mepu.domain; /** * @User 艾康 * @create 2019-11-12 13:56 */ public class User { priv ...
- su - 运行替换用户和组标识的shell
SYNOPSIS(总览) su [OPTION]... [-] [USER [ARG]...] DESCRIPTION(描述) 修改有效用户标识和组标识为USER的. -, -l, --login 使 ...
- htop资源管理器
htop是linux资源管理器,安装后界面如下图: 当我们用安装yum -y htop时,会报错,这是因为需要安装扩展源 yum -y epel 扩展源 安装完扩展源之后,就可以安装了
- 手机号码格式验证和 FASTDFS 工具类
常见大陆和香港号码格式验证 import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex ...
- 【TCP】TCP状态
下图所示,TCP通信过程包括三个步骤:建立TCP连接通道(三次握手).数据传输.断开TCP连接通道(四次挥手). 这里进一步探究TCP三路握手和四次挥手过程中的状态变迁以及数据传输过程.先看TCP状态 ...
- Service7
在真机上,利用clone-vm7新建一台虚拟机,名字:PXE-Server 1.设置防火墙为trusted 2.当前及永久关闭SELinux 3.配置IP地址:192.168.4.16 ...