【HDU5421】Victor and String(回文树)
【HDU5421】Victor and String(回文树)
题面
Vjudge
大意:
你需要支持以下操作:
动态在前端插入一个字符
动态在后端插入一个字符
回答当前本质不同的回文串个数
回答当前回文串个数
题解
回文树前端插入的操作,学一学感觉并不难?
额外维护一下一个前端插入的\(last\)
然后就和后端插入一模一样,前端插入时就是前端插入的\(last\)
唯一会产生的影响的就是当前字符差完后,
发现现在的\(last\)就是整个串,那么就要更新另外一端的\(last\)为当前这一端的\(last\)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 222222
inline int read()
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
struct Node
{
int son[26],ff;
int len,dep;
}t[MAX];
char s[MAX];
ll ans;
int tot,l,r,pre,suf,n;
void init()
{
l=1e5,r=l-1;memset(s,'\0',sizeof(s));
memset(t,0,sizeof(t));ans=0;
t[pre=suf=0].ff=t[tot=1].ff=1;t[1].len=-1;
}
void extend(int c,int n,int &last,int op)
{
int p=last;
while(s[n]!=s[n-op*t[p].len-op])p=t[p].ff;
if(!t[p].son[c])
{
int v=++tot,k=t[p].ff;t[v].len=t[p].len+2;
while(s[n]!=s[n-op*t[k].len-op])k=t[k].ff;
t[v].ff=t[k].son[c];t[p].son[c]=v;
t[v].dep=t[t[v].ff].dep+1;
}
last=t[p].son[c];ans+=t[last].dep;
if(t[last].len==r-l+1)pre=suf=last;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
while(n--)
{
int opt=read();
if(opt<=2)
{
char c=getchar();
if(opt==1)s[--l]=c,extend(c-97,l,pre,-1);
else s[++r]=c,extend(c-97,r,suf,1);
}
else if(opt==3)printf("%d\n",tot-1);
else printf("%lld\n",ans);
}
}
return 0;
}
【HDU5421】Victor and String(回文树)的更多相关文章
- hdu5421 Victor and String 回文树(前后插入)
题目传送门 题意:对一个字符串支持四种操作,前插入字符,后插入字符,询问本质不同的回文串数量和所有回文串的数量. 思路: 就是在普通回文树的基础上,维护suf(最长回文后缀)的同时再维护一个pre(最 ...
- 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$赋值 ...
- 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 ...
随机推荐
- Qt5.3.2(VS2010)_调试_查看变量值
1.菜单栏 -->控件(W) --> 视图 2.在"Debug"状态下,选择一个变量(或者 按住左键 选择变量及其属性/方法)--> 右键 --> 添加表达 ...
- RabbitMQ入门_14_Policies
参考资料:https://www.rabbitmq.com/parameters.html#policies A. Policies 的用途 RabbitMQ 有很多可选参数(x-arguments) ...
- SVN同步版本库与网站目录2
定义: SVN版本库 = /home/svn/repos 网站目录 = /var/www/web 1.检出一个项目到网站目录 #svn checkout file:///home/svn/ ...
- [.NET开发] C#实现发送手机验证码功能
之前不怎么了解这个,一直以为做起来很复杂. 直到前两天公司要求要做这个功能. 做了之后才发现 这不过就是一个POST请求就能实现的东西.现在给大家分享一下,有不足之处还请多多指教. 废话不多说 直接上 ...
- protected internal == internal
总结:在同程序集下,protected internal类型修饰的成员变量可以在基类或派生类的类内.类外访问(同程序集下protected internal和internal访问性质相同,此处保留了i ...
- LeetCode--100--相同的树
问题描述: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [1,2, ...
- Ubuntu 18.04 install JDK8.201
sudo add-apt-repository ppa:webupd8team/java Press Enter to continue. sudo apt update sudo apt insta ...
- Mac安装fish shell
1.brew update 2.brew install fish 3.sudo vi /etc/shells 增加内容:/usr/local/bin/fish ##增加fish到shell环境变 ...
- nginx支持返回相对路径
一.问题:http://192.168.72.4/bm-crm/ 局域网可以访问[实际指向了192.168.80.1:8081/brm-crm] http://59.41.111.24:8280/b ...
- Java NIO理解与使用
https://blog.csdn.net/qq_18860653/article/details/53406723 Netty的使用或许我们看着官网user guide还是很容易入门的.因为java ...