开始时间:19:40

完成时间:21:00

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2555

题目大意:(1):在当前字符串的后面插入一个字符串
    
        (2):询问字符串s在当前字符串中出现了几次?(作为连续子串)

题解:最近在写后缀自动机,求一个字符串中出现了几次就相当与其right集合大小,直接上parent树,因为后缀自动机构造特性,可能在parent树改变边,于是用lct维护;

代码:

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<cmath>
  6. #define maxn 1200005
  7. #define maxl 3000005
  8. using namespace std;
  9. int q,n,tot,root,last,mark,m,ri[maxn],lazy[maxn];
  10. char st[maxl];
  11. char ss[];
  12. struct data{
  13. int fa[maxn],son[maxn][];
  14. int isroot(int x) {return (son[fa[x]][]!=x && son[fa[x]][]!=x);}
  15. int which(int x){return son[fa[x]][]==x;}
  16. void change(int x,int k){ ri[x]+=k; lazy[x]+=k;}
  17. void pushdown(int x)
  18. {
  19. if (!lazy[x]) return;
  20. if (son[x][]) change(son[x][],lazy[x]);
  21. if (son[x][]) change(son[x][],lazy[x]);
  22. lazy[x]=;
  23. }
  24. void relax(int x){if (!isroot(x)) relax(fa[x]); pushdown(x);}
  25. void turn(int x){
  26. int y=fa[x],wx=which(x),wy=which(y);
  27. if (!isroot(y)) son[fa[y]][wy]=x; fa[x]=fa[y];
  28. son[y][wx]=son[x][-wx]; fa[son[x][-wx]]=y;
  29. son[x][-wx]=y; fa[y]=x;
  30. }
  31. void splay(int x)
  32. {
  33. relax(x);
  34. while (!isroot(x))
  35. {
  36. if (isroot(fa[x])) turn(x);
  37. else if (which(x)==which(fa[x])) turn(fa[x]),turn(x);
  38. else turn(x),turn(x);
  39. }
  40. }
  41. void access(int x)
  42. {
  43. for (int p=; x; x=fa[x]){ splay(x); son[x][]=p; p=x;}
  44. }
  45. void link(int x,int y){
  46. fa[x]=y; access(y); splay(y); change(y,ri[x]);
  47. }
  48. void cut(int x)
  49. {
  50. access(x); splay(x); change(son[x][],-ri[x]); fa[son[x][]]=; son[x][]=;
  51. }
  52. }lct;
  53. struct date{
  54. int fa[maxn],son[maxn][],val[maxn];
  55. void prepare(){root=tot=last=;}
  56. int newnode(int x){val[++tot]=x; return tot;}
  57. void extend(int x)
  58. {
  59. int p=last,np=newnode(val[p]+);ri[np]=;last=np;
  60. for (; p&&!son[p][x]; p=fa[p]) son[p][x]=np;
  61. if (!p) fa[np]=root,lct.link(np,root);
  62. else
  63. {
  64. int q=son[p][x];
  65. if (val[q]==val[p]+){fa[np]=q; lct.link(np,q);}
  66. else
  67. {
  68. int nq=newnode(val[p]+);ri[nq]=;
  69. memcpy(son[nq],son[q],sizeof(son[q]));
  70. fa[nq]=fa[q]; lct.cut(q),lct.link(nq,fa[nq]);
  71. fa[q]=fa[np]=nq; lct.link(q,nq),lct.link(np,nq);
  72. for (; p && son[p][x]==q; p=fa[p]) son[p][x]=nq;
  73. }
  74. }
  75. }
  76. void build()
  77. {for (int i=; i<=m; i++) extend(st[i]-'A');}
  78. void query(){
  79. int x,y;
  80. bool can=;
  81. x=root;
  82. for (int i=;i<=m;i++){
  83. y=st[i]-'A';
  84. if (!son[x][y]){
  85. can=;
  86. break;
  87. }else{
  88. x=son[x][y];
  89. }
  90. }
  91. if (can==||x==root) puts("");
  92. else{
  93. lct.splay(x);
  94. printf("%d\n",ri[x]),mark^=ri[x];
  95. }
  96. }
  97. }SAM;
  98. void unzip(){
  99. int temp=mark;
  100. for (int i=;i<=m;i++){
  101. temp=(temp*+i-)%m+;
  102. char t=st[i]; st[i]=st[temp],st[temp]=t,temp--;
  103. }
  104. }
  105. int main()
  106. {
  107. scanf("%d\n",&q);mark=;
  108. scanf("%s",st+); m=strlen(st+);
  109. SAM.prepare();
  110. SAM.build();
  111. for (int i=; i<=q; i++)
  112. {
  113. scanf("%s",ss);scanf("%s",st+); m=strlen(st+);unzip();
  114. if (ss[]=='A') SAM.build();
  115. else SAM.query();
  116. }
  117. }

