BZOJ 1014 火星人prefix
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
7
Q 1 7
Q 4 8
Q 10 11
R 3 a
Q 1 7
I 10 a
Q 2 11
Sample Output
1
0
2
1
HINT
数据规模:
对于100%的数据,满足:
1、 所有字符串自始至终都只有小写字母构成。
2、 M <= 150,000
3、 字符串长度L自始至终都满足L <= 100,000
4、 询问操作的个数不超过10,000个。
对于第1,2个数据,字符串长度自始至终都不超过1,000
对于第3,4,5个数据,没有插入操作。
Source
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std; #define rhl 37
#define maxl 100010
#define maxm 150010
char s[maxl]; int len;
unsigned long long mi[maxl+maxm]; struct SPLAY
{
int ch[maxl+maxm][],fa[maxl+maxm],size[maxl+maxm],key[maxl+maxm],cnt,root;
unsigned long long hash[maxl+maxm]; inline void updata(int x)
{
size[x] = size[ch[x][]] + size[ch[x][]] + ;
hash[x] = hash[ch[x][]] * mi[size[ch[x][]]+] + mi[size[ch[x][]]] * (key[x]) + hash[ch[x][]];
} inline void rotate(int x)
{
int y = fa[x],z = fa[y],l,r;
if (ch[y][] == x) l = ; else l = ; r = l ^ ;
if (z != )
{
if (ch[z][] == y) ch[z][] = x; else ch[z][] = x;
}
fa[x] = z; fa[y] = x; fa[ch[x][r]] = y;
ch[y][l] = ch[x][r]; ch[x][r] = y;
updata(y); updata(x);
fa[] = ; ch[][] = ch[][] = ;
} inline void splay(int x,int aim)
{
int t = fa[aim];
while (fa[x] != t)
{
int y = fa[x],z = fa[y];
if (fa[y] != t)
{
if ((ch[y][] == x)^(ch[z][] == y)) rotate(x);
else rotate(y);
}
rotate(x);
}
if (aim == root) root = x;
} inline int find(int rank,int have,int now)
{
if (size[ch[now][]]+have+==rank) return now;
if (size[ch[now][]]+have+ > rank) return find(rank,have,ch[now][]);
else return find(rank,have+size[ch[now][]]+,ch[now][]);
} inline void init(int l,int r,int f)
{
int p; cnt = ; root = ;
build(l,r,f);
p = find(len,,root);
splay(p,root);
fa[++cnt] = p;
ch[p][] = cnt;
key[cnt] = ;
size[cnt] = ;
updata(p);
p = find(,,root);
splay(p,root);
fa[++cnt] = p;
ch[p][] = cnt;
key[cnt] = ;
size[cnt] = ;
updata(p);
} inline void build(int l,int r,int f)
{
fa[++cnt] = f;
if (l == r)
{
key[cnt] = s[l-] - 'a' + ;
hash[cnt] = key[cnt];
size[cnt] = ;
return;
}
int mid = (l+r)>>,now = cnt;
key[now] = s[mid-] - 'a' + ;
if (mid > l)
{
ch[now][] = cnt + ;
build(l,mid - ,now);
}
if (mid < r)
{
ch[now][] = cnt+;
build(mid+,r,now);
}
updata(now);
} inline void change(int a,char b)
{
int p = find(a+,,root);
splay(p,root);
key[p] = b-'a'+;
updata(p);
} inline void insert(int a,char b)
{
int p = find(a+,,root),q = find(a+,,root);
splay(p,root);
splay(q,ch[p][]);
fa[++cnt] = q;
ch[q][] = cnt; key[cnt] = b-'a'+;
updata(cnt);
updata(q);
updata(p);
} inline unsigned long long calc(int a,int b)
{
int p = find(a,,root),q = find(b+,,root);
splay(p,root);
splay(q,ch[p][]);
return hash[ch[q][]];
}
}tree; inline int ask(int a,int b)
{
int l = ,r = min(len-a+,len-b+),mid;
while (l <= r)
{
mid = (l + r) >> ;
unsigned long long h1 = tree.calc(a,a+mid-),h2 = tree.calc(b,b+mid-);
if (h1 == h2)
l = mid+;
else r = mid - ;
}
return r;
} inline void ready()
{
mi[] = ;
for (int i = ;i < maxl+maxm;++i)
mi[i] = rhl*mi[i-];
} inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} int main()
{
freopen("1014.in","r",stdin);
freopen("1014.out","w",stdout);
scanf("%s",s); len = strlen(s);
ready();
tree.init(,len,);
int T = read(); char opt;
while (T--)
{
scanf("%c",&opt);
if (opt == 'Q')
{
int a = read(),b = read();
printf("%d\n",ask(a,b));
}
else if (opt == 'R')
{
int a = read(); char b; scanf("%c\n",&b);
tree.change(a,b);
}
else
{
int a = read(); char b; scanf("%c\n",&b);
tree.insert(a,b); ++len;
}
}
fclose(stdin); fclose(stdout);
return ;
}
BZOJ 1014 火星人prefix的更多相关文章
- bzoj 1014 火星人prefix - 链表 - 分块
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- [BZOJ]1014 火星人prefix(JSOI2008)
一边听省队dalao讲课一边做题真TM刺激. BZOJ的discuss简直就是题面plus.大样例.SuperHINT.dalao题解的结合体. Description 火星人最近研究了一种操作:求一 ...
- bzoj 1014 火星人prefix —— splay+hash
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 用 splay 维护字符串上不同位置的哈希值还是第一次... 具体就是每个节点作为位置 ...
- BZOJ 1014 火星人 | 平衡树维护哈希
BZOJ 1014 火星人 题意 有一个字符串,三中操作:在某位置后面插入一个字符.修改某位置的字符.询问两个后缀的最长公共前缀. 题解 看到网上的dalao们都说这道题是平衡树,我就很懵x--平衡树 ...
- 【BZOJ】【1014】【JLOI2008】火星人prefix
Splay/二分/Hash 看了网上的题目关键字(都不用点进去看……我也是醉了)了解到做法= =那就上呗,前面做了好几道Splay的题就是为了练手搞这个的. Hash判断字符串是否相同应该很好理解吧? ...
- BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6243 Solved: 2007[Submit] ...
- BZOJ 1014: [JSOI2008]火星人prefix Splay+二分
1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...
- bzoj 1014: [JSOI2008]火星人prefix hash && splay
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3154 Solved: 948[Submit][ ...
- 求帮看!!!!BZOJ 1014 [JSOI2008]火星人prefix
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4164 Solved: 1277[Submit] ...
随机推荐
- Video标签播放视频?谷歌浏览器?safari?? 谷歌浏览器播放不了mp4格式的视频的原因
webm格式和mp4格式,判断了浏览器能否支持的视频类型后,给了一个if判断,如果是支持mp4格式,就返回视频后缀mp4,如果是webm,就返回后缀webm.结果,在谷歌浏览器中播放不了,为什么我指定 ...
- win8系统添加开机自启动软件的方法(转)
win8系统添加开机自启动软件的方法,把需要设置开机自启动的软件的快捷方式复制到下面任意路径就ok了.开机自启动路径如下:C:\ProgramData\Microsoft\Windows\Start ...
- PHP运行出现Notice : Use of undefined constant 的解决方法【已测】
关闭 PHP 提示的方法 搜索php.ini:error_reporting = E_ALL改为:error_reporting = E_ALL & ~E_NOTICE还有个不是办法的办法就是 ...
- hdu 3720 Arranging Your Team 枚举
不可能解可以直接判断. 搭配产生的附加分可以用一个二维数组保存. 枚举1442,4种类型的人,因为总人数只有23个,所以可以搜索暴力枚举,然后保存最优解. 注意trick,答案可能为负数,所以初始化a ...
- Androidannotation使用之@Rest与server交互的JSON数据转换(二)
开篇 之前的一篇博客:Androidannotation使用之@Rest获取资源及用户登录验证(一):http://blog.csdn.net/nupt123456789/article/detail ...
- 详细分享UICollectionView的自定义布局(瀑布流, 线性, 圆形…)
前言: 本篇文章不是分享collectionView的详细使用教程, 而是属于比较’高级’的collectionView使用技巧, 阅读之前, 我想你已经很熟悉collectionView的基本使用, ...
- Programming a Spider in Java 源码帖
Programming a Spider in Java 源码帖 Listing 1: Finding the bad links (CheckLinks.java) import java.awt. ...
- android开发之service详解
service作为android的四大组件之一,其重要性可想而知,在开发中,我们经常把一些不需要与用户进行交互的工作放在service中来完成,service运行在后台,这样有些人可能会产生错觉,以为 ...
- Java基础知识强化之集合框架笔记47:Set集合之TreeSet保证元素唯一性和比较器排序的原理及代码实现(比较器排序:Comparator)
1. 比较器排序(定制排序) 前面我们说到的TreeSet的自然排序是根据集合元素的大小,TreeSet将它们以升序排列. 但是如果需要实现定制排序,比如实现降序排序,则要通过比较器排序(定制排序)实 ...
- SQL Server中的20个系统变量
1.@@CONNECTIONS返回自上次启动 Microsoft SQL Server以来连接或试图连接的次数.示例:下面的示例显示了到当前日期和时间为止试图登录的次数.SELECT GETDATE( ...