Jewel Magic UVA - 11996 || bzoj1014: [JSOI2008]火星人prefix
这是一道用splay/非旋treap做的题(这里用的是非旋treap)
1/2/3是splay/非旋treap的常规操作。对于操作4,可以用哈希法求LCP。记hash(i,L)为子串[i,i+L-1](即第i个开始的L个)的hash值。记s[i]为序列第i位(编号从1开始),n为序列长度
如果通过某种方式做到能在O(logn)时间内取出一段子串的hash值,那么二分答案(即LCP长度x),可以在O(logn)时间内判一个x是否合法(如果hash(l,x)==hash(r,x)则认为[l,l+x-1]和[r,r+x-1]是相同的,合法,否则不合法),可以做到在O(log^2n)内完成一个操作4。当然,hash判字符串是否相同正确性可能受影响,因此可以多计算一些hash,当他们都相同时才认为字符串相同,可以将错误率降到足够小。
如何维护一段子串的hash值?首先定义x为任意整数,定义$hash(i,L)=s[i+L-1]*x^{L-1}+s[i+L-2]*x^{L-2}+...+s[i+1]*x+s[i]$
(这里及之后都省略了取模)
(简单记法:左边乘的次数小)
(另一种记法:另一种求法的伪代码表示:ans=0;for(j=i+L-1;j>=i;j--) ans=ans*x+s[j];)
可以发现:如果已知hash(i,p)和hash(i+p,q)(即已知[i,i+p-1]和[i+p,i+p+q-1]的hash值),要求hash(i,p+q)(就是这两段合起来的hash值),那么:
令j=i+p,那么$hash(i,p+q)$
$=s[j+q-1]*x^{p+q-1}+s[j+q-2]*x^{p+q-2}+...+s[j]*x^p+s[i+p-1]*x^{p-1}+...+s[i]*x^0$
所以$hash(i,p+q)=hash(j,q)*x^p+hash(i,p)=hash(i,p)+hash(i+p,q)*x^p$
这样就得到了对于平衡树某个节点,根据子节点为根的子树的hash值与自身值求以自身为根的子树的hash值的方法(先将左子树和自身合起来,再将结果与右子树合起来)
当然,由于此题有一个翻转操作,对于一个节点要维护两个hash:正向序列hash和反向序列hash。翻转操作时顺便交换一下两个的值。
附:这道题没有卡hash,单hash就能过,
附:听说操作4有O(logn)的方法?待解决
错误记录:
1.141行误用build函数(build是用的左闭右闭区间),输入了(a+1,a+n+1)。(然而不知道为什么虽然过不了udebug的数据然而把题目A掉了)
2.没注意在字符前还是字符后插入
*3.posib函数写错:没有考虑要计算hash值的串超出长度范围的情况(就是第二个"&&"之前的部分)。错了不止一次
4.可能出现的错误:如果hash不用ull自然溢出,自己取模,那么要考虑爆int、爆longlong、负数等等
#include<cstdio>
#include<algorithm>
using namespace std;
inline int rand1()
{
static int x=;
return x=(48271LL*x+)%;
}
unsigned long long powx[];
struct Node
{
Node(){}
Node* ch[];
int r;
bool flip;
int v;
unsigned long long h,rh;
int size;
void upd()
{
if(ch[]) ch[]->pd();
if(ch[]) ch[]->pd();
size=+(ch[]?ch[]->size:)+(ch[]?ch[]->size:);
h=(ch[]?ch[]->h:)+v*powx[ch[]?ch[]->size:]+(ch[]?ch[]->h:)*powx[(ch[]?ch[]->size:)+];
rh=(ch[]?ch[]->rh:)+v*powx[ch[]?ch[]->size:]+(ch[]?ch[]->rh:)*powx[(ch[]?ch[]->size:)+];
}
void pd()
{
if(flip)
{
swap(ch[],ch[]);
swap(h,rh);
if(ch[]) (ch[]->flip)^=;
if(ch[]) (ch[]->flip)^=;
flip=;
}
}
}nodes[];
Node* root;int mem;
Node* getnode(){return nodes+(mem++);}
Node* merge(Node* a,Node* b)
{
if(a==NULL) return b;
if(b==NULL) return a;
if(a->r < b->r)
{
a->pd();a->ch[]=merge(a->ch[],b);a->upd();
return a;
}
else
{
b->pd();b->ch[]=merge(a,b->ch[]);b->upd();
return b;
}
}
typedef pair<Node*,Node*> P;
P split(Node* a,int n)
{
if(a==NULL) return P(NULL,NULL);
P y;
a->pd();int s=a->ch[] ? a->ch[]->size : ;
if(s>=n)
{
y=split(a->ch[],n);
a->ch[]=y.second;a->upd();
y.second=a;
}
else
{
y=split(a->ch[],n-s-);
a->ch[]=y.first;a->upd();
y.first=a;
}
return y;
}
inline void insert(int k,int x)
{
Node* t=getnode();
t->ch[]=t->ch[]=NULL;t->r=rand1();t->v=x;t->flip=;t->upd();
P y=split(root,k-);
root=merge(merge(y.first,t),y.second);
}
inline void erase(int k)
{
P y=split(root,k-);
P y2=split(y.second,);
root=merge(y.first,y2.second);
}
inline void reverse(int l,int r)
{
if(l>r) swap(l,r);
P y=split(root,l-);
P y2=split(y.second,r-l+);
y2.first->flip^=;
root=merge(merge(y.first,y2.first),y2.second);
}
inline int size()
{
return root ? root->size : ;
}
inline unsigned long long geth(int l,int r)
{
if(l>r) return ;
P y=split(root,l-);
P y2=split(y.second,r-l+);
unsigned long long ans=y2.first ? y2.first->h : ;
root=merge(merge(y.first,y2.first),y2.second);
return ans;
}
Node* build(int *l,int *r)
{
if(l>r) return NULL;
if(l==r)
{
Node* t=getnode();
t->ch[]=t->ch[]=NULL;t->r=rand1();t->v=*l;t->flip=;t->upd();
return t;
}
else
{
int* mid=l+(r-l)/;
return merge(build(l,mid),build(mid+,r));
}
}
int n,m,q;
int a[];
const int X=;
int l,r;
inline bool posib(int x)
{
return (l+x-<=size())&&(r+x-<=size())&&(geth(l,l+x-)==geth(r,r+x-));
}
int main()
{
register int i;
int lx,rx,k,x,mid,tmp;
powx[]=;
for(i=;i<=;i++) powx[i]=powx[i-]*X;
scanf("%d%d",&n,&q);
for(i=;i<=n;i++) scanf("%1d",&a[i]);
root=build(a+,a+n);
while(q--)
{
scanf("%d",&tmp);
if(tmp==)
{
scanf("%d%d",&k,&x);
insert(k+,x);
}
else if(tmp==)
{
scanf("%d",&k);
erase(k);
}
else if(tmp==)
{
scanf("%d%d",&l,&r);
reverse(l,r);
}
else if(tmp==)
{
scanf("%d%d",&l,&r);
lx=;rx=size()+;
while(rx-lx>)
{
mid=(lx+rx)>>;
if(posib(mid)) lx=mid;
else rx=mid;
}
printf("%d\n",lx);
}
}
return ;
}
https://www.lydsy.com/JudgeOnline/problem.php?id=1014
https://www.luogu.org/problemnew/show/P4036
贴一下常数超大的代码
做法类似
#pragma GCC optimize("Ofast")
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
inline int rand1()
{
static int x=;
return x=(48271LL*x+)%;
}
unsigned long long powx[];
struct Node
{
Node(){}
Node* ch[];
int r;
int v;
unsigned long long h;
int size;
void upd()
{
size=+(ch[]?ch[]->size:)+(ch[]?ch[]->size:);
h=(ch[]?ch[]->h:)+v*powx[ch[]?ch[]->size:]+(ch[]?ch[]->h:)*powx[(ch[]?ch[]->size:)+];
}
}nodes[];
Node* root;int mem;
Node* getnode(){return nodes+(mem++);}
Node* merge(Node* a,Node* b)
{
if(a==NULL) return b;
if(b==NULL) return a;
if(a->r < b->r)
{
a->ch[]=merge(a->ch[],b);a->upd();
return a;
}
else
{
b->ch[]=merge(a,b->ch[]);b->upd();
return b;
}
}
typedef pair<Node*,Node*> P;
P split(Node* a,int n)
{
if(a==NULL) return P(NULL,NULL);
P y;
int s=a->ch[] ? a->ch[]->size : ;
if(s>=n)
{
y=split(a->ch[],n);
a->ch[]=y.second;a->upd();
y.second=a;
}
else
{
y=split(a->ch[],n-s-);
a->ch[]=y.first;a->upd();
y.first=a;
}
return y;
}
inline void insert(int k,int x)
{
Node* t=getnode();
t->r=rand1();t->v=x;t->upd();
P y=split(root,k-);
root=merge(merge(y.first,t),y.second);
}
inline void erase(int k)
{
P y=split(root,k-);
P y2=split(y.second,);
root=merge(y.first,y2.second);
}
inline int size()
{
return root ? root->size : ;
}
inline unsigned long long geth(int l,int r)
{
if(l>r) return ;
P y=split(root,l-);
P y2=split(y.second,r-l+);
unsigned long long ans=y2.first ? y2.first->h : ;
root=merge(merge(y.first,y2.first),y2.second);
return ans;
}
Node* build(char *l,char *r)
{
if(l>r) return NULL;
if(l==r)
{
Node* t=getnode();
t->r=rand1();t->v=(int)(*l);t->upd();
return t;
}
else
{
char* mid=l+(r-l)/;
return merge(build(l,mid),build(mid+,r));
}
}
int n,m,q;
char a[];
const int X=;
int l,r;
char tmp;
inline bool posib(int x)
{
return (l+x-<=size())&&(r+x-<=size())&&(geth(l,l+x-)==geth(r,r+x-));
}
int main()
{
register int i;
int lx,rx,k,mid;
powx[]=;
for(i=;i<=;i++) powx[i]=powx[i-]*X;
scanf("%s",a+);n=strlen(a+);
root=build(a+,a+n);
scanf("%d",&q);
while(q--)
{
tmp=getchar();while(tmp<'A'||tmp>'Z') tmp=getchar();
if(tmp=='I')
{
scanf("%d",&k);tmp=getchar();while(tmp<'a'||tmp>'z') tmp=getchar();
insert(k+,(int)tmp);
}
else if(tmp=='R')
{
scanf("%d",&k);tmp=getchar();while(tmp<'a'||tmp>'z') tmp=getchar();
erase(k);insert(k,(int)tmp);
}
else if(tmp=='Q')
{
scanf("%d%d",&l,&r);
lx=;rx=size()+;
while(rx-lx>)
{
mid=(lx+rx)>>;
if(posib(mid)) lx=mid;
else rx=mid;
}
printf("%d\n",lx);
}
}
return ;
}
Jewel Magic UVA - 11996 || bzoj1014: [JSOI2008]火星人prefix的更多相关文章
- [BZOJ1014][JSOI2008]火星人prefix
[BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ...
- BZOJ1014 JSOI2008 火星人prefix 【非旋转Treap】*
BZOJ1014 JSOI2008 火星人prefix Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符 ...
- 2018.06.28 BZOJ1014 [JSOI2008]火星人prefix(非旋treap+hash)
[JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Submit: 8951 Solved: 2860 Description 火星 ...
- bzoj千题计划106:bzoj1014 [JSOI2008]火星人prefix
http://www.lydsy.com/JudgeOnline/problem.php?id=1014 两个后缀的最长公共前缀:二分+hash 带修改带插入:splay维护 #include< ...
- [BZOJ1014] [JSOI2008] 火星人prefix (splay & 二分答案)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- bzoj1014: [JSOI2008]火星人prefix splay+hash+二分
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- [bzoj1014](JSOI2008)火星人 prefix (Splay维护哈希)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀. 比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 ...
- BZOJ1014[JSOI2008]火星人prefix(splay维护hash)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- BZOJ1014: [JSOI2008]火星人prefix(splay 二分 hash)
题意 题目链接 Sol 一眼splay + 二分hash,不过区间splay怎么写来着呀 试着写了两个小时发现死活不对 看了一下yyb的代码发现自己根本就不会splay.... // luogu-ju ...
随机推荐
- maven 的编译插件的配置
原文: https://stackoverflow.com/questions/29258141/maven-compilation-error-use-source-7-or-higher-to-e ...
- 在DIV中自己主动换行
word-break:break-all和word-wrap:break-word都是能使其容器如DIV的内容自己主动换行. 它们的差别就在于: 1,word-break:break-all 比如di ...
- YII获取当前URL
<?php //当前域名 echoYii::app()->request->hostInfo; //除域名外的URL echoYii::app()->request ...
- PHP记录商品历史纪录
/* 记录浏览历史 */ if (!empty($_COOKIE['history'])) { if(stripos($_COOKIE['history'].',',$goods_id.',')=== ...
- 点滴记录——Ubuntu 14.04中Solr与Tomcat整合安装
转载请说明出处:http://blog.csdn.net/cywosp/article/details/38965981 1. 安装jdk,tomcat sudo apt-get instal ...
- Linux 简单的Shell输出
echo:用于输出指定字符串或用于在Shell中打印Shell变量的值 语法格式:echo [选项] [参数] -n:不输出换行 linlin@ubuntu:~/linlin/text$ ...
- Xsolla和Hi-Rez工作室联手推行SMITE
视频游戏店面管理和计费解决方式的领导者,Xsolla.将重拳出击将与Hi-Rez游戏工作室合作.该工作室是一家美国的独立游戏开发商,主要开发MOBA游戏-SMITE. 支持全球600多种支付方式 Xs ...
- eclipse中导入其它的webproject遇到和解决的问题
注:下面为我从网上搜来的方法.经使用及学习后整理. 学习javaweb有段时间了.对于导入新项目.遇到好多问题.但终于成功了. 错误1:string cannot be resolved to a t ...
- 更改Mysql登录密码
版本号49之前的跨域设置 在Windows命令行下修改mysql数据库密码步骤如下: 1.通过dos命令进入mysql的bin目录: 2.输入“mysql -uroot -p”,回车进入mysql命令 ...
- UPDATE command denied DELETE
可用磁盘空间不足 支持SELECT information_schema. TABLES