题目链接: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)的更多相关文章

  1. BZOJ1014 JSOI2008 火星人prefix 【非旋转Treap】*

    BZOJ1014 JSOI2008 火星人prefix Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符 ...

  2. [BZOJ1014][JSOI2008]火星人prefix

    [BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ...

  3. 2018.06.28 BZOJ1014 [JSOI2008]火星人prefix(非旋treap+hash)

    [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Submit: 8951 Solved: 2860 Description 火星 ...

  4. bzoj千题计划106:bzoj1014 [JSOI2008]火星人prefix

    http://www.lydsy.com/JudgeOnline/problem.php?id=1014 两个后缀的最长公共前缀:二分+hash 带修改带插入:splay维护 #include< ...

  5. Jewel Magic UVA - 11996 || bzoj1014: [JSOI2008]火星人prefix

    Jewel Magic UVA - 11996 这是一道用splay/非旋treap做的题(这里用的是非旋treap) 1/2/3是splay/非旋treap的常规操作.对于操作4,可以用哈希法求LC ...

  6. bzoj1014: [JSOI2008]火星人prefix splay+hash+二分

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  7. BZOJ1014[JSOI2008]火星人prefix(splay维护hash)

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  8. BZOJ1014: [JSOI2008]火星人prefix(splay 二分 hash)

    题意 题目链接 Sol 一眼splay + 二分hash,不过区间splay怎么写来着呀 试着写了两个小时发现死活不对 看了一下yyb的代码发现自己根本就不会splay.... // luogu-ju ...

  9. bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)

    题目大意:一个字符串三个操作:①求两个后缀的LCP②插入一个字符③修改一个字符. 前几天刚学了hash+二分求lcp,就看到这题. 原来splay还能这么用?!原来splay模板这么好写?我以前写的s ...

随机推荐

  1. vue.js(15)--vue的生命周期

    生命周期钩子 生命周期钩子=生命周期函数=生命周期事件 每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如,需要设置数据监听.编译模板.将实例挂载到 DOM 并在数据变化时更新 DOM 等 ...

  2. eclipse codeFormatter 和 codeTemplates 模板

    下载  eclipse_modles.rar 好用高效的eclipse的注释和代码风格模板.     版权声明:本文为博主原创文章,未经博主允许不得转载.

  3. 1-基于Xilinx XCKU115的半高PCIe x8 硬件加速卡

    基于Xilinx XCKU115的半高PCIe x8 硬件加速卡 一.概述 本板卡系我公司自主研发,采用Xilinx公司的XCKU115-3-FLVF1924-E芯片作为主处理器,主要用于FPGA硬件 ...

  4. django:一个RESTfull的接口从wsgi到函数的历程

    1.wsgi将web server参数python化,封装为request对象传递给apllication命名的func对象并接受其传出的response参数,这个application在wsgi.p ...

  5. python-条件判断

    条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= ...

  6. CSP-S2019 停课日记

    前言 不想上文化课,于是就停课了 (雾) \(10.13\) 停课前一天 今天名义上是放假,所以不算停课. 老师和同学们听说我要停课,都十分的不舍.我啥也没说就悄悄溜到一中来了. \(10.14\) ...

  7. Map.Entry的由来和使用

    首先,回忆和练习一下HashMap的遍历 package Exercise.exercise; import java.util.HashMap; import java.util.Iterator; ...

  8. java实现js端的escape和unescape

    1.今天遇到这么个问题,需要把一些特殊字符传递到后台进行处理,例如Aa111111!@#,结果到了后台出现了个别字符中文符号了.这个时候需要转码.常见的就是js端的escape和unescape这种函 ...

  9. input的文件上传类型判断

    参考网址: http://www.helloweba.com/view-blog-224.html <p> <label>请选择一个图像文件:</label> &l ...

  10. 【CF1243B1】Character Swap (Easy Version)【思维】

    题意:给你两个字符串,问是否存在交换方案使得两个字符串变成一样的,方案为只交换一次且只交换s1与s2里的一个字符 题解:若一开始就相同,则存在交换方案 若一开始不同的位置为1个或大于2个,则不存在方案 ...