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、 询问。语法:Q x y,x, y均为正整数。功能:计算LCQ(x, y) 限制:1 <= x, y <= 当前字符串长度。 2、 修改。语法:R x d,x是正整数,d是字符。功能:将字符串中第x个数修改为字符d。限制:x不超过当前字符串长度。 3、 插入:语法:I x d,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
数据规模:
对于100%的数据,满足:
1、 所有字符串自始至终都只有小写字母构成。
2、 M <= 150,000
3、 字符串长度L自始至终都满足L <= 100,000
4、 询问操作的个数不超过10,000个。

对于第1,2个数据,字符串长度自始至终都不超过1,000
对于第3,4,5个数据,没有插入操作。

看了题解,知道使用splay维护hash值,但是严重超时(自己评测还好,bzoj上时间是两倍多)

没办法,看AC代码,对着调,发现取得hash值的方法不同

它的做法:先把l旋到根,然后向右边遍历,取hash值,取完之后如果发现深度太大(根据自己的情况而定,它取得40,我取的23)就把它旋到根,在程序里用一个step记录深度

 type
node=record
son:array[..]of longint;
fa,a,size:longint;
hash:int64;
end;
const
maxn=;
h=;
var
tree:array[..maxn]of node;
n,root,len,tot:longint;
mi:array[..maxn]of int64;
s:array[..maxn]of char; procedure swap(var x,y:longint);
var
t:longint;
begin
t:=x;x:=y;y:=t;
end; procedure new(x:longint);
begin
with tree[x] do
begin
size:=+tree[son[]].size+tree[son[]].size;
hash:=((tree[son[]].hash*+a)*mi[tree[son[]].size]+tree[son[]].hash)mod h;
end;
end; function build(l,r,f:longint):longint;
var
mid:longint;
begin
mid:=(l+r)>>;
if (mid<>) and (mid<>len+) then tree[mid].a:=ord(s[mid-])-ord('a')+;
with tree[mid] do
begin
fa:=f;
hash:=a;
if mid>l then son[]:=build(l,mid-,mid);
if mid<r then son[]:=build(mid+,r,mid);
end;
new(mid);
exit(mid);
end; procedure init;
var
i:longint;
begin
while not eoln do
begin
inc(len);
read(s[len]);
end;
mi[]:=;
for i:= to do
mi[i]:=mi[i-]* mod h;
root:=build(,len+,);
tot:=len+;
end; procedure rotate(x,w:longint);
var
y:longint;
begin
with tree[x] do
begin
y:=fa;
tree[y].son[w]:=son[ xor w];
if son[ xor w]<> then tree[son[ xor w]].fa:=fa;
son[ xor w]:=y;
if root=y then root:=x
else
if tree[tree[y].fa].son[]=y then tree[tree[y].fa].son[]:=x
else tree[tree[y].fa].son[]:=x;
fa:=tree[y].fa;
tree[y].fa:=x;
new(y);
end;
end; procedure splay(x,z:longint);
begin
with tree[x] do
while fa<>z do
begin
if tree[fa].fa=z then
if tree[fa].son[]=x then rotate(x,)
else rotate(x,)
else
begin
if tree[fa].son[]=x then
if tree[tree[fa].fa].son[]=fa then
begin
rotate(fa,);
rotate(x,);
end
else
begin
rotate(x,);
rotate(x,);
end
else
if tree[tree[fa].fa].son[]=fa then
begin
rotate(x,);
rotate(x,);
end
else
begin
rotate(fa,);
rotate(x,);
end;
end;
end;
new(x);
end; function find(x:longint):longint;
begin
find:=root;
while x<> do
with tree[find] do
begin
if x<=tree[son[]].size then find:=son[]
else
if x=tree[son[]].size+ then exit(find)
else
begin
dec(x,tree[son[]].size+);
find:=son[];
end;
end;
end; function get(l,len:longint):int64;
var
now,step:longint;
begin
splay(find(l+),);
now:=tree[root].son[];
dec(len);
step:=;
get:=tree[root].a;
while len> do
begin
inc(step);
if len=tree[now].size then
begin
get:=(get*mi[len]+tree[now].hash)mod h;
if step> then splay(now,);
exit;
end;
if len=tree[tree[now].son[]].size+ then
begin
get:=(get*mi[len]+tree[tree[now].son[]].hash*+tree[now].a)mod h;
if step> then splay(now,);
exit;
end;
if len<=tree[tree[now].son[]].size then now:=tree[now].son[]
else
begin
get:=(get*mi[tree[tree[now].son[]].size+]+tree[tree[now].son[]].hash*+tree[now].a)mod h;
dec(len,tree[tree[now].son[]].size+);
now:=tree[now].son[];
end;
end;
end; procedure q;
var
l,r,left,right,mid:longint;
begin
readln(l,r);
if l>r then swap(l,r);
left:=;
right:=len-r+;
while left<right do
begin
mid:=(left+right+)>>;
if get(l,mid)=get(r,mid) then left:=mid
else right:=mid-;
end;
writeln(left);
end; procedure r;
var
s:char;
x:longint;
begin
read(x,s,s);
splay(find(x+),);
tree[root].a:=ord(s)-ord('a')+;
new(root);
readln;
end; procedure ins;
var
s:char;
x:longint;
begin
inc(len);
read(x,s,s);
x:=find(x+);
splay(x,);
inc(tot);
with tree[tot] do
begin
a:=ord(s)-ord('a')+;
son[]:=tree[x].son[];
tree[son[]].fa:=tot;
tree[x].son[]:=tot;
fa:=x;
end;
new(tot);
new(x);
readln;
end; procedure work;
var
s:char;
i,m:longint;
begin
readln(m);
for i:= to m do
begin
read(s);
case s of
'Q':q;
'R':r;
'I':ins;
end;
end;
end; begin
init;
work;
end.

