看了《Hash在信息学竞赛中的一类应用》中的例题3,这道题很类似啊,只不过没有删点和区间翻转。

用Splay维护字符串哈希,加点改点什么的就不用说了,查询时二分答案,这样时间复杂度是$O(mlog^2 n)$的

论文的例题3中删点很简单,和插点一样,不用说了,区间翻转只要打一个翻转标记,维护正序hash和逆序hash,翻转时交换两个hash值即可。

对拍终于成功了QAQ,插点时孩子不认父亲TwT又手残了~

《Hash在信息学竞赛中的一类应用》中还提到了块状链表的做法,都很易懂:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 100003;
const int p = 9875321; int P[N];
struct node *null;
struct node {
node *ch[2], *fa;
int k, s, ha;
node (int _k = 0) {k = _k; s = 1; ha = _k; ch[0] = ch[1] = fa = null;}
void setc(node *r, bool c) {this->ch[c] = r; r->fa = this;}
bool pl() {return fa->ch[1] == this;}
void count() {
s = ch[0]->s + ch[1]->s + 1;
ha = ((ch[0]->ha + 1ll * k * P[ch[0]->s]) % p + 1ll * ch[1]->ha * P[ch[0]->s + 1] % p) % p;
}
} *root; int n, m;
char s[N]; namespace Splay {
node *Build(int l, int r) {
if (l > r) return null;
int mid = (l + r) >> 1;
node *t = new node(s[mid] - 'a');
t->ch[0] = Build(l, mid - 1); t->setc(t->ch[0], 0);
t->ch[1] = Build(mid + 1, r); t->setc(t->ch[1], 1);
t->count();
return t;
}
void init() {
P[0] = 1; for(int i = 1; i < N; ++i) P[i] = P[i - 1] * 26 % p;
null = new node; null->s = 0; null->ch[0] = null->ch[1] = null->fa = null;
scanf("%s", s + 1); n = strlen(s + 1); root = Build(1, n);
}
void rotate(node *r) {
node *f = r->fa;
bool c = r->pl();
if (f != root) f->fa->setc(r, f->pl());
else root = r, r->fa = null;
f->setc(r->ch[!c], c);
r->setc(f, !c);
f->count();
}
void splay(node *r, node *tar = null) {
for(; r->fa != tar; rotate(r))
if (r->fa->fa != tar) rotate(r->pl() == r->fa->pl() ? r->fa : r);
r->count();
}
node *kth(int k) {
node *r = root;
while (1) {
if (r->ch[0]->s >= k) r = r->ch[0];
else if (r->ch[0]->s + 1 >= k) return r;
else k -= (r->ch[0]->s + 1), r = r->ch[1];
}
}
int hash(int l, int r) {
if (l == 1 && r == n) return root->ha;
else if (l == 1) {splay(kth(r + 1)); return root->ch[0]->ha;}
else if (r == n) {splay(kth(l - 1)); return root->ch[1]->ha;}
else {splay(kth(l - 1)); splay(kth(r + 1), root); return root->ch[1]->ch[0]->ha;}
}
void QQ(int l, int r) {
int left = 0, right = root->s - max(l, r) + 1, mid;
while (left < right) {
mid = (left + right + 1) >> 1;
if (hash(l, l + mid - 1) == hash(r, r + mid - 1)) left = mid;
else right = mid - 1;
}
printf("%d\n", left); return;
}
void RR(int k, int num) {
node *r = kth(k);
r->k = num;
splay(r);
}
void II(int k, int num) {
if (k == 0) {
node *r = root;
while (r->ch[0] != null) r = r->ch[0];
r->setc(new node(num), 0);
splay(r->ch[0]);
} else {
splay(kth(k));
if (k == n) {
root->setc(new node(num), 1);
splay(root->ch[1]);
} else {
splay(kth(k + 1), root);
root->ch[1]->setc(new node(num), 0);
splay(root->ch[1]->ch[0]);
}
}
++n;
}
} int main() {
Splay::init();
scanf("%d", &m);
char c; int x, y;
while (m--) {
for(c = getchar(); c < 'A' || c > 'Z'; c = getchar());
switch (c) {
case 'Q':
scanf("%d%d", &x, &y);
Splay::QQ(x, y);
break;
case 'R':
scanf("%d", &x); for(c = getchar(); c < 'a' || c > 'z'; c = getchar());
Splay::RR(x, c - 'a');
break;
case 'I':
scanf("%d", &x); for(c = getchar(); c < 'a' || c > 'z'; c = getchar());
Splay::II(x, c - 'a');
break;
}
}
return 0;
}

