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
代码:

  1. #include<cstdio>
  2.  
  3. #include<cstdlib>
  4.  
  5. #include<cmath>
  6.  
  7. #include<cstring>
  8.  
  9. #include<algorithm>
  10.  
  11. #include<iostream>
  12.  
  13. #include<vector>
  14.  
  15. #include<map>
  16.  
  17. #include<set>
  18.  
  19. #include<queue>
  20.  
  21. #include<string>
  22.  
  23. #define inf 1000000000
  24.  
  25. #define maxn 1000000+5
  26.  
  27. #define maxm 500+100
  28.  
  29. #define eps 1e-10
  30.  
  31. #define ull unsigned long long
  32.  
  33. #define pa pair<int,int>
  34.  
  35. #define for0(i,n) for(int i=0;i<=(n);i++)
  36.  
  37. #define for1(i,n) for(int i=1;i<=(n);i++)
  38.  
  39. #define for2(i,x,y) for(int i=(x);i<=(y);i++)
  40.  
  41. #define for3(i,x,y) for(int i=(x);i>=(y);i--)
  42.  
  43. #define mod 1000000007
  44. #define base 13131
  45.  
  46. using namespace std;
  47.  
  48. inline int read()
  49.  
  50. {
  51.  
  52. int x=,f=;char ch=getchar();
  53.  
  54. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  55.  
  56. while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
  57.  
  58. return x*f;
  59.  
  60. }
  61. int n,m,q;
  62. ull a[maxn],b[maxn];
  63. char ch[maxn];
  64. string s;
  65. inline void rebuild()
  66. {
  67. n=s.length();
  68. b[n-]=s[n-];
  69. for3(i,n-,)b[i]=b[i+]*base+(ull)s[i];
  70. }
  71. inline ull get(int x,int l){return b[x]-b[x+l]*a[l];}
  72.  
  73. int main()
  74.  
  75. {
  76.  
  77. freopen("input.txt","r",stdin);
  78.  
  79. freopen("output.txt","w",stdout);
  80.  
  81. n=read();q=read();
  82. scanf("%s",ch);s=ch;
  83. a[]=;
  84. for1(i,maxn-)a[i]=a[i-]*(ull)base;
  85. rebuild();
  86. while(q--)
  87. {
  88. scanf("%s",ch);
  89. if(ch[]=='L')
  90. {
  91. int x=read()-,y=read()-,l=,r=n-y;
  92. while(l<=r)
  93. {
  94. int mid=(l+r)>>;
  95. if(get(x,mid)==get(y,mid))l=mid+;else r=mid-;
  96. }
  97. printf("%d\n",r);
  98. }
  99. else if(ch[]=='A')
  100. {
  101. int x=read()-;
  102. scanf("%s",ch);
  103. s.insert(x,ch);
  104. rebuild();
  105. }
  106. else if(ch[]=='C')
  107. {
  108. int x=read()-,y=read()-;
  109. scanf("%s",ch);
  110. for2(i,x,y)s[i]=ch[i-x];
  111. rebuild();
  112. }
  113. else
  114. {
  115. int x=read()-,y=read()-;
  116. s.erase(x,y-x+);
  117. rebuild();
  118. }
  119. }
  120.  
  121. return ;
  122.  
  123. }

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

