看了《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. 给IIS添加CA证书以支持https

    一.在IIS中生成Certificate Signing Request (CSR) 个人理解:生成CSR就是生成“私钥/公钥对”之后从中提取出公钥. 1. 打开IIS Manager,在根节点中选择 ...

  2. openjudge8469特殊密码锁[贪心]

    描述 有一种特殊的二进制密码锁,由n个相连的按钮组成(n<30),按钮有凹/凸两种状态,用手按按钮会改变其状态. 然而让人头疼的是,当你按一个按钮时,跟它相邻的两个按钮状态也会反转.当然,如果你 ...

  3. 第18章 图元文件_18.2 增强型图元文件(emf)(1)

    18.2 增强型图元文件(emf) 18.2.1 创建并显示增强型图元文件的步骤 (1)创建:hdcEMF = CreateEnhMetaFile(hdcRef,szFilename,lpRect,l ...

  4. 第四章 Hibernate入门

    1.构建了一个Student实体类 public class Student { private Integer id; //name private String name; //age priva ...

  5. Linux系统资源使用情况

    概述: 用 'top -i' 看看有多少进程处于 Running 状态,可能系统存在内存或 I/O 瓶颈,用 free 看看系统内存使用情况,swap 是否被占用很多,用 iostat 看看 I/O ...

  6. 在ASP.NET中如何运行后台任务

    from:https://blogs.msdn.microsoft.com/scott_hanselman/2014/12/21/asp-net/ [原文发表地址] How to run Backgr ...

  7. NOIP2016提高组解题报告

    NOIP2016提高组解题报告 更正:NOIP day1 T2天天爱跑步 解题思路见代码. NOIP2016代码整合

  8. Java 集合系列17之 TreeSet详细介绍(源码解析)和使用示例

    概要 这一章,我们对TreeSet进行学习.我们先对TreeSet有个整体认识,然后再学习它的源码,最后再通过实例来学会使用TreeSet.内容包括:第1部分 TreeSet介绍第2部分 TreeSe ...

  9. Java集合系列:-----------03ArrayList源码分析

    上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具体实现类进行讲解:首先,讲解List,而List中ArrayList又最为常用.因此,本章我们讲解ArrayLi ...

  10. 获取iframe加载完毕事件

    function IframeLoad(o,fn) { if (document.all) { o.attachEvent('onload', fn); } else { o.onload = fn; ...