1014: [JSOI2008]火星人prefix - BZOJ的更多相关文章

  1. BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】

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

  2. BZOJ 1014: [JSOI2008]火星人prefix Splay+二分

    1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...

  3. bzoj 1014: [JSOI2008]火星人prefix hash && splay

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

  4. 求帮看!!!!BZOJ 1014 [JSOI2008]火星人prefix

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

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

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

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

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

  7. 1014: [JSOI2008]火星人prefix

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Description 火星人最近研究了一种操作:求一个字串两个后缀 ...

  8. [BZOJ 1014] [JSOI2008] 火星人prefix 【Splay + Hash】

    题目链接:BZOJ - 1014 题目分析 求两个串的 LCP ,一种常见的方法就是 二分+Hash,对于一个二分的长度 l,如果两个串的长度为 l 的前缀的Hash相等,就认为他们相等. 这里有修改 ...

  9. BZOJ 1014: [JSOI2008]火星人prefix

    Sol Splay+Hash+二分答案. 用Splay维护Hash,二分答案判断. 复杂度 \(O(nlog^2n)\) PS:这题调了两个晚上因为没开long long.许久不写数据结构题感觉写完整 ...

随机推荐

  1. Ajax返回xml类型数据

    ajax可以返回文本类型数据和xml类型数据,xml是计算机通用语言 可以使用js解析返回xml类型数据的dom对象 前端页面 <!doctype html> <html lang= ...

  2. struts2-权限拦截器、日志拦截器、execAndWait(进度条)拦截器配置

    1.权限拦截器 package login; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.Serv ...

  3. iOS常见面试题汇总

    iOS常见面试题汇总 1. 什么是 ARC? (ARC 是为了解决什么问题而诞生的?) ARC 是 Automatic Reference Counting 的缩写, 即自动引用计数. 这是苹果在 i ...

  4. Objective-C 【单个对象内存管理(野指针&内存泄露)】

    ------------------------------------------- 单个对象内存管理 (1)野指针 ①定义了一个指针变量,但是并没有赋初值,它随机指向一个东西 ②某指针变量指向的内 ...

  5. HTML5的Web SQL Database

    本文将介绍 Web SQL Database 规范中定义的三个核心方法: openDatabase:这个方法使用现有数据库或新建数据库来创建数据库对象 transaction:这个方法允许我们根据情况 ...

  6. Poj 3117 World Cup

    1.Link: http://poj.org/problem?id=3117 2.Content: World Cup Time Limit: 1000MS   Memory Limit: 65536 ...

  7. [转]C#发送Email邮件 (实例:QQ邮箱和Gmail邮箱)

    下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号. 需要引用:using System.Net.Mail;using System.Text;using System.Net; 程序 ...

  8. libevent 初试

    一直就想用一下libevent库,直到今天才去码代码.用法在他的头文件里面写的很清楚,原理的话也不是很难,感谢作者做的工作! 今天做了几个探索: 实现自定义事件类型的设置与触发 尝试 setInter ...

  9. PHP基础在线视频教程高清版

    这个是我给大家介绍的PHP基础在线视频教程高清版,目前PHP慢慢垄断WEB的语言编写市场了,PHP 是一种服务器内置式的script 语言,它的出现使得在unix上快速地开发动态web成为现实.PHP ...

  10. mysql 报Row size too large 65535 原因与解决方法

    报错信息:Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535 ...