JSOI 2008 火星人prefix Splay+hash

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函数的值。

Input

  第一行给出初始的字符串。第二行是一个非负整数M,表示操作的个数。接下来的M行,每行描述一个操作。操
作有3种,如下所示
1、询问。语法:Qxy,x,y均为正整数。功能:计算LCQ(x,y)限制:1<=x,y<=当前字符串长度。
2、修改。语法:Rxd,x是正整数,d是字符。功能:将字符串中第x个数修改为字符d。限制:x不超过当前字
符串长度。
3、插入:语法:Ixd,x是非负整数,d是字符。功能:在字符串第x个字符之后插入字符d,如果x=0,则在字
符串开头插入。限制:x不超过当前字符串长度

Output

  对于输入文件中每一个询问操作,你都应该输出对应的答案。一个答案一行。


思路:

这题需要支持修改字符串和查询LCP,而大多字符串算法都不支持修改.注意到哈希比较灵活,我们可以很方便地合并两段字符串的哈希值.

而插入和修改操作都是splay的基本操作,这样我们用splay维护哈希值,询问时二分答案,查询两段哈希值是否相等即可.这样单次查询复杂度的,然而数据保证查询不超过10000,可以AC.


代码:

#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstring>

#define For(i, j, k) for(int i = j; i <= k; i++)

using namespace std;

typedef unsigned long long ull;

const int N = 100010;
const ull BASE = 1000007;
ull base[N];

void init(){
	base[0] = 1;
	For(i, 1, N - 1) base[i] = base[i - 1] * BASE;
}

char S[N];

namespace Splay{
	int ch[N][2], fa[N], sz[N], val[N], root, e;
	ull hash[N];

	void pushup(int h){
		int lc = ch[h][0], rc = ch[h][1];
		sz[h] = sz[lc] + sz[rc] + 1;
		hash[h] = hash[lc] + base[sz[lc]] * val[h] + base[sz[lc] + 1] * hash[rc];
	}


	void build(int h, int l, int r){
		int M = (l + r) >> 1;
		val[h] = S[M], sz[h] = 1;
		if(l < M) ch[h][0] = ++e, fa[e] = h, build(e, l, M - 1);
		if(M < r) ch[h][1] = ++e, fa[e] = h, build(e, M + 1, r);
		pushup(h);
	}

	void Rotate(int h){
		int f = fa[h], g = fa[f], c = ch[f][1] == h;
		if(g) ch[g][ch[g][1] == f] = h; fa[h] = g;
		ch[f][c] = ch[h][c ^ 1], fa[ch[f][c]] = f;
		ch[h][c ^ 1] = f, fa[f] = h;
		if(root == f) root = h;
		pushup(f);
	}

	void splay(int h, int aim = 0){
		while(fa[h] != aim){
			int f = fa[h];
			if(fa[f] != aim) Rotate(f);
			Rotate(h);
		}
		pushup(h);
	}

	int kth(int x, int o = root){
		int t = sz[ch[o][0]];
		if(x <= t) return kth(x, ch[o][0]);
		else if(x == t + 1) return o;
		else return kth(x - t - 1, ch[o][1]);
	}

	void modify(int x, int w){
		x = kth(x);
		splay(x);
		val[x] = w;
		pushup(x);
	}

	void insert(int &o, int x, int w){
		if(!o) o = ++e, val[e] = w;
		else{
			if(x <= sz[ch[o][0]]) insert(ch[o][0], x, w), fa[ch[o][0]] = o;
			else insert(ch[o][1], x - sz[ch[o][0]] - 1, w), fa[ch[o][1]] = o;
		}
		pushup(o);
	}

	ull query(int x, int y){
		if(x == y) return val[x];
		splay(x), splay(y, x);
		int t = sz[ch[y][0]];
		return val[x] + hash[ch[y][0]] * base[1] + val[y] * base[t + 1];
	}

	int LCP(int u, int v){
		int x = kth(u), y = kth(v);
		if(val[x] != val[y]) return 0;
		int L = 1, R = min(e - v + 1, e - u + 1);
		while(L < R){
			int M = (L + R + 1) >> 1;
			int xr = kth(u + M - 1), yr = kth(v + M - 1);
			if(query(x, xr) == query(y, yr)) L = M;
			else R = M - 1;
		}
		return L;
	}
};

using namespace Splay;

int main(){
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	init();
	scanf("%s", S + 1);
	int n = strlen(S + 1), m;
	root = e = 1;
	build(root, 1, n);
	scanf("%d", &m);
	while(m--){
		char q[5];
		int x, y;
		scanf("%s%d", q, &x);
		if(q[0] == 'Q') scanf("%d", &y), printf("%d\n", LCP(x, y));
		else if(q[0] == 'I') scanf("%s", q), insert(root, x, q[0]);
		else scanf("%s", q), modify(x, q[0]);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值