1014: [JSOI2008]火星人prefix

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 3154  Solved: 948
[Submit][Status]

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

HINT

数据规模:

对于100%的数据,满足:

1、 所有字符串自始至终都只有小写字母构成。

2、 M <= 150,000

3、 字符串长度L自始至终都满足L <= 100,000

4、 询问操作的个数不超过10,000个。

对于第1,2个数据,字符串长度自始至终都不超过1,000

对于第3,4,5个数据,没有插入操作。

  这道题在知道用hash做之后,还是不会,其实这道题用hash转换为另一道题,给定一个序列,支持插入,改变值,询问区间hash值,这样就可以用splay维护。

  一个需要注意的东西,字符串hash+自然溢出时,如果进制用的是偶数(26)是会挂掉滴。

  splay写的还是太慢了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXT 400000
#define MAXN 210000
unsigned int mi[MAXN];
struct Splay_tree
{
int ch[MAXT][],pnt[MAXT],val[MAXT],siz[MAXT];
unsigned int hs[MAXT];
int root;
int topt;
void update(int now)
{
siz[now]=siz[ch[now][]]+siz[ch[now][]]+;
hs[now]=hs[ch[now][]] + val[now]*mi[siz[ch[now][]]] + hs[ch[now][]]*(mi[siz[ch[now][]]+]);
}
void rotate(int now)
{
int p=pnt[now],anc=pnt[p];
int dir=(ch[p][]==now);
if (anc)ch[anc][ch[anc][]==p]=now;
pnt[now]=anc;
pnt[ch[now][dir]]=p;
ch[p][-dir]=ch[now][dir];
pnt[p]=now;
ch[now][dir]=p;
update(p);
update(now);
}
void splay(int now,int tp=)
{
while (now!=tp && pnt[now]!=tp)
{
int p=pnt[now],anc=pnt[p];
if (anc==tp)
{
rotate(now);
}else
{
if((ch[anc][]==p) == (ch[p][]==now))
rotate(p),rotate(now);
else
rotate(now),rotate(now);
}
}
if (!tp)
root=now;
return ;
}
int get_kth(int now,int rk)
{
if (!now)return -;
if (siz[ch[now][]]+==rk)
return now;
else if (siz[ch[now][]]+<rk)
return get_kth(ch[now][],rk-siz[ch[now][]]-);
else return get_kth(ch[now][],rk);
}
void insert(int pos,int v)
{
if (!root)
{
root=++topt;
val[topt]=v;
pnt[topt]=;
update(topt);
return ;
}
if (!pos)
{
splay(get_kth(root,),);
ch[root][]=++topt;
ch[topt][]=ch[topt][]=;
val[topt]=v;
pnt[topt]=root;
update(topt);
update(root);
return ;
}
splay(get_kth(root,pos));
if (pos+<=siz[root])
splay(get_kth(root,pos+),root);
//scan(root);
if (!ch[root][])
{
ch[root][]=++topt;
pnt[topt]=root;
}else
{
ch[ch[root][]][]=++topt;
pnt[topt]=ch[root][];
}
ch[topt][]=ch[topt][]=;
val[topt]=v;
update(topt);
if (ch[root][]!=topt)
update(ch[root][]);
update(root);
}
void change_val(int pos,int v)
{
splay(get_kth(root,pos));
val[root]=v;
update(root);
}
unsigned int query(int l,int r)
{
if (l== && r==siz[root])
{
// scan(root);
return hs[root];
}else if (l==)
{
splay(get_kth(root,r+));
// scan(ch[root][0]);printf("\n");
return hs[ch[root][]];
}else if (r==siz[root])
{
splay(get_kth(root,l-));
// scan(ch[root][1]);printf("\n");
return hs[ch[root][]];
}else
{
splay(get_kth(root,r+));
splay(get_kth(root,l-),root);
// scan(ch[ch[root][0]][1]);printf("\n");
return hs[ch[ch[root][]][]];
}
}
void scan(int now)
{
if (!now)
return ;
if (ch[now][] && pnt[ch[now][]]!=now)throw ;
if (ch[now][] && pnt[ch[now][]]!=now)throw ;
if (siz[now]!=siz[ch[now][]]+siz[ch[now][]]+)throw ;
if (hs[now]!=hs[ch[now][]]+val[now]*mi[siz[ch[now][]]]+hs[ch[now][]]*(mi[siz[ch[now][]]+]))throw ;
scan(ch[now][]);
printf("%d[%c] ",val[now],val[now]+'a');
scan(ch[now][]);
}
}pp;
char str[MAXN];
int main()
{
freopen("input.txt","r",stdin);
mi[]=;
int i;
for (i=;i<MAXN;i++)
mi[i]=mi[i-]*;
scanf("%s",str);
int n=strlen(str);
while (!islower(str[n-]))
n--;
for (i=;i<n;i++)
{
pp.insert(i,str[i]-'a');
}
int m;
scanf("%d\n",&m);
char opt;
char ch;
int x,y;
for (i=;i<m;i++)
{
scanf("%c",&opt);
if (opt=='Q')
{
scanf("%d%d\n",&x,&y);
if (x>y)swap(x,y);
int l=,r=n-y+,mid;
while(l+<r)
{
mid=(l+r)>>;
if (pp.query(x,x+mid-)==pp.query(y,y+mid-))
l=mid;
else
r=mid;
}
printf("%d\n",l);
}else if (opt=='R')
{
scanf("%d %c\n",&x,&ch);
pp.change_val(x,ch-'a');
}else if (opt=='I')
{
scanf("%d %c\n",&x,&ch);
pp.insert(x,ch-'a');
n++;
}
}
}

