[BZOJ1014][JSOI2008][Splay][RKHash]火星人prefix
来源:互联网 发布:java新开线程 编辑:程序博客网 时间:2024/05/17 08:24
[Problem Description]
火星人最近研究了一种操作:求一个字串两个后缀的公共前缀。比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 11 字符 m a d a m i m a d a m 现在,火星人定义了一个函数LCQ(x, y),表示:该字符串中第x个字符开始的字串,与该字符串中第y个字符开始的字串,两个字串的公共前缀的长度。比方说,LCQ(1, 7) = 5, LCQ(2, 10) = 1, LCQ(4, 7) = 0 在研究LCQ函数的过程中,火星人发现了这样的一个关联:如果把该字符串的所有后缀排好序,就可以很快地求出LCQ函数的值;同样,如果求出了LCQ函数的值,也可以很快地将该字符串的后缀排好序。 尽管火星人聪明地找到了求取LCQ函数的快速算法,但不甘心认输的地球人又给火星人出了个难题:在求取LCQ函数的同时,还可以改变字符串本身。具体地说,可以更改字符串中某一个字符的值,也可以在字符串中的某一个位置插入一个字符。地球人想考验一下,在如此复杂的问题中,火星人是否还能够做到很快地求取LCQ函数的值。
[Algorithm & Data Structure]
Splay + Hash
[Analysis]
首先这肯定不是后缀数组的那个lcq(否则就这么直接说出来岂不是有点太裸了……)然后有插入有修改神马的自然让人联想到splay。维护字符串好维护,但是如何判lcq呢?可以二分答案……但是如何在O(1)的时间里判断splay上的两个区间是否相等?这里用了一种靠RP的方法——RKHash。其实就是把字符串转成一个26进制数(当然如果把头尾的两个虚拟节点算上就是27进制)然后再mod一个合适的数(合适的数是啥……看自己RP了……)。这样两个不同的字符串hash值相同的几率比较小。splay上每一个节点维护一个hash值,update的时候顺便就更新了
[Pay Attention]
蛋疼啊刚开始维护了一个当前字符串长度len结果插入的时候没有更新……导致wa一片还不知道错哪里了……后来又是一片tle全卡在1s多一diudiu。然后把longlong改成int就过了……少用longlong啊少用longlong(mod效率特别渣……)
[Code]
/************************************************************** Problem: 1014 User: gaotianyu1350 Language: C++ Result: Accepted Time:10540 ms Memory:4148 kb****************************************************************/ #include <cstdio>#include <cstring>#include <cmath>#include <cstring>#include <iostream>#include <queue>using namespace std; const int MAXLEN = 101000;const int MOD = 9875321;const int bit = 27; int poww[MAXLEN] = {0};int size[MAXLEN] = {0}, cc[MAXLEN] = {0}, hashsh[MAXLEN] = {0};int ch[MAXLEN][2] = {0}, f[MAXLEN] = {0};int root = 0, tot = 0;int len, m;char s[MAXLEN]; inline void init(){ poww[0] = 1; for (int i = 1; i < MAXLEN; i++) poww[i] = ((long long)poww[i - 1] * bit) % MOD;} inline void update(int now){ size[now] = size[ch[now][0]] + 1 + size[ch[now][1]]; hashsh[now] = (hashsh[ch[now][0]] + ((long long)poww[size[ch[now][0]]] * cc[now]) % MOD + ((long long)poww[size[ch[now][0]] + 1] * hashsh[ch[now][1]]) % MOD) % MOD;} inline int get(int now){ return ch[f[now]][0] == now ? 0 : 1;} inline void rotate(int now){ int old = f[now], oldfather = f[old], which = get(now); ch[old][which] = ch[now][which ^ 1]; f[ch[old][which]] = old; f[old] = now; ch[now][which ^ 1] = old; f[now] = oldfather; if (oldfather) ch[oldfather][ch[oldfather][0] == old ? 0 : 1] = now; update(old); update(now);} inline void splay(int now, int tar){ for (int father = f[now]; father != tar; rotate(now), father = f[now]) if (f[father] != tar) get(now) == get(father) ? rotate(father) : rotate(now); if (tar == 0) root = now;} inline int findkth(int k){ int now = root; while (now) { if (k == size[ch[now][0]] + 1) return now; if (k <= size[ch[now][0]]) now = ch[now][0]; else k -= size[ch[now][0]] + 1, now = ch[now][1]; } return 0;} inline void insert(int k, char c){ k++; int kpoint = findkth(k), knpoint = findkth(k + 1); splay(kpoint, 0); splay(knpoint, kpoint); if (ch[knpoint][0]) printf("Wrong at 84\n"); int now = ++tot; size[now] = 1; cc[now] = c - 'a'; hashsh[now] = cc[now]; ch[now][0] = ch[now][1] = 0; f[now] = knpoint; ch[knpoint][0] = now; update(knpoint); update(kpoint);} inline void change(int k, char c){ k++; int now = findkth(k); splay(now, 0); cc[now] = c - 'a'; update(now);} inline void print(int now){ if (ch[now][0]) print(ch[now][0]); putchar(cc[now] + 'a'); if (ch[now][1]) print(ch[now][1]);} inline int calc(int l, int r, int op){ if (l > r) return 0; int ln = findkth(l - 1), rn = findkth(r + 1); splay(ln, 0); splay(rn, ln); if (!ch[rn][0]) printf("Wrong at 106!\n"); if (op == 1) { print(ch[rn][0]); putchar('\n'); } return hashsh[ch[rn][0]];} inline int lcq(int x, int y){ x++; y++; if (x < y) { int temp = x; x = y; y = temp; } int left = 0, right = (len + 1) - x + 1; while (left < right) { int mid = (left + right + 1) >> 1; if (calc(x, x + mid - 1, 0) == calc(y, y + mid - 1, 0)) left = mid; else right = mid - 1; } //calc(x, x + left - 1, 1); //calc(y, y + left - 1, 1); return left;} int build(int father, int left, int right){ if (left > right) return 0; int now = ++tot; int mid = (left + right) >> 1; cc[now] = s[mid] - 'a'; f[now] = father; ch[now][0] = build(now, left, mid - 1); ch[now][1] = build(now, mid + 1, right); update(now); return now;} int main(){ init(); //freopen("input.txt", "r", stdin); //freopen("user.out", "w", stdout); scanf("%s", s + 1); len = strlen(s + 1); s[len + 1] = s[0] = 'a' + 27 - 1; root = build(0, 0, len + 1); scanf("%d", &m); while (m--) { char command, theC; int x, y; scanf(" %c", &command); switch (command) { case 'Q': scanf("%d%d", &x, &y); printf("%d\n", lcq(x, y)); break; case 'R': scanf("%d %c", &x, &theC); change(x, theC); break; case 'I': len++; scanf("%d %c", &x, &theC); insert(x, theC); break; } } //print(root);}
0 0
- [BZOJ1014][JSOI2008][Splay][RKHash]火星人prefix
- bzoj1014: [JSOI2008]火星人prefix splay
- [bzoj1014][splay][JSOI2008]火星人prefix
- 【bzoj1014】[JSOI2008]火星人prefix splay+hash+二分
- 【bzoj1014】【jsoi2008】【火星人prefix】【splay+hash】
- 【BZOJ1014】[JSOI2008]火星人prefix【Splay】【Hash】
- [splay+二分+哈希] BZOJ1014: [JSOI2008]火星人prefix
- bzoj1014: [JSOI2008]火星人prefix(Splay)
- bzoj1014 [JSOI2008]火星人prefix ( splay + hash )
- 【BZOJ1014】[JSOI2008]火星人prefix
- [BZOJ1014] [JSOI2008]火星人prefix
- bzoj1014 [JSOI2008]火星人prefix
- bzoj1014: [JSOI2008]火星人prefix
- [BZOJ1014][JSOI2008]火星人prefix
- 【JSOI2008】【BZOJ1014】火星人prefix
- [BZOJ1014] [JSOI2008]火星人prefix
- bzoj1014: [JSOI2008]火星人prefix
- 【BZOJ1014】【JSOI2008】火星人prefix
- 设置光标的位置
- eXtremeDB xsql usage(2)
- JS获取URL参数
- VMware的“Intel VT-x is disabled”解决方法
- BFC是什么?
- [BZOJ1014][JSOI2008][Splay][RKHash]火星人prefix
- Oracle MERGE INTO的用法
- 项目中js的工具类
- 至少从它占据的市场份额来说
- java enum详解
- NGUI必备概念和理论(一)【NGUI3.5.8】
- Unix下/usr/include头文件
- 域名的结构
- Java基础_异常