题目链接

后缀数组显然不行啊。求LCP还可以哈希+二分,于是考虑用平衡树维护哈希值。

\[某一节点的哈希值 = hs[lson]*base^{sz[rson]+1} + s[rt]*base^{sz[rson]} + hs[rson]
\]

好像跑的很慢。。

//4800kb	5492ms
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define lson son[x][0]
#define rson son[x][1]
#define gc() getchar()
typedef unsigned long long ull;
const int N=110000;
const ull base=31; int root,size,len,sz[N],fa[N],son[N][2],s[N];
char tmp[N];
ull hs[N],pw[N]; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline char Get_opt()
{
char c=gc();
while(c!='Q'&&c!='R'&&c!='I') c=gc();
return c;
}
inline char Get_char()
{
char c=gc();
while(!isalpha(c)) c=gc();
return c;
}
inline void Update(int x)
{
int rs=rson;
sz[x]=sz[lson]+sz[rs]+1,
hs[x]=hs[lson]*pw[sz[rs]+1]+s[x]*pw[sz[rs]]+hs[rs];
}
void Build(int l,int r,int f)
{
if(l>r) return;
int m=l+r>>1;
son[f][m>f]=m, fa[m]=f, s[m]=tmp[m]-'a'+1;
if(l==r) sz[l]=1, hs[l]=s[m];
else Build(l,m-1,m), Build(m+1,r,m), Update(m);
}
void Rotate(int x,int &k)
{
int a=fa[x],b=fa[a],l=son[a][1]==x,r=l^1;
if(a==k) k=x;
else son[b][son[b][1]==a]=x;
fa[a]=x, fa[x]=b, fa[son[x][r]]=a, son[a][l]=son[x][r], son[x][r]=a;
Update(a);
}
void Splay(int x,int &k)
{
while(x!=k)
{
int a=fa[x];
if(a!=k) son[a][1]==x^son[fa[a]][1]==a?Rotate(x,k):Rotate(a,k);
Rotate(x,k);
}
Update(x);
}
int Kth(int k,int x)
{
while(1)
{
if(sz[lson]+1==k) return x;
if(sz[lson]<k) k-=sz[lson]+1, x=rson;
else x=lson;
}
}
inline int Split(int l,int r)//Get(l,r)
{
int x=Kth(l,root),y=Kth(r,root);
Splay(x,root), Splay(y,son[x][1]);
return son[y][0];
}
inline bool Check(int x,int y,int len)
{
ull v1=hs[Split(x,x+len+1)], v2=hs[Split(y,y+len+1)];
return v1==v2;
}
void Query(int x,int y)
{
int l=1,r=std::min(size-x-1,size-y-1),mid;
while(l<=r){
if(Check(x,y,mid=l+r>>1)) l=mid+1;
else r=mid-1;
}
printf("%d\n",r);
}
void Modify(int p,char c)
{
int x=Kth(p,root);
Splay(x,root), s[x]=c-'a'+1, Update(x);
}
void Insert(int p,char c)
{
// Split(p,p+1);//不能只用Split()!这的p是下标,不一定是编号!
int p1=Kth(p,root),p2=Kth(p+1,root);
Splay(p1,root), Splay(p2,son[root][1]);
int x=++size;
sz[x]=1, hs[x]=s[x]=c-'a'+1, fa[x]=p2, son[p2][0]=x;
Update(p2), Update(p1);
} int main()
{
pw[0]=1;
for(int i=1; i<N; ++i) pw[i]=pw[i-1]*31;
scanf("%s",tmp+2), len=strlen(tmp+2);
size=len+2, root=len+3>>1, Build(1,size,0);
int m=read(),x,y; char opt;
while(m--)
switch(opt=Get_opt(),opt)
{
case 'Q':x=read(),y=read(),Query(x,y); break;
case 'R':x=read(),Modify(x+1,Get_char()); break;
case 'I':x=read(),Insert(x+1,Get_char()); break;
}
return 0;
}

BZOJ.1014.[JSOI2008]火星人(Splay 二分 Hash)的更多相关文章

  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 + Hash + 二分)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. bzoj千题计划286:bzoj1226: [SDOI2009]学校食堂Dining

    http://www.lydsy.com/JudgeOnline/problem.php?id=1226 关键点:一个人只能忍受 ‘紧跟’ 在他 后面的b个人比他先打到饭 dp[i][j][k] 前i ...

  2. bzoj千题计划234:bzoj3802: Vocabulary

    http://www.lydsy.com/JudgeOnline/problem.php?id=3802 dp[i][0/1/2/3]  表示前i个字母,第1.2个字符串,第2.3个字符串的关系分别为 ...

  3. 模拟jQuery中的ready方法及实现按需加载css,js

    一.ready函数的实现 经常用jQuery类库或其他类库中的ready方法,有时候想想它们到底是怎么实现的,但是看了一下jQuery中的源码,涉及到的模块比较多,(水平有限)代码比较难看懂:自己结合 ...

  4. [转载]Cross-Platform Development in Visual Studio

    http://msdn.microsoft.com/en-us/library/dn771552.aspx http://www.cnblogs.com/mengkzhaoyun/p/4152823. ...

  5. Django Book 学习笔记(上)

    拜读了网上的Django Book,现在来总结一下吧...... 一.Django的配置 非常的蛋疼,由于Django的块组之间耦合度低,这既是它的优点,也是它的缺点.我在Ubuntu所配置的Djan ...

  6. Update Bits

    Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits be ...

  7. UART中的硬件流控RTS与CTS【转】

    转自:http://blog.csdn.net/zeroboundary/article/details/8966586 5/23/2013 5:13:04 PM at rock-chips insh ...

  8. js 加alert后才能执行方法

    原因是:访问页面时,某些js方法还没初始化(或者还没有加载出来)此时调用肯定不执行.alert起到了延迟的功能,当用户点击确定此时要执行的js恰好初始化完成,能正常执行. 解决方法是 加setTime ...

  9. .NET 的 WCF 和 WebService 有什么区别?(转载)

    [0]问题: WCF与 Web Service的区别是什么? 和ASP.NET Web Service有什么关系? WCF与ASP.NET Web Service的区别是什么? 这是很多.NET开发人 ...

  10. 使用@SpringBootApplication注解

    很多Spring Boot开发者总是使用@Configuration , @EnableAutoConfiguration 和 @ComponentScan 注解他们的main类. 由于这些注解被如此 ...