bzoj 1014: [JSOI2008]火星人prefix hash && splay的更多相关文章

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

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

  2. bzoj 1014 [JSOI2008]火星人prefix(splay+hash)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1014 [题意] 给定一个字符串,要求提供修改一个字符,插入一个字符,查询两个后缀LCP ...

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

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

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

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

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

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

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

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

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

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

  8. 【BZOJ】1014: [JSOI2008]火星人prefix(splay+hash+二分+lcp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1014 题意:支持插入一个字符.修改一个字符,查询lcp.(总长度<=100000, 操作< ...

  9. BZOJ 1014 [JSOI2008]火星人prefix (splay+二分答案+字符串hash)

    题目大意:维护一个字符串,支持插入字符和替换字符的操作,以及查询该字符串两个后缀的最长公共前缀长度 乍一看以为是后缀数组,然而并没有可持久化后缀数组(雾) 看题解才知道这是一道splay题,首先要对s ...

随机推荐

  1. (求助大牛)关于vs2010上的AVS代码bug问题~~

    问题1:就是解码端,出现错误,找到bug所在地了,见下图: memcpy出错了,跳到下图了.可是错误显示的我不懂,求解释一下就ok了,小女子在此谢过了~~哎,调bug的能力弱爆了!! 大家看看吧~~是 ...

  2. 懂,你的App生,不懂,死!

    近期有一些开发人员.创业公司的人加我微信viyi88,咨询一些关于自己App的事情.被问得最多的可能就是:"我的App怎样推广添加下载量?"而且信誓旦旦地说自己的App做得非常好, ...

  3. PrintWriter的print和write方法(转)

    public void print(String s) {if (s == null) {s = "null";}write(s);  }  print只是先对s==null转换为 ...

  4. bitset使用

    17.10使用序列1.2.3.5.8.13.21初始化一个bitset,将这些位置置位.对另一个bitset进行默认初始化,并编写一小段程序将其恰当的位置位. #include<iostream ...

  5. 汇编语言-[BX]和loop指令

    汇编语言-[BX]和loop指令 [BX]指令介绍 mov ax,[bx] 功能: bx为偏移地址,段地址默认为ds.因此,上面指令作用就是将ax中的数据送入内存ds:bx处,即:((ds)*16 + ...

  6. python 验证码

    python写的验证码小程序 ##################验证码,数字+字母 import random check_code="" for i in range(6): ...

  7. C# 分页

    #region 分页 /// <summary> /// 分页 /// </summary> /// <param name="page">当前 ...

  8. Java设计模式--单列设计模式

    设计模式:解决某一类问题行知最有效的方法.java有23种设计模式 单列设计模式: 解决一个类在内存中只存在一个对象 思路:(要保证对象的唯一性) 1.为了避免其它程序建立该对象,先禁止替他类创建改对 ...

  9. jquery 对select option 增删改查

    一.查 jQuery获取select的Text和Value: 代码如下: 1.当select添加选择事件,当选择其中一项时触发:          $("#select_id"). ...

  10. jQuery 遍历同胞(siblings)

    同胞拥有相同的父元素. 通过 jQuery,您能够在 DOM 树中遍历元素的同胞元素. 在 DOM 树中水平遍历 有许多有用的方法让我们在 DOM 树进行水平遍历: siblings() next() ...