bzoj2555的更多相关文章

  1. 【BZOJ2555】SubString(后缀自动机,Link-Cut Tree)

    [BZOJ2555]SubString(后缀自动机,Link-Cut Tree) 题面 BZOJ 题解 这题看起来不难 每次要求的就是\(right/endpos\)集合的大小 所以搞一个\(LCT\ ...

  2. 【bzoj2555】 SubString

    http://www.lydsy.com/JudgeOnline/problem.php?id=2555 (题目链接) 题意 给出一个初始串,维护两个操作.在原串后面加入一个字符串:询问某个字符串在原 ...

  3. 【BZOJ2555】SubString 后缀自动机+LCT

    [BZOJ2555]SubString Description 懒得写背景了,给你一个字符串init,要求你支持两个操作         (1):在当前字符串的后面插入一个字符串         (2 ...

  4. BZOJ2555 SubString【SAM + Link Cut Tree】

    BZOJ2555. SubString 要求在线询问一个串在原串中出现的次数,并且可以在原串末尾添加字符串 如果没有修改的话,考虑建出\(parent\)树之后统计每个\(endpos\)节点的\(r ...

  5. 【BZOJ-2555】SubString 后缀自动机 + LinkCutTree

    2555: SubString Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1936  Solved: 551[Submit][Status][Di ...

  6. bzoj2555: SubString

    SAM+LCT维护parent tree版本 虽然说子树维护那套理论需要ETT 不过parent tree的根是固定的,所以用lct加一些奇怪的乱搞就行了 //随手拖个SAM的板子和LCT的板子,然后 ...

  7. BZOJ2555——SubString

    0.题目很短,就不概括了 给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支持这些操作. ...

  8. bzoj2555(后缀自动机+LCT)

    题目描述 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支持这些操作. 题解 做法很自然,建出后缀自动机,维护每个节点的right ...

  9. 【BZOJ2555】SubString

    算是学会sam了吧…… 原题: 懒得写背景了,给你一个字符串init,要求你支持两个操作        (1):在当前字符串的后面插入一个字符串        (2):询问字符串s在当前字符串中出现了 ...

随机推荐

  1. java OPENCV 连通域, Imgproc.findContours 例子,参数说明

    http://stackoverflow.com/questions/29491669/real-time-paper-sheet-detection-using-opencv-in-android/ ...

  2. Windows应用程序要点

    一个完整的Windows应用程序除了WinMain函数外,还包含用于处理用户动作和窗口消息的窗口函数.  Windows应用程序具有的一些特性: 消息驱动机制 图形设备接口(GDI) 基于资源的程序设 ...

  3. Centos下安装jdk详解

    环境: 系统: [root@Wulaoer ~]# cat /proc/version Linux version 2.6.32-431.el6.x86_64 (mockbuild@c6b8.bsys ...

  4. zf-关于通知公告显示问题

    1 公告结束日期超过当前时间是不能在通知公告上显示出来的 2 无限制时间的公告也是要在通知公告上显示出来的 于是我在后台实现类增加了如下代码 需要注意的是 当初解决第一个问题的时候增加了一个AND 当 ...

  5. List转换成DataSet实现代码

    public DataSet ConvertToDataSet<T>(IList<T> list) { if (list == null || list.Count <= ...

  6. 1209:Catch That Cow(bfs)

    题意: 从一个坐标到另一个坐标的移动方式有三种,即:st-1,st+1,2*st.每移动一步时间是一秒. 给出两个坐标,求得从第一坐标到第二座标的最短时间. #include<iostream& ...

  7. 通过ant调用shell脚本执行adb命令

    在Hudson或者Jenkins中利用ant的exec 来调用shell命令,通过shell脚本来执行adb shell命令,可以正常执行,不会出现在ant中直接调用adb shell出现的假死情况. ...

  8. 最完整的自动化测试流程:Python编写执行测试用例及定时自动发送最新测试报告邮件

    今天笔者就要归纳总结下一整套测试流程,从无到有,实现零突破,包括如何编写测试用例,定时执行测试用例,查找最新生成的测试报告文件,自动发送最新测试报告邮件,一整套完整的测试流程.以后各位只要着重如何编写 ...

  9. 16级第一周寒假作业F题

    Subsequence TimeLimit:1000MS  MemoryLimit:65536K 64-bit integer IO format:%lld Problem Description A ...

  10. linux下的安装百度云网盘

    linux下的百度网盘 (2014-10-20 18:01:14) 标签: linux 百度网盘 网盘 百度 forlinux 分类: 技术博文 百度网盘说实话,其实我挺喜欢的,好处什么的,就不说了, ...