对拍大法好~

【BZOJ 1014】【JSOI 2008】火星人prefix的更多相关文章

  1. JSOI 2008 火星人prefix

    FROM http://www.lydsy.com/JudgeOnline/problem.php?id=1014 LCP问题 给定串 S[0..n] , 对于一对(a,b)其中0<a,b< ...

  2. Bzoj 1014&Luogu 4036 火星人Prefix(FHQ-Treap)

    题面 洛谷 Bzoj 题解 首先,这种带修改的是不能用$SA$的,然后,我们做$SA$的题一般也能二分+$Hash$,所以不妨考虑用$FHQ-Treap$维护树,然后查询就用二分+$Hash$. $H ...

  3. 【BZOJ 1014】 [JSOI2008]火星人prefix

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1014 [题意] 让你在线查询最长公共前缀. 支持单节点修改; 插入操作; [题解] / ...

  4. [BZOJ 1013][JSOI 2008] 球形空间产生器sphere 题解(高斯消元)

    [BZOJ 1013][JSOI 2008] 球形空间产生器sphere Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球 面 ...

  5. [BZOJ1074] [luogu 4036] [JSOI 2008] 火星人 (二分答案+哈希+fhq treap)

    [BZOJ1074] [luogu 4036] [JSOI 2008] 火星人 (二分答案+哈希+fhq treap) 题面 给出一个长度为n的字符串,m个操作,字符串仅包含小写英文字母 操作1:在k ...

  6. BZOJ 1016 JSOI 2008 最小生成树计数 Kruskal+搜索

    题目大意:给出一些边,求出一共能形成多少个最小生成树. 思路:最小生成树有非常多定理啊,我也不是非常明确.这里仅仅简单讲讲做法.关于定各种定理请看这里:http://blog.csdn.net/wyf ...

  7. 【BZOJ】【1014】【JLOI2008】火星人prefix

    Splay/二分/Hash 看了网上的题目关键字(都不用点进去看……我也是醉了)了解到做法= =那就上呗,前面做了好几道Splay的题就是为了练手搞这个的. Hash判断字符串是否相同应该很好理解吧? ...

  8. BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6243  Solved: 2007[Submit] ...

  9. BZOJ 1014: [JSOI2008]火星人prefix Splay+二分

    1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...

  10. bzoj 1014: [JSOI2008]火星人prefix hash && splay

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3154  Solved: 948[Submit][ ...

随机推荐

  1. 三维网格细分算法(Catmull-Clark subdivision & Loop subdivision)附源码

    下图描述了细分的基本思想,每次细分都是在每条边上插入一个新的顶点,可以看到随着细分次数的增加,折线逐渐变成一条光滑的曲线.曲面细分需要有几何规则和拓扑规则,几何规则用于计算新顶点的位置,拓扑规则用于确 ...

  2. 05章项目: QuickHit快速击键

    一.项目分析 根据输入速率和正确率将玩家分为不同等级,级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高.如果玩家在规定时间内完成规定次数的输入,正确率达到规定要求,则玩家升级.玩家最高级别 ...

  3. JQuery获取元素的方法总结

    JQuery获取元素的方法总结 一.说明   获取元素的方法分为两种:jQuery选择器.jQuery遍历函数. 做个总结,巩固下知识. 二.获取本身 1.只需要一种jQuery选择器   选择器 实 ...

  4. jmeter jdbc request使用详解

    1.在使用 jdbc request之前需要加载一个jar包 在test plan中将jar包引入到classpath中 2.创建一个JDBC Connection Configuration Var ...

  5. DBCP连接池配置参数说明

    <!-- 数据源1 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicData ...

  6. java 27 - 1 反射之 类的加载器

    说到反射,首先说类的加载器. 类的加载: 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. 加载: 就是指将class文件读入内存,并为之 ...

  7. MVC,MVP 和 MVVM 的图示

    作者: 阮一峰 日期: 2015年2月 1日 复杂的软件必须有清晰合理的架构,否则无法开发和维护. MVC(Model-View-Controller)是最常见的软件架构之一,业界有着广泛应用.它本身 ...

  8. Js多国时间动态更新

    Js多国时间动态更新 点击下载

  9. jdbc连接数据库总结

    jdbc支持多种数据库,比如说oracle, mysql, mssql,现在总结一下连接各种数据库的相关知识 1,mysql连接,代码如下 Class.forName("com.mysql. ...

  10. jquery 添加节点的几种方法介绍

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...