看了《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. XBOX ONE游戏开发常见问题

    XBOX ONE游戏开发常见问题 终于弄懂这个在Unity的sdk在Account Picker切换账号的机制了,一个手柄注册一个账号,在游戏里面的时候,只有另外一个手柄选择自己的账号,系统的Acti ...

  2. [多图]Windows 10 Build 10565今推送:优化界面菜单 Cortana改进

    酷站网软:此前的Windows Build 10558并没有向公众发布,而是直到近日才向Fast Ring用户推送了更多功能和改进的Build 10565版.除之前版本上的加入了Messaging.E ...

  3. [转]mvc3 使用session来存储类来存储用户登陆信息

    mvc3 使用session来存储类来存储用户登陆信息 2013-08-26 09:48:56|  分类: NET开发 |举报 |字号 订阅   项目之前的登陆机制是这样的:用户登陆后初始化一个类,类 ...

  4. IDF实验室WORD隐写术

    上帝也哭泣http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=48 下载下来,发现是一个word文档,看来信息应该就 ...

  5. 修复垂直滑动RecyclerView嵌套水平滑动RecyclerView水平滑动不灵敏问题

    在 Android 应用中,大部分情况下都会使用一个垂直滚动的 View 来显示内容(比如 ListView.RecyclerView 等).但是有时候你还希望垂直滚动的View 里面的内容可以水平滚 ...

  6. java 继承(下)

    1.抽象方法一定要写在抽象类中. 2.抽象类只在描述该事事务应该具备的东西. 3.抽象只能是抽象类和抽象方法. 4,抽象类没有任何抽象方法,这种类是不让创建对象. private  static

  7. JavaScript 运算符

    JavaScript 运算符 JavaScript 运算符用于赋值,比较值,执行算术运算等. JavaScript 算术运算符 算术运算符用于执行两个变量或值的运算. 赋值 y = 5, 以下表格将向 ...

  8. mysql连接数设置操作(Too many connections)

    mysql在使用过程中,发现连接数超了~~~~ [root@linux-node1 ~]# mysql -u glance -h 192.168.1.17 -pEnter password: ERRO ...

  9. Kali linux渗透测试常用工具汇总1

    1.ProxyChains 简介:代理工具.支持HTTP/SOCKS4/SOCK5的代理服务器,允许TCP/DNS通过代理隧道. 应用场景:通过代理服务器上网. 配置:/etc/proxychains ...

  10. IE 和Firefox的js兼容性总结

    IE 和Firefox的js兼容性总结 12 August 2010 11:39 Thursday by 小屋 标签: 浏览器 方法 属性 IT 写法 一.函数和方法差异 1 . getYear()方 ...