题目描述

火星人最近研究了一种操作:求一个字串两个后缀的公共前缀。比方说,有这样一个字符串: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函数的值。

输入

第一行给出初始的字符串。第二行是一个非负整数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不超过当前字符串长度

输出

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

样例输入

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

样例输出

5
1
0
2
1


题解

Splay+Hash+二分

求两个字符串的LCP,可以使用Hash+二分的方法。本题由于需要支持插入和修改字符,维护Hash值较为容易,因此采用Hash+二分的方法。

那么现在问题就转变为如何动态维护Hash值,支持插入与修改。需要Splay来完成。

使用Splay维护子树Hash值,然后每次pushup时合并Hash。

查询时二分长度,然后直接比较Hash值大小即可。

时间复杂度$O(n\log^2n)$。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100010
using namespace std;
typedef unsigned long long ull;
ull hash[N] , base[N];
int fa[N] , c[2][N] , si[N] , root , tot;
char s[N] , opt[5] , tmp[5];
void pushup(int x)
{
int l = c[0][x] , r = c[1][x];
si[x] = si[l] + si[r] + 1;
hash[x] = (hash[l] * 233 + s[x]) * base[si[r]] + hash[r];
}
void build(int l , int r , int f)
{
if(l > r) return;
int mid = (l + r) >> 1;
build(l , mid - 1 , mid) , build(mid + 1 , r , mid);
fa[mid] = f , c[mid > f][f] = mid;
pushup(mid);
}
void rotate(int &k , int x)
{
int y = fa[x] , z = fa[y] , l = (c[1][y] == x) , r = l ^ 1;
if(y == k) k = x;
else c[c[1][z] == y][z] = x;
fa[x] = z , fa[y] = x , fa[c[r][x]] = y , c[l][y] = c[r][x] , c[r][x] = y;
pushup(y) , pushup(x);
}
void splay(int &k , int x)
{
int y , z;
while(x != k)
{
y = fa[x] , z = fa[y];
if(y != k)
{
if((c[0][y] == x) ^ (c[0][z] == y)) rotate(k , x);
else rotate(k , y);
}
rotate(k , x);
}
}
int find(int k , int x)
{
if(x <= si[c[0][k]]) return find(c[0][k] , x);
else if(x > si[c[0][k]] + 1) return find(c[1][k] , x - si[c[0][k]] - 1);
else return k;
}
int split(int l , int r)
{
int a = find(root , l - 1) , b = find(root , r + 1);
splay(root , a) , splay(c[1][root] , b);
return c[0][c[1][root]];
}
int main()
{
int m , i , x , y , l , r , mid , ans;
ull tx , ty;
scanf("%s%d" , s + 2 , &m) , tot = strlen(s + 2) + 2;
base[0] = 1;
for(i = 1 ; i <= 100000 ; i ++ ) base[i] = base[i - 1] * 233;
build(1 , tot , 0) , root = (tot + 1) >> 1;
while(m -- )
{
scanf("%s%d" , opt , &x) , x ++ ;
if(opt[0] == 'R') scanf("%s" , tmp) , x = find(root , x) , splay(root , x) , s[x] = tmp[0] , pushup(x);
else if(opt[0] == 'I') scanf("%s" , tmp) , s[++tot] = tmp[0] , split(x + 1 , x) , c[0][c[1][root]] = tot , fa[tot] = c[1][root] , pushup(tot) , pushup(fa[tot]) , pushup(root);
else
{
scanf("%d" , &y) , y ++ ;
l = 1 , r = min(tot - x , tot - y) , ans = 0;
while(l <= r)
{
mid = (l + r) >> 1;
tx = hash[split(x , x + mid - 1)] , ty = hash[split(y , y + mid - 1)];
if(tx == ty) ans = mid , l = mid + 1;
else r = mid - 1;
}
printf("%d\n" , ans);
}
}
return 0;
}

【bzoj1014】[JSOI2008]火星人prefix Splay+Hash+二分的更多相关文章

  1. bzoj1014: [JSOI2008]火星人prefix splay+hash+二分

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  2. BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8112  Solved: 2569[Submit] ...

  3. bzoj1014: [JSOI2008]火星人prefix splay+hash

    我写的代码好像自古以来就是bzoj不友好型的 本地跑的比std快,但是交上去巧妙被卡 答案...应该是对的,拍了好久了 #include <bits/stdc++.h> #define M ...

  4. 【BZOJ1014】[JSOI2008]火星人prefix Splay+hash

    [BZOJ1014][JSOI2008]火星人prefix Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个 ...

  5. BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )

    用splay维护序列, 二分+hash来判断LCQ.. #include<bits/stdc++.h> using namespace std; typedef unsigned long ...

  6. P4036 [JSOI2008]火星人(splay+hash+二分)

    P4036 [JSOI2008]火星人 Splay维护hash,查询二分 $a[x].vl=a[lc].vl*ha[a[rc].sz+1]+a[x].w*ha[a[rc].sz]+a[rc].vl$ ...

  7. BZOJ1014: [JSOI2008]火星人prefix(splay 二分 hash)

    题意 题目链接 Sol 一眼splay + 二分hash,不过区间splay怎么写来着呀 试着写了两个小时发现死活不对 看了一下yyb的代码发现自己根本就不会splay.... // luogu-ju ...

  8. [BZOJ1014] [JSOI2008] 火星人prefix (splay & 二分答案)

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  9. BZOJ1014[JSOI2008]火星人prefix(splay维护hash)

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

随机推荐

  1. IE浏览器已经卸载,但是桌面上的图标却无法删除的解决方案

    第一步——win+R运行[regedit],打开注册表. 第二步——在注册表里面依次找到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVer ...

  2. 爬取豆瓣电影top250并存储到mysql数据库

    import requests from lxml import etree import re import pymysql import time conn= pymysql.connect(ho ...

  3. Android系统Binder机制学习总结

    一.Binder机制概述 在Android开发中,很多时候我们需要用到进程间通信,所谓进程间通信,实现进程间通信的机制有很多种,比如说socket.pipe等,Android中进程间通信的方式主要有三 ...

  4. [opencv] applyColorMap

    applyColorMap 功能 转化为热力图,因为热力图我们看的变化更加细微,在很多地方都用到了热力图. 最近在看CAM,所以记一记这个函数. 感觉还是很有用的. 代码 >>> i ...

  5. php的字符转换 & php登入注册界面设计以及源码 & 分离公共部分

    我们在编写的时候总是会出现乱码 https://www.cnblogs.com/mafeng/p/5827215.html php登入注册界面设计以及源码 https://blog.csdn.net/ ...

  6. iOS应用架构谈-part2 view层的组织和调用方案

    前言 <iOS应用架构谈 开篇>出来之后,很多人来催我赶紧出第二篇.这一篇文章出得相当艰难,因为公司里的破事儿特别多,我自己又有点私事儿,以至于能用来写博客的时间不够充分. 现在好啦,第二 ...

  7. 简单的Datable转List方法

    public static class DataTableUtils<T> where T : new() { public static List<T> ConvertToM ...

  8. echarts事件中获取当前实例

    直接使用this即可

  9. C#基础-数组-冒泡排序

    冒泡排序基础 冒泡排序原理图分析 tmp在算法中起到数据交换的作用 int[] intNums = { 12,6,9,3,8,7 }; int tmp = intNums[0]; // 一共5次冒泡, ...

  10. kali下安装中文输入法

    参考网址:https://blog.csdn.net/qq_37367124/article/details/79229739 更性源 vim /etc/apt/source.list 设置更新源 更 ...