题目链接

后缀数组显然不行啊。求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. Hadoop生态圈-Azkaban部署实战

    Hadoop生态圈-Azkaban部署实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.  一.Azkaban部署流程 1>.上传azkaban程序并创建解压目录 [yinz ...

  2. Java大话设计模式

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  3. spring中set注入的一些小细节错误

    这是小白偶尔一直null指针的错误,调试了好久,原来是自己对spring注入的不够了解 我相信有很多跟我差不多的初学者会遇上,所以特地写出来,防止有人跟我一样.哈哈,也写上去,以防自己下次还犯这样的错 ...

  4. protobuf与json相互转换的方法

    google的protobuf对象转json,不能直接使用FastJson之类的工具进行转换,原因是protobuf生成对象的get方法,返回的类型有byte[],而只有String类型可以作为jso ...

  5. 原始套接字-TCP/IP下三层数据显示

    #include <stdio.h> #include <errno.h> #include <unistd.h> #include <sys/socket. ...

  6. jenkins设置CSRF 协议(CRUMB值设置)

    在关闭“”调用出现Error 403 No valid crumb was included in the request 第一种解决方式是 关闭 csrf,如上图,去掉勾就可以,但是并不推荐. 第二 ...

  7. JS模块化写法(转)

    一.原始写法 模块就是实现特定功能的一组方法. 只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块. function m1(){ //... } function m2(){ // ...

  8. [转载]AngularJS 指令 用法

    http://book.2cto.com/201312/37782.html http://www.itnose.net/detail/6144038.html http://www.cnblogs. ...

  9. python字典转datafarm,pandas

    # coding:utf-8 import json import pandas as pd with open("./article_file/all_article.json" ...

  10. Shell-遍历删除指定目录

    Code: find $LibPath/ -name .svn | xargs rm -rf