1. /*
  2. 动态求right集合的大小
  3. LCT维护parent树即可
  4. 注意 由于树是有向的不会换根并且每次操作单一, 于是不需要维护子树和(写起来很麻烦)
  5. 直接打标记修改即可
  6. */
  7. #include<cstdio>
  8. #include<algorithm>
  9. #include<iostream>
  10. #include<cstring>
  11. #include<queue>
  12. #define ll long long
  13. #define M 1200010
  14. #define mmp make_pair
  15. using namespace std;
  16. int read()
  17. {
  18. int nm = 0, f = 1;
  19. char c = getchar();
  20. for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
  21. for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
  22. return nm * f;
  23. }
  24. int mask, ans;
  25. struct Lct
  26. {
  27. #define ls ch[x][0]
  28. #define rs ch[x][1]
  29. int ch[M][2], rev[M], ver[M], fa[M], s[M];
  30. bool isr(int x)
  31. {
  32. return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
  33. }
  34. void up(int x, int vv)
  35. {
  36. ver[x] += vv;
  37. s[x] += vv;
  38. }
  39. void pushdown(int x)
  40. {
  41. if(s[x])
  42. {
  43. up(ls, s[x]);
  44. up(rs, s[x]);
  45. s[x] = 0;
  46. }
  47. }
  48. void pd(int x)
  49. {
  50. if(!isr(x)) pd(fa[x]);
  51. pushdown(x);
  52. }
  53. void rotate(int x)
  54. {
  55. int y = fa[x], q = fa[y];
  56. bool dy = (ch[y][1] == x), dz = (ch[q][1] == y);
  57. if(!isr(y)) ch[q][dz] = x;
  58. fa[x] = q;
  59. fa[ch[x][dy ^ 1]] = y;
  60. ch[y][dy] = ch[x][dy ^ 1];
  61. ch[x][dy ^ 1] = y;
  62. fa[y] = x;
  63. }
  64. void splay(int x)
  65. {
  66. pd(x);
  67. while(!isr(x))
  68. {
  69. int y = fa[x], q = fa[y];
  70. if(!isr(y))
  71. {
  72. if((ch[y][1] == x) ^ (ch[q][1] == y))
  73. {
  74. rotate(x);
  75. }
  76. else rotate(y);
  77. }
  78. rotate(x);
  79. }
  80. }
  81. void access(int x)
  82. {
  83. for(int y = 0; x; y = x, x = fa[x]) splay(x), rs = y;
  84. }
  85. void link(int x, int y)
  86. {
  87. fa[x] = y;
  88. access(y);
  89. splay(y);
  90. up(y, ver[x]);
  91. }
  92. void cut(int x)
  93. {
  94. access(x), splay(x), up(ls, -ver[x]), fa[ls] = 0, ls = 0;
  95. }
  96. }T;
  97. int ch[M][26], len[M], fa[M], lst = 1, cnt = 1;
  98. void insert(int c)
  99. {
  100. int p = ++cnt, f = lst;
  101. lst = p;
  102. len[p] = len[f] + 1;
  103. T.ver[p] = 1;
  104. while(f && !ch[f][c]) ch[f][c] = p, f = fa[f];
  105. if(!f)
  106. {
  107. fa[p] = 1;
  108. T.link(p, 1);
  109. }
  110. else
  111. {
  112. int q = ch[f][c];
  113. if(len[q] == len[f] + 1)
  114. {
  115. T.link(p, q);
  116. fa[p] = q;
  117. }
  118. else
  119. {
  120. int nq = ++cnt;
  121. len[nq] = len[f] + 1;
  122. memcpy(ch[nq], ch[q], sizeof(ch[nq]));
  123. fa[nq] = fa[q];
  124. T.link(nq, fa[q]);
  125. fa[q] = fa[p] = nq;
  126. T.cut(q);
  127. T.link(q, nq);
  128. T.link(p, nq);
  129. while(f && ch[f][c] == q) ch[f][c] = nq, f = fa[f];
  130. }
  131. }
  132. }
  133. char s[M * 3];
  134. void work()
  135. {
  136. int mlgb = mask;
  137. int l = strlen(s);
  138. for(int i = 0; i < l; i++)
  139. {
  140. mlgb = (mlgb * 131 + i) % l;
  141. swap(s[i], s[mlgb]);
  142. }
  143. // puts(s);
  144. }
  145. int main()
  146. {
  147. int q = read();
  148. scanf("%s", s + 1);
  149. int l = strlen(s + 1);
  150. for(int i = 1; i <= l; i++) insert(s[i] - 'A');
  151. while(q--)
  152. {
  153. scanf("%s", s + 1);
  154. if(s[1] == 'Q')
  155. {
  156. scanf("%s", s);
  157. l = strlen(s);
  158. work();
  159. int now = 1;
  160. for(int i = 0; i < l; i++) now = ch[now][s[i] - 'A'];
  161. if(!now) ans = 0;
  162. else
  163. {
  164. T.splay(now);
  165. ans = T.ver[now];
  166. }
  167. cout << ans << "\n";
  168. mask ^= ans;
  169. }
  170. else
  171. {
  172. scanf("%s", s);
  173. l = strlen(s);
  174. work();
  175. for(int i = 0; i < l; i++) insert(s[i] - 'A');
  176. }
  177. }
  178. return 0;
  179. }
  180. /*
  181. 2
  182. BBABBBBAAB
  183. ADD BBBAAB
  184. QUERY BBA
  185. */

