[Bzoj1014][JSOI2008]火星人prefix(无旋Treap&hash)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1014
因为涉及到增加和修改,所以后缀数组就被pass掉了,想到的就是平衡树维护hash值,查询的时候就是二分相同的长度来比较,修改就是删除再增加。
这里使用的是无旋Treap
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef unsigned int ull;
- const int maxn = ;
- int Siz[maxn], ls[maxn], rs[maxn], pos[maxn], val[maxn], root, cnt;
- ull Hash[maxn], w[maxn];
- inline void up(int x) {
- Siz[x] = Siz[ls[x]] + Siz[rs[x]] + ;
- w[x] = w[ls[x]] * Hash[Siz[rs[x]] + ] + Hash[Siz[rs[x]]] * val[x] + w[rs[x]];
- }
- inline void split_size(int x, int siz, int &A, int &B) {
- if (x == )return (void)(A = B = );
- if (siz <= Siz[ls[x]])
- B = x, split_size(ls[x], siz, A, ls[x]);
- else
- A = x, split_size(rs[x], siz - Siz[ls[x]] - , rs[x], B);
- up(x);
- }
- inline int Merge(int A, int B) {
- if (A == || B == )return A | B;
- int ans;
- if (pos[A] > pos[B])ans = A, rs[A] = Merge(rs[A], B);
- else ans = B, ls[B] = Merge(A, ls[B]);
- up(ans);
- return ans;
- }
- inline void insert(int x, char v) {
- int A, B;
- split_size(root, x, A, B);
- cnt++;
- w[cnt] = val[cnt] = v - 'a' + ;
- Siz[cnt] = ;
- pos[cnt] = rand();
- root = Merge(Merge(A, cnt), B);
- }
- inline void Delete(int x) {
- int A, B, C, D;
- split_size(root, x, A, B);
- split_size(A, x - , C, D);
- D = Merge(ls[D], rs[D]);
- root = Merge(Merge(C, D), B);
- }
- inline ull query(int l, int r) {
- int A, B, C, D;
- ull ans;
- split_size(root, l - , A, B);
- split_size(B, r - l + , C, D);
- ans = w[C];
- root = Merge(A, Merge(C, D));
- return ans;
- }
- inline bool check(int x, int y, int len) {
- return query(x, x + len - ) == query(y, y + len - );
- }
- inline int read() {
- int x = , f = ; char c = getchar();
- for (; !isdigit(c); c = getchar()) if (c == '-') f = -;
- for (; isdigit(c); c = getchar()) x = x * + c - ''; return x * f;
- }
- inline char cread() {
- char c = getchar();
- for (; !isalpha(c); c = getchar()); return c;
- }
- inline void reads(string& s) {
- char c = getchar();
- for (; !isalpha(c); c = getchar()); s = " ";
- for (; isalpha(c); c = getchar()) s += c;
- }
- string s;
- int main() {
- reads(s);
- int n = s.size() - , m;
- m = read();
- Hash[] = ;
- for (int i = ; i < maxn; i++)
- Hash[i] = Hash[i - ] * ;
- for (int i = ; i <= n; i++)
- insert(i, s[i]);
- while (m--) {
- char opt;
- int x, y;
- opt = cread();
- if (opt == 'Q') {
- x = read(), y = read();
- int l = , r = min(n - x, n - y) + , ans;
- while (l <= r) {
- int mid = l + r >> ;
- if (check(x, y, mid)) {
- ans = mid;
- l = mid + ;
- }
- else
- r = mid - ;
- }
- printf("%d\n", ans);
- }
- else if (opt == 'R') {
- x = read(), opt = cread();
- Delete(x);
- insert(x - , opt);
- }
- else {
- x = read(), opt = cread();
- n++;
- insert(x, opt);
- }
- }
- return ;
- }
[Bzoj1014][JSOI2008]火星人prefix(无旋Treap&hash)的更多相关文章
- BZOJ1014 JSOI2008 火星人prefix 【非旋转Treap】*
BZOJ1014 JSOI2008 火星人prefix Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符 ...
- [BZOJ1014][JSOI2008]火星人prefix
[BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ...
- 2018.06.28 BZOJ1014 [JSOI2008]火星人prefix(非旋treap+hash)
[JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Submit: 8951 Solved: 2860 Description 火星 ...
- bzoj千题计划106:bzoj1014 [JSOI2008]火星人prefix
http://www.lydsy.com/JudgeOnline/problem.php?id=1014 两个后缀的最长公共前缀:二分+hash 带修改带插入:splay维护 #include< ...
- Jewel Magic UVA - 11996 || bzoj1014: [JSOI2008]火星人prefix
Jewel Magic UVA - 11996 这是一道用splay/非旋treap做的题(这里用的是非旋treap) 1/2/3是splay/非旋treap的常规操作.对于操作4,可以用哈希法求LC ...
- bzoj1014: [JSOI2008]火星人prefix splay+hash+二分
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- BZOJ1014[JSOI2008]火星人prefix(splay维护hash)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- BZOJ1014: [JSOI2008]火星人prefix(splay 二分 hash)
题意 题目链接 Sol 一眼splay + 二分hash,不过区间splay怎么写来着呀 试着写了两个小时发现死活不对 看了一下yyb的代码发现自己根本就不会splay.... // luogu-ju ...
- bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)
题目大意:一个字符串三个操作:①求两个后缀的LCP②插入一个字符③修改一个字符. 前几天刚学了hash+二分求lcp,就看到这题. 原来splay还能这么用?!原来splay模板这么好写?我以前写的s ...
随机推荐
- 用Node.js原生代码实现静态服务器
---恢复内容开始--- 后端中服务器类型有两种 1. web服务器[ 静态服务器 ] - 举例: wamp里面www目录 - 目的是为了展示页面内容 - 前端: nginx 2. 应用级服务器[ a ...
- 安装运行谷歌开源的TensorFlow Object Detection API视频物体识别系统
Linux安装 参照官方文档:https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/inst ...
- Python Paramiko模块使用
1 执行远程命令 #!/usr/bin/python import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_polic ...
- 定时器,定时发邮件JavaMail
一.定时器用法: 1.1先导入jar包 <!--spring整合其他文件时要用的jar包--> <dependency> <groupId>org.springfr ...
- goland使用:导入一个github开源项目tidb
概要:在windos下的IDEA 的go语言的编辑器 goland的使用,导入github上面的开源项目. 问题: 下载好goland之后,open project打开一个下载好的githubhub项 ...
- python笔记(1)--基础知识
一.注释 单行注释 #打印“hello world” print("hello.world!") 另外一种单行注释 print("hello,world!") ...
- hdu 4845 : 拯救大兵瑞恩 (bfs+状态压缩)
题目链接 #include<bits/stdc++.h> using namespace std; typedef long long LL; int n,m,p,s,k; ,,,-}; ...
- php stripos()函数 语法
php stripos()函数 语法 作用:寻找字符串中某字符最先出现的位置,不区分大小写.直线电参数 语法:stripos(string,find,start) 参数: 参数 描述 string ...
- luoguP4721 【模板】分治 FFT (分治NTT)
给定 $g[1....n-1]$,求 $f[0],f[1],...,f[n-1]$,其中 $f[i]=\sum_{j=1}^{i}f[i-j]g[j]$ 变界为 $f[0]=1$ 答案模 9 ...
- Python_011(生成器)
一.生成器 def func(): ") return 222 ret = func() print(ret) #结果 111 222 1)这里面函数体里是返回值return;如果将retu ...