zoukankan      html  css  js  c++  java
  • BZOJ1014火星人prefix Splay維護序列 + 字符串哈希

    @[Splay, 哈希]

    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

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

    Sample Input

    madamimadam
    7
    Q 1 7
    Q 4 8
    Q 10 11
    R 3 a
    Q 1 7
    I 10 a
    Q 2 11
    

    Sample Output

    5
    1
    0
    2
    1
    

    HINT

    1、所有字符串自始至终都只有小写字母构成。
    2、M<=150,000
    3、字符串长度L自始至终都满足L<=100,000
    4、询问操作的个数不超过10,000个。
    对于第1,2个数据,字符串长度自始至终都不超过1,000
    对于第3,4,5个数据,没有插入操作。

    Solution

    首先吐槽一下題目

    火星人发现了这样的一个关联:如果把该字符串的所有后缀排好序,就可以很快地求出LCQ函数的值;同样,如果求出了LCQ函数的值,也可以很快地将该字符串的后缀排好序.

    這都是什麼鬼.. 完全是誤導..
    這一題正解是用Splay維護帶有插入的區間, 再用二分加Hash來判斷兩個子串是否相同.
    首先是要對Hash的性質熟悉. 兩個串在知道長度的情況下, 將其首尾相接, 是可以在(O(1))的時間內得到新的Hash值的. 有了這個性質, 才能對一個節點(O(1))進行更新.
    然後是Splay維護區間. Splay維護區間的題到目前來說做得很少, 但實際上其應用還是很廣泛的. 操作利用了Splay的旋轉操作, 以及二叉搜索树的性質. 具體來說, 有以下兩種操作:

    1. 插入: 假設要在第(x)和第(x + 1)位之間進行插入, 則將第(x)(x + 1)位分別旋轉至根和根節點的子節點上(即確保這兩個點直接相連). 這樣的好處就是, 可以確保(x + 1)這個點的左子樹一定是空的. 這時, 直接在左子樹上加入新节点即可.
    2. 查詢: 假設要查詢((L, R))這一段區間, 則按插入的方法將兩個節點上旋, 這時, (R)的左子樹即是需要查詢的整個範圍.

    嗯, 大概就是這麼多. 原理實際上稍微想想就可以理解了.
    然後這一題還要注意分別在字符串首尾加上一位, 因为插入和查詢时要访问到需要用到的区间的前一位和后一位. 具體細節看代碼吧(代碼好難寫.. QAQ)

    #include<cstdio>
    #include<cctype>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    inline void read(char *s)
    {
    	char c;
    	while(! isgraph(c = getchar()));
    	int len = 0;
    	while(isgraph(c))
    		s[len ++] = c, c = getchar();
    }
    inline void read(int &x)
    {
    	x = 0;
    	char c;
    	while(! isdigit(c = getchar()));
    	while(isdigit(c))
    		x = x * 10 + c - '0', c = getchar();
    }
    inline void read(char &c)
    {
    	while(! isgraph(c = getchar()));
    }
    const int L = 1 << 18;
    int pwr[L];
    char s[L];
    int top;
    int root;
    struct Node
    {
    	int size, key, suc[2], pre;
    	char c;
    	Node(){}
    	Node(int L, int R, int _pre, int _c)
    	{
    		suc[0] = L, suc[1] = R, pre = _pre, c = _c;
    	} 
    }T[L];
    void update(int u)
    {
    	T[u].size = (~ T[u].suc[0] ? T[T[u].suc[0]].size : 0)
    		+ (~ T[u].suc[1] ? T[T[u].suc[1]].size : 0)
    		+ 1;
    	T[u].key = (~ T[u].suc[0] ? T[T[u].suc[0]].key : 0)
    		+ T[u].c * pwr[~ T[u].suc[0] ? T[T[u].suc[0]].size : 0]
    		+ (~ T[u].suc[1] ? pwr[~ T[u].suc[0] ? T[T[u].suc[0]].size + 1 : 1] * T[T[u].suc[1]].key : 0);
    }
    int Build(int L, int R, int pre)
    {
    	if(L > R)
    		return - 1;
    	int mid = L + R >> 1;
    	int u = top ++;
    	T[u] = Node(Build(L, mid - 1, u), Build(mid + 1, R, u), pre, s[mid]);
    	update(u);
    	return u; 
    }
    int Find(int u, int k)
    {
    	if(k == (~ T[u].suc[0] ? T[T[u].suc[0]].size : 0) + 1)
    		return u;
    	else if(k > (~ T[u].suc[0] ? T[T[u].suc[0]].size : 0))
    		return Find(T[u].suc[1], k - (~ T[u].suc[0] ? T[T[u].suc[0]].size : 0) - 1);
    	else
    		return Find(T[u].suc[0], k);
    }
    void Rotate(int u)
    {
    	int pre = T[u].pre, prepre = T[T[u].pre].pre, k = u == T[T[u].pre].suc[1];
    	T[pre].suc[k] = T[u].suc[k ^ 1];
        T[T[u].suc[k ^ 1]].pre = pre;
        T[u].suc[k ^ 1] = pre;
        T[pre].pre = u;
        T[u].pre = prepre;
        if(prepre != - 1)
            T[prepre].suc[T[prepre].suc[1] == pre] = u;
        if(root == pre)
        	root = u;
        update(pre), update(u);
    }
    void Splay(int u, int rt)
    {
    	while(T[u].pre != rt)
    	{
    		if(T[T[u].pre].pre != rt)
    			Rotate((u == T[T[u].pre].suc[0]) == (T[u].pre == T[T[T[u].pre].pre].suc[0]) ? T[u].pre : u);
    		Rotate(u);
    	}
    }
    inline void println(int x)
    {
    	if(x == 0)
    		putchar('0');
    	if(x < 0)
    		putchar('-'), x *= - 1;
    	int top = 0, ans[1 << 4];
    	while(x)
    		ans[top ++] = x % 10, x /= 10;
    	for(; top; top --)
    		putchar(ans[top - 1] + '0');
    	putchar('
    ');
    }
    int main()
    {
    	pwr[0] = 1;
    	for(int i = 1; i < L; i ++)
    		pwr[i] = pwr[i - 1] * 31;
    	#ifndef ONLINE_JUDGE
    	freopen("BZOJ1014.in", "r", stdin);
    	freopen("BZOJ1014.out", "w", stdout);
    	#endif
    	int m;
    	read(s + 1), read(m);
    	int n = strlen(s + 1);
    	s[0] = s[n + 1] = 0;
    	top = 0;
    	root = Build(0, n + 1, - 1);
    	for(int i = 0; i < m; i ++)
    	{
    		char opt;
    		read(opt);
    		if(opt == 'I')
    		{
    			int x;
    			char c;
    			read(x), read(c);
    			int u = Find(root, x + 1);
    			int v = Find(root, x + 2);
    			Splay(u, - 1);
    			Splay(v, u);
    			u = top ++;
    			T[u] = Node(- 1, - 1, v, c);
    			T[v].suc[0] = u;
    			update(u), update(v), update(T[v].pre);
    			n ++;
    		}
    		if(opt == 'R')
    		{
    			int x;
    			char c;
    			read(x), read(c);
    			int u = Find(root, x + 1);
    			Splay(u, - 1);
    			T[u].c = c;
    			update(u);
    		}
    		if(opt == 'Q')
    		{
    			int x, y;
    			read(x), read(y);
    			if(x == y)
    			{
    				println(n - x + 1);
    				continue;
    			}
    			if(x > y)
    				swap(x, y);
    			int L = 0, R = n, ans = 0;
    			while(L <= R)
    			{
    				int mid = L + R >> 1;
    				if(y + 1 + mid > n + 2)
    				{
    					R = mid - 1;
    					continue; 
    				}
    				int u = Find(root, x), v = Find(root, x + 1 + mid);
    				Splay(u, - 1), Splay(v, u);
    				int hax = (~ T[v].suc[0] ? T[T[v].suc[0]].key : 0);
    				u = Find(root, y), v = Find(root, y + 1 + mid);
    				Splay(u, - 1), Splay(v, u);
    				int hay = (~ T[v].suc[0] ? T[T[v].suc[0]].key : 0);
    				if(hax == hay)
    					ans = max(ans, mid), L = mid + 1;
    				else
    					R = mid - 1; 
    			}
    			println(ans);
    		}
    	}
    }
    
    
  • 相关阅读:
    shell脚本学习001
    Discuz 代码分析 001 forum.php
    Oauth2.0 入门
    第一篇文章
    position的四个属性值: relative ,absolute ,fixed,static
    JQuery中stop([clearQueue],[goToEnd])介绍
    <meta>
    sublime 2中Package control安装和使用
    ios 状态码
    <video>和<audio>标签,对视频和音频的支持
  • 原文地址:https://www.cnblogs.com/ZeonfaiHo/p/6483079.html
Copyright © 2011-2022 走看看