代码:

  1. #include<cstdio>
  2.  
  3. #include<cstdlib>
  4.  
  5. #include<cmath>
  6.  
  7. #include<cstring>
  8.  
  9. #include<algorithm>
  10.  
  11. #include<iostream>
  12.  
  13. #include<vector>
  14.  
  15. #include<map>
  16.  
  17. #include<set>
  18.  
  19. #include<queue>
  20.  
  21. #include<string>
  22.  
  23. #define inf 1000000000
  24.  
  25. #define maxn 1000000+5
  26.  
  27. #define maxm 500+100
  28.  
  29. #define eps 1e-10
  30.  
  31. #define ull unsigned long long
  32.  
  33. #define pa pair<int,int>
  34.  
  35. #define for0(i,n) for(int i=0;i<=(n);i++)
  36.  
  37. #define for1(i,n) for(int i=1;i<=(n);i++)
  38.  
  39. #define for2(i,x,y) for(int i=(x);i<=(y);i++)
  40.  
  41. #define for3(i,x,y) for(int i=(x);i>=(y);i--)
  42.  
  43. #define mod 1000000007
  44.  
  45. using namespace std;
  46.  
  47. inline int read()
  48.  
  49. {
  50.  
  51. int x=,f=;char ch=getchar();
  52.  
  53. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  54.  
  55. while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
  56.  
  57. return x*f;
  58.  
  59. }
  60. int n,m,q,xx,yy,rt,tot,id[maxn],s[maxn],c[maxn][],fa[maxn];
  61. ull v[maxn],sum[maxn],hash[maxn];
  62. char st[maxn];
  63. inline void pushup(int x)
  64. {
  65. if(!x)return;
  66. int l=c[x][],r=c[x][];
  67. s[x]=s[l]+s[r]+;
  68. sum[x]=sum[r]+v[x]*hash[s[r]]+sum[l]*hash[s[r]+];
  69. //if(x==7)cout<<x<<' '<<l<<' '<<r<<' '<<sum[x]<<' '<<sum[r]<<' '<<s[r]<<' '<<v[x]<<endl;
  70. }
  71. inline void rotate(int x,int &k)
  72. {
  73. int y=fa[x],z=fa[y],l=c[y][]==x,r=l^;
  74. if(y!=k)c[z][c[z][]==y]=x;else k=x;
  75. //cout<<x<<' '<<y<<' '<<z<<endl;
  76. fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
  77. c[y][l]=c[x][r];c[x][r]=y;
  78. pushup(y);pushup(x);
  79. }
  80. inline void splay(int x,int &k)
  81. {
  82. while(x!=k)
  83. {
  84. int y=fa[x],z=fa[y];
  85. if(y!=k)
  86. {
  87. if((c[z][]==y)^(c[y][]==x))rotate(x,k);else rotate(y,k);
  88. }
  89. rotate(x,k);
  90. }
  91. }
  92. void build(int l,int r,int f)
  93. {
  94. if(l>r)return;
  95. int mid=(l+r)>>,x=id[mid]=++tot,y=fa[x]=id[f];
  96. v[x]=st[mid];c[y][mid>f]=x;
  97. if(l==r)
  98. {
  99. sum[x]=v[x];s[x]=;
  100. return;
  101. }
  102. build(l,mid-,mid);build(mid+,r,mid);
  103. pushup(x);
  104. //cout<<x<<' '<<c[x][0]<<' '<<c[x][1]<<' '<<sum[x]<<endl;
  105. }
  106. inline int find(int x,int k)
  107. {
  108. int l=c[x][],r=c[x][];
  109. if(s[l]+==k)return x;
  110. else if(s[l]>=k)return find(l,k);
  111. else return find(r,k-s[l]-);
  112. }
  113. inline void split(int l,int r)
  114. {
  115. xx=find(rt,l);yy=find(rt,r);
  116. splay(xx,rt);splay(yy,c[xx][]);
  117. }
  118. inline void print(int x)
  119. {
  120. if(!x)return;
  121. //cout<<x<<' '<<c[x][0]<<' '<<c[x][1]<<"AAAAAAAAAAA"<<endl;
  122. print(c[x][]);
  123. cout<<(char)v[x];
  124. print(c[x][]);
  125. }
  126. inline ull query(int l,int r)
  127. {
  128. split(l,r+);
  129. //cout<<l<<' '<<r<<endl;
  130. //cout<<l<<' '<<r<<' '<<c[y][0]<<' '<<sum[c[y][0]]<<' '<<v[c[y][0]]<<endl;
  131. //print(c[yy][0]);cout<<endl;
  132. //cout<<sum[c[yy][0]]<<endl;
  133. return sum[c[yy][]];
  134. }
  135.  
  136. int main()
  137.  
  138. {
  139.  
  140. n=read();q=read();
  141. hash[]=;
  142. for1(i,maxn-)hash[i]=hash[i-]*(ull);
  143. scanf("%s",st+);m=strlen(st+);
  144. st[]=st[m++]='a';
  145. build(,m+,);rt=id[(+m+)>>];
  146. //cout<<id[2]<<' '<<id[3]<<endl;
  147. while(q--)
  148. {
  149. char ch[];scanf("%s",ch);
  150. //cout<<"AAAAAAAAAA"<<endl;
  151. if(ch[]=='L')
  152. {
  153. int a=read(),b=read(),l=,r=s[rt]--b+;
  154. while(l<=r)
  155. {
  156. int mid=(l+r)>>;
  157. //cout<<l<<' '<<mid<<' '<<r<<endl;
  158. if(query(a,a+mid-)==query(b,b+mid-))l=mid+;else r=mid-;
  159. }
  160. printf("%d\n",r);
  161. }
  162. else if(ch[]=='A')
  163. {
  164. int a=read();//cout<<a<<endl;
  165. scanf("%s",st+);m=strlen(st+);
  166. build(,m,);
  167. split(a,a+);
  168. fa[c[yy][]=id[(+m)>>]]=yy;
  169. pushup(yy);pushup(xx);
  170. }
  171. else if(ch[]=='C')
  172. {
  173. int a=read(),b=read();
  174. scanf("%s",st+);m=strlen(st+);
  175. build(,m,);
  176. split(a,b+);
  177. fa[c[yy][]=id[(+m)>>]]=yy;
  178. pushup(yy);pushup(xx);
  179. }
  180. else
  181. {
  182. int a=read(),b=read();
  183. split(a,b+);
  184. c[yy][]=;
  185. pushup(yy);pushup(xx);
  186. }
  187. }
  188.  
  189. return ;
  190.  
  191. }

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. ASC码 .

    有些时候需要用到一些字符的ASC码,到网上查找太麻烦,现在记录下来. 第128-255号为扩展字符(不常用) Dec Hx Oct Char   Dec Hx Oct Char Dec Hx Oct ...

  2. springmvc中url-pattern的大坑

    <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springfr ...

  3. IDE开发<LER-Studio>(2)::登录模块

    软件中写登录模块是为了防止软件的恶意传播,内测阶段可以忽略登录. 以下为登录模块主要源代码: void CLoginDlg::OnBnClickedBtnLogin() { // TODO: Add ...

  4. 学习基于OpenGL的CAD程序的开发计划(一)

    本人目前从事的工作面对的客户中很多来自高端制造业,他们对CAD/CAE/CAM软件的应用比较多.公司现有的软件产品主要是用于渲染展示及交互,但面对诸如CAD方面的应用(比如基于约束的装配.制造工艺的流 ...

  5. OpenJudge/Poj 1657 Distance on Chessboard

    1.链接地址: http://bailian.openjudge.cn/practice/1657 http://poj.org/problem?id=1657 2.题目: 总时间限制: 1000ms ...

  6. MongoDB笔记(二)访问权限

    要访问数据库,那么对访问权限的设置是必须的! 1.启用权限控制(-auth),当启用MongoDB数据库服务时,对参数的设置可以决定是否启用权限控制   不开启: mongod -dbpath=D:/ ...

  7. 【Android】Sqlite3命令详解

    Sqlite3常用命令 Sqlite3命令有"."符合作为前缀. 基本操作 1.创建或者打开数据库 sqlite3 xxx.db 如果xxx.db存在则打开如果没有则新建此时执行创 ...

  8. demo_07选择器练习

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. Windows加密视频播放器使用教程

    1.   下载文件 http://pan.baidu.com/s/1c2aESQs 2.    操作流程 温馨提示 播放时,请务必保证播放设备联网(原因:用户名权限验证需要网络,播放后10秒即可关闭网 ...

  10. 测试php页面执行代码时间

    //生命一个计算脚本运行时间的类 class Timer { private $startTime = 0; //保存脚本开始执行时的时间(以微秒的形式保存) private $stopTime = ...