2105: 增强型LCP

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 366  Solved: 86
[Submit][Status]

Description

Input

Output

对于每个Lcp(a,b)操作输出最长公共前缀

Sample Input

47
abab
L 13
A 1 ab
L 1 3
C 56 cb
L 1 3
D 1 2
L 1 3

Sample Output

2
4
2
0

HINT

Source

题解:
这题。。。
原来一直以为是个splay练手题,于是昨天来写。。。
2h没调出来,今天来了发现pushup写错了。。。T_T
然后就是狂T了。。。
看题解发现我们暴力重构写hash就行了,因为修改少,我也是醉了。。。
然后终于会了字符串的hash算法
我们另b[i]=si-sn的hash值,递推式b[i]=b[i+1]*base+s[i]
然后我们要得到从i开始的len长度的hash就是 b[i]-b[i+len]*a[len]  a[len]表示base^len
还有c++的字符串问题
s,insert(pos,st) 表示在pos前插入st
s.erase(pos,len)表示从pos开始删除len的字符,包括pos
代码:

 #include<cstdio>

 #include<cstdlib>

 #include<cmath>

 #include<cstring>

 #include<algorithm>

 #include<iostream>

 #include<vector>

 #include<map>

 #include<set>

 #include<queue>

 #include<string>

 #define inf 1000000000

 #define maxn 1000000+5

 #define maxm 500+100

 #define eps 1e-10

 #define ull unsigned long long

 #define pa pair<int,int>

 #define for0(i,n) for(int i=0;i<=(n);i++)

 #define for1(i,n) for(int i=1;i<=(n);i++)

 #define for2(i,x,y) for(int i=(x);i<=(y);i++)

 #define for3(i,x,y) for(int i=(x);i>=(y);i--)

 #define mod 1000000007
#define base 13131 using namespace std; 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 n,m,q;
ull a[maxn],b[maxn];
char ch[maxn];
string s;
inline void rebuild()
{
n=s.length();
b[n-]=s[n-];
for3(i,n-,)b[i]=b[i+]*base+(ull)s[i];
}
inline ull get(int x,int l){return b[x]-b[x+l]*a[l];} int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); n=read();q=read();
scanf("%s",ch);s=ch;
a[]=;
for1(i,maxn-)a[i]=a[i-]*(ull)base;
rebuild();
while(q--)
{
scanf("%s",ch);
if(ch[]=='L')
{
int x=read()-,y=read()-,l=,r=n-y;
while(l<=r)
{
int mid=(l+r)>>;
if(get(x,mid)==get(y,mid))l=mid+;else r=mid-;
}
printf("%d\n",r);
}
else if(ch[]=='A')
{
int x=read()-;
scanf("%s",ch);
s.insert(x,ch);
rebuild();
}
else if(ch[]=='C')
{
int x=read()-,y=read()-;
scanf("%s",ch);
for2(i,x,y)s[i]=ch[i-x];
rebuild();
}
else
{
int x=read()-,y=read()-;
s.erase(x,y-x+);
rebuild();
}
} return ; }

再贴一下splay的代码,sad story。。。

代码:

 #include<cstdio>

 #include<cstdlib>

 #include<cmath>

 #include<cstring>

 #include<algorithm>

 #include<iostream>

 #include<vector>

 #include<map>

 #include<set>

 #include<queue>

 #include<string>

 #define inf 1000000000

 #define maxn 1000000+5

 #define maxm 500+100

 #define eps 1e-10

 #define ull unsigned long long

 #define pa pair<int,int>

 #define for0(i,n) for(int i=0;i<=(n);i++)

 #define for1(i,n) for(int i=1;i<=(n);i++)

 #define for2(i,x,y) for(int i=(x);i<=(y);i++)

 #define for3(i,x,y) for(int i=(x);i>=(y);i--)

 #define mod 1000000007

 using namespace std;

 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 n,m,q,xx,yy,rt,tot,id[maxn],s[maxn],c[maxn][],fa[maxn];