bzoj2555 substring(LCT 后缀自动机)的更多相关文章

  1. [BZOJ2555]SubString LCT+后缀自动机

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

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

    题目 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支持这些操作. 输入 ...

  3. BZOJ2555: SubString(后缀自动机,LCT维护Parent树)

    Description 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支 ...

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

    Description 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支 ...

  5. BZOJ2555 SubString(后缀自动机+LCT)

    询问串放在SAM上不跳fail跑到的节点的|right|即为答案.用LCT维护parent树即可.可以直接维护子树信息,也可以转化为路径加.注意强制在线所使用的mask是作为参数传进去的. #incl ...

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

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

  7. spoj 7258 Lexicographical Substring Search (后缀自动机)

    spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...

  8. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

  9. bzoj 2555: SubString【后缀自动机+LCT】

    一直WA--找了半天错的发现居然是解密那里的mask其实是不能动的--传进去的会变,但是真实的那个不会变-- 然后就是后缀自动机,用LCT维护parent树了--注意不能makeroot,因为自动机的 ...

随机推荐

  1. MongoDB 设置账号和密码

    一.安装MongoDB 1.环境配置: i.操作系统:CentOS release 6.8 (Final) [root@iZ2ze2pbbffhmn53ao4tuaZ bin]# cat /etc/r ...

  2. Jumpserver 介绍

    安装jumperserver Sudo yum install –y git Su root Cd  /usr/local Mkdir jumpserver 安装等依赖包 yum -y install ...

  3. JavaScript 基本数据类型和引用类型的区别详解

    js基本数据类型: js基本数据类型包括:undefined,null,number,boolean,string.基本数据类型是按值访问的,就是说我们可以操作保存在变量中的实际的值 1. 基本数据类 ...

  4. PL/SQL Developer 使用小技巧

    1.PL/SQL Developer记住登陆密码 在使用PL/SQL Developer时,为了工作方便希望PL/SQL Developer记住登录Oracle的用户名和密码: 设置方法:tools- ...

  5. 黄聪:php7配置php.ini使其支持<? ?>

    <? ?>这种写在php配置文件里php.ini法叫short_tags,默认是不打开的,也就是,在默认配置的php里,这样写法不被认为是php脚本的,除非设置 short_open_ta ...

  6. navigator 判断移动端是Android还是iOS

    let u = navigator.userAgent; let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > - ...

  7. ElasticSearch - Node

    elasticSearch node 的配置如下: # Every node can be configured to allow or deny being eligible as the mast ...

  8. git 对比两个分支差异

    比如我们有 2 个分支:master, dev,现在想查看这两个 branch 的区别,有以下几种方式: 1.查看 dev 有,而 master 中没有的: git log dev ^master 同 ...

  9. Excel导出文件流下载

    Controller.cs @CrossOrigin(allowCredentials="true", allowedHeaders="*", methods= ...

  10. Ubuntu 14.10 下安装伪分布式hbase 0.99.0

    HBase 安装分为:单击模式,伪分布式,完全分布式,在单机模式中,HBase使用本地文件系统而不是HDFS ,所有的服务和zooKeeper都运作在一个JVM中.本文是安装的伪分布式. 安装步骤如下 ...