ull v[maxn],sum[maxn],hash[maxn];
char st[maxn];
inline void pushup(int x)
{
if(!x)return;
int l=c[x][],r=c[x][];
s[x]=s[l]+s[r]+;
sum[x]=sum[r]+v[x]*hash[s[r]]+sum[l]*hash[s[r]+];
//if(x==7)cout<<x<<' '<<l<<' '<<r<<' '<<sum[x]<<' '<<sum[r]<<' '<<s[r]<<' '<<v[x]<<endl;
}
inline void rotate(int x,int &k)
{
int y=fa[x],z=fa[y],l=c[y][]==x,r=l^;
if(y!=k)c[z][c[z][]==y]=x;else k=x;
//cout<<x<<' '<<y<<' '<<z<<endl;
fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;
pushup(y);pushup(x);
}
inline void splay(int x,int &k)
{
while(x!=k)
{
int y=fa[x],z=fa[y];
if(y!=k)
{
if((c[z][]==y)^(c[y][]==x))rotate(x,k);else rotate(y,k);
}
rotate(x,k);
}
}
void build(int l,int r,int f)
{
if(l>r)return;
int mid=(l+r)>>,x=id[mid]=++tot,y=fa[x]=id[f];
v[x]=st[mid];c[y][mid>f]=x;
if(l==r)
{
sum[x]=v[x];s[x]=;
return;
}
build(l,mid-,mid);build(mid+,r,mid);
pushup(x);
//cout<<x<<' '<<c[x][0]<<' '<<c[x][1]<<' '<<sum[x]<<endl;
}
inline int find(int x,int k)
{
int l=c[x][],r=c[x][];
if(s[l]+==k)return x;
else if(s[l]>=k)return find(l,k);
else return find(r,k-s[l]-);
}
inline void split(int l,int r)
{
xx=find(rt,l);yy=find(rt,r);
splay(xx,rt);splay(yy,c[xx][]);
}
inline void print(int x)
{
if(!x)return;
//cout<<x<<' '<<c[x][0]<<' '<<c[x][1]<<"AAAAAAAAAAA"<<endl;
print(c[x][]);
cout<<(char)v[x];
print(c[x][]);
}
inline ull query(int l,int r)
{
split(l,r+);
//cout<<l<<' '<<r<<endl;
//cout<<l<<' '<<r<<' '<<c[y][0]<<' '<<sum[c[y][0]]<<' '<<v[c[y][0]]<<endl;
//print(c[yy][0]);cout<<endl;
//cout<<sum[c[yy][0]]<<endl;
return sum[c[yy][]];
} int main() { n=read();q=read();
hash[]=;
for1(i,maxn-)hash[i]=hash[i-]*(ull);
scanf("%s",st+);m=strlen(st+);
st[]=st[m++]='a';
build(,m+,);rt=id[(+m+)>>];
//cout<<id[2]<<' '<<id[3]<<endl;
while(q--)
{
char ch[];scanf("%s",ch);
//cout<<"AAAAAAAAAA"<<endl;
if(ch[]=='L')
{
int a=read(),b=read(),l=,r=s[rt]--b+;
while(l<=r)
{
int mid=(l+r)>>;
//cout<<l<<' '<<mid<<' '<<r<<endl;
if(query(a,a+mid-)==query(b,b+mid-))l=mid+;else r=mid-;
}
printf("%d\n",r);
}
else if(ch[]=='A')
{
int a=read();//cout<<a<<endl;
scanf("%s",st+);m=strlen(st+);
build(,m,);
split(a,a+);
fa[c[yy][]=id[(+m)>>]]=yy;
pushup(yy);pushup(xx);
}
else if(ch[]=='C')
{
int a=read(),b=read();
scanf("%s",st+);m=strlen(st+);
build(,m,);
split(a,b+);
fa[c[yy][]=id[(+m)>>]]=yy;
pushup(yy);pushup(xx);
}
else
{
int a=read(),b=read();
split(a,b+);
c[yy][]=;
pushup(yy);pushup(xx);
}
} return ; }

BZOJ2105: 增强型LCP的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. 第18章 图元文件_18.2 增强型图元文件(emf)(2)

    18.2.7 增强型图元文件的查看和打印程序 (1)传递EMF到剪贴板,剪贴板类型应为:CF_ENHMETAFILE (2)CopyEnhMetaFile用于复制图元文件 (3)剪贴板中的图元文件会自 ...

  3. 第18章 图元文件_18.2 增强型图元文件(emf)(1)

    18.2 增强型图元文件(emf) 18.2.1 创建并显示增强型图元文件的步骤 (1)创建:hdcEMF = CreateEnhMetaFile(hdcRef,szFilename,lpRect,l ...

  4. Java 增强型的for循环 for each

    Java 增强型的for循环 for each For-Each循环 For-Each循环也叫增强型的for循环,或者叫foreach循环. For-Each循环是JDK5.0的新特性(其他新特性比如 ...

  5. 黑马程序员——【Java高新技术】——JDK1.5新特性:静态导入、可变参数、增强型for循环、自动装箱拆箱、枚举

    ---------- android培训.java培训.期待与您交流! ---------- 一.静态导入 1.import和import static区别: (1)import 是导入一个类或某个包 ...

  6. 增强型for循环,用于遍历数组元素

    /** * */ package com.cn.u4; /** * @author Administrator *增强型for */ public class ZhengQiangFor { publ ...

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

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

  8. php版redis插件,SSDB数据库,增强型的Redis管理api实例

    php版redis插件,SSDB数据库,增强型的Redis管理api实例 SSDB是一套基于LevelDB存储引擎的非关系型数据库(NOSQL),可用于取代Redis,更适合海量数据的存储.另外,ro ...

  9. poj 2774 Long Long Message 后缀数组LCP理解

    题目链接 题意:给两个长度不超过1e5的字符串,问两个字符串的连续公共子串最大长度为多少? 思路:两个字符串连接之后直接后缀数组+LCP,在height中找出max同时满足一左一右即可: #inclu ...

随机推荐

  1. 不允许对索引显式地使用 DROP INDEX,该索引正用于 UNIQUE KEY

    [转载]http://blog.csdn.net/w87875251l/article/details/7929657 不允许对索引显式地使用 DROP INDEX,该索引正用于 UNIQUE KEY ...

  2. C#&JQuery非缓存式无刷新临时存储数据之仿购物车功能

    感谢广大博问博友的帮助和共同研究讨论,终于实现了一个无缓存无刷新仿购物车的小功能: 一.实现效果简述: 有一种列表,是由双层Repeater嵌套,第一层用来显示类别,第二层用来显示类别下的商品数据, ...

  3. 时空分割的画面--用xcode命令行回忆turbo c

    大学时期曾经玩过turbo c的同学,可以用xcode命令行写写c程序,回味一下吧:) 1. 首先在终端输入,touch main.c 新建文件 2. 编辑main.c内容,写一段简单代码 #incl ...

  4. mac最常用终端命令

    1分钟,快速复习下: pwd (显示当前所在路径) ls -l  (列出文件的详细信息,如创建者,创建时间,文件的读写权限列表等等) touch `filename`(创建文件) open `file ...

  5. Content by query webpart 自定义样式的使用方法

    今天研究一个非常实用的webpart 如果在office365 上 的webpart中一直没“内容查询”, 这里需要开启2个features:http://community.office365.co ...

  6. Net中exe之间的消息传递

    1.创建一个消息通讯类 using System;using System.Collections.Generic;using System.Linq;using System.Text;using ...

  7. HTTP 417解决方案

      在一次模拟HTPP请求时,本人在项目中的一般处理程序中调用客户接口返回非成功的结果.为了方便调试,所以将核心代码拷贝至控制台中进逐个调试. 在控制台中,启动调试时提示:   未经处理的异常:  S ...

  8. C++函数指针和指针函数

    本文参考http://www.prglab.com/cms/pages/c-tutorial/advanced-data/pointers.php http://blog.csdn.net/ameyu ...

  9. 第一个wxWidgets程序

    wxWidgets的安装方法网上有一大堆,可以参照http://wiki.codeblocks.org/index.php?title=WxWindowsQuickRef 这里解压并编译 ,也可以参照 ...

  10. [Computer Vision] SIFT特征学习笔记

    SIFT(Scale Invariant Feature Transform),尺度空间不变特征,目前手工设计的最好vision特征. 以下是学习http://blog.csdn.net/zddblo ...