此题花费我整整三天的功夫。还在NoiP贴吧发过贴。

最后发现trie树建新节点时信息未完全复制,真是愚蠢之极。

言归正传。

如果我们已经知道了每个点上的trie树那么询问就是sum[x]+sum[y]-sum[lca(x,y)]*2

然后就是trie树变可持久化。

DFS2中插入所有字符串,建立新节点,复制出现次数与trie树的next指针。

然后就没有然后了。

  1. map:array[..,'a'..'z']of longint;
  2. t:array[..]of record
  3. s:longint;
  4. end;
  5. root:array[..]of longint;
  6. head,vet,next,dep:array[..]of longint;
  7. f:array[..,..]of longint;
  8. len:array[..]of string;
  9. n,i,x,y,tot,tmp,cnt,j,k,q:longint;
  10. zyd,ch1,ch:string;
  11.  
  12. procedure add(a,b:longint;c:string);
  13. begin
  14. inc(tot);
  15. next[tot]:=head[a];
  16. vet[tot]:=b;
  17. len[tot]:=c;
  18. head[a]:=tot;
  19. end;
  20.  
  21. procedure dfs1(u:longint);
  22. var e,v,i:longint;
  23. begin
  24. for i:= to do
  25. begin
  26. if dep[u]<(<<i) then break;
  27. f[u,i]:=f[f[u,i-],i-];
  28. end;
  29. e:=head[u];
  30. while e<> do
  31. begin
  32. v:=vet[e];
  33. if v<>f[u,] then
  34. begin
  35. dep[v]:=dep[u]+;
  36. f[v,]:=u;
  37. dfs1(v);
  38. end;
  39. e:=next[e];
  40. end;
  41. end;
  42.  
  43. procedure ins(var x:longint;i:longint);
  44. begin
  45. inc(cnt); t[cnt]:=t[x];
  46. map[cnt]:=map[x];
  47. x:=cnt; inc(t[x].s);
  48. if i<=length(zyd) then ins(map[x,zyd[i]],i+);
  49. end;
  50.  
  51. function query(a,b,c,i:longint):longint;
  52. begin
  53. // writeln(a,' ',b,' ',c);
  54. if i>length(zyd) then exit(t[a].s+t[b].s-t[c].s*);
  55. exit(query(map[a,zyd[i]],map[b,zyd[i]],map[c,zyd[i]],i+));
  56. end;
  57.  
  58. procedure dfs2(u:longint);
  59. var e,v:longint;
  60. begin
  61. e:=head[u];
  62. while e<> do
  63. begin
  64. v:=vet[e];
  65. if v<>f[u,] then
  66. begin
  67. root[v]:=root[u];
  68. zyd:=len[e];
  69. ins(root[v],);
  70. dfs2(v);
  71. end;
  72. e:=next[e];
  73. end;
  74. end;
  75.  
  76. procedure swap(var x,y:longint);
  77. var t:longint;
  78. begin
  79. t:=x; x:=y; y:=t;
  80. end;
  81.  
  82. function lca(x,y:longint):longint;
  83. var i,d:longint;
  84. begin
  85. if dep[x]<dep[y] then swap(x,y);
  86. d:=dep[x]-dep[y];
  87. for i:= to do
  88. if d and (<<i)> then x:=f[x,i];
  89. for i:= downto do
  90. if f[x,i]<>f[y,i] then
  91. begin
  92. x:=f[x,i]; y:=f[y,i];
  93. end;
  94. if x=y then exit(x);
  95. exit(f[x,]);
  96. end;
  97.  
  98. begin
  99. assign(input,'strings.in'); reset(input);
  100. assign(output,'strings.out'); rewrite(output);
  101. readln(n);
  102. for i:= to n- do
  103. begin
  104. readln(ch);
  105. j:=; x:=;
  106. while (ch[j]>='')and(ch[j]<='') do
  107. begin
  108. x:=x*+ord(ch[j])-ord('');
  109. inc(j);
  110. end;
  111. inc(j); y:=;
  112. while (ch[j]>='')and(ch[j]<='') do
  113. begin
  114. y:=y*+ord(ch[j])-ord('');
  115. inc(j);
  116. end;
  117. inc(j);
  118. ch1:='';
  119. for k:=j to length(ch) do ch1:=ch1+ch[k];
  120. add(x,y,ch1);
  121. add(y,x,ch1);
  122. end;
  123.  
  124. //f[1,0]:=1;
  125.  
  126. dfs1();
  127. dfs2();
  128. readln(q);
  129. for i:= to q do
  130. begin
  131. readln(ch);
  132. j:=; x:=;
  133. while (ch[j]>='')and(ch[j]<='') do
  134. begin
  135. x:=x*+ord(ch[j])-ord('');
  136. inc(j);
  137. end;
  138. inc(j); y:=;
  139. while (ch[j]>='')and(ch[j]<='') do
  140. begin
  141. y:=y*+ord(ch[j])-ord('');
  142. inc(j);
  143. end;
  144. inc(j);
  145. ch1:='';
  146. for k:=j to length(ch) do ch1:=ch1+ch[k];
  147. tmp:=lca(x,y);
  148. zyd:=ch1;
  149. writeln(query(root[x],root[y],root[tmp],));
  150. end;
  151.  
  152. close(input);
  153. close(output);
  154. end.

2016.12.25

学会主席树后重写了一遍,感觉异常轻松

因为此题询问的是边上的信息,所以LCA点不会被重复计算,询问时也就不用-1

如果是点则要-1

  1. var map:array[..,'a'..'z']of longint;
  2. t:array[..]of longint;
  3. root:array[..]of longint;
  4. head,vet,next,dep,flag:array[..]of longint;
  5. f:array[..,..]of longint;
  6. len:array[..]of string;
  7. n,i,x,y,tot,tmp,cnt,j,k,s,m,l,q:longint;
  8. ch,h:string;
  9.  
  10. procedure swap(var x,y:longint);
  11. var t:longint;
  12. begin
  13. t:=x; x:=y; y:=t;
  14. end;
  15.  
  16. procedure add(a,b:longint;c:string);
  17. begin
  18. inc(tot);
  19. next[tot]:=head[a];
  20. vet[tot]:=b;
  21. len[tot]:=c;
  22. head[a]:=tot;
  23. end;
  24.  
  25. function lca(x,y:longint):longint;
  26. var d,i:longint;
  27. begin
  28. if dep[x]<dep[y] then swap(x,y);
  29. d:=dep[x]-dep[y];
  30. for i:= to do
  31. if d and (<<i)> then x:=f[x,i];
  32. for i:= downto do
  33. if f[x,i]<>f[y,i] then
  34. begin
  35. x:=f[x,i]; y:=f[y,i];
  36. end;
  37. if x=y then exit(x);
  38. exit(f[x,]);
  39. end;
  40.  
  41. procedure build(i:longint;var p:longint);
  42. begin
  43. inc(cnt); t[cnt]:=t[p];
  44. map[cnt]:=map[p];
  45. p:=cnt; inc(t[p]);
  46. if i<=l then build(i+,map[p,h[i]]);
  47. end;
  48.  
  49. procedure dfs(u:longint);
  50. var e,v,i:longint;
  51. begin
  52. for i:= to do
  53. begin
  54. if dep[u]<(<<i) then break;
  55. f[u,i]:=f[f[u,i-],i-];
  56. end;
  57. flag[u]:=; e:=head[u];
  58. while e<> do
  59. begin
  60. v:=vet[e];
  61. if flag[v]= then
  62. begin
  63. dep[v]:=dep[u]+;
  64. f[v,]:=u;
  65. root[v]:=root[u];
  66. h:=len[e]; l:=length(h);
  67. build(,root[v]);
  68. dfs(v);
  69. end;
  70. e:=next[e];
  71. end;
  72. end;
  73.  
  74. function query(x,y,z,i:longint):longint;
  75. begin
  76. if i=l+ then exit(t[x]+t[y]-*t[z]);
  77. exit(query(map[x,h[i]],map[y,h[i]],map[z,h[i]],i+));
  78. end;
  79.  
  80. begin
  81. assign(input,'bzoj4477.in'); reset(input);
  82. assign(output,'bzoj4477.out'); rewrite(output);
  83. readln(n);
  84. for i:= to n- do
  85. begin
  86. readln(ch);
  87. k:=length(ch); x:=; y:=; h:='';
  88. s:=;
  89. for j:= to k do
  90. begin
  91. if ch[j]=' ' then begin inc(s); continue; end;
  92. if s= then x:=x*+ord(ch[j])-ord('');
  93. if s= then y:=y*+ord(ch[j])-ord('');
  94. if s= then h:=h+ch[j];
  95. end;
  96. add(x,y,h);
  97. add(y,x,h);
  98. end;
  99. dfs();
  100. readln(m);
  101. for i:= to m do
  102. begin
  103. readln(ch);
  104. k:=length(ch); x:=; y:=; s:=; h:='';
  105. for j:= to k do
  106. begin
  107. if ch[j]=' ' then begin inc(s); continue; end;
  108. if s= then x:=x*+ord(ch[j])-ord('');
  109. if s= then y:=y*+ord(ch[j])-ord('');
  110. if s= then h:=h+ch[j];
  111. end;
  112. q:=lca(x,y);
  113. l:=length(h);
  114. writeln(query(root[x],root[y],root[q],));
  115. end;
  116. close(input);
  117. close(output);
  118. end.

【BZOJ4477】字符串树(可持久化Trie)的更多相关文章

  1. BZOJ4477[Jsoi2015]字符串树——可持久化trie树

    题目描述 萌萌买了一颗字符串树的种子,春天种下去以后夏天就能长出一棵很大的字符串树.字符串树很奇特,树枝上都密密麻麻写满了字符串,看上去很复杂的样子.[问题描述]字符串树本质上还是一棵树,即N个节点N ...

  2. luogu P6088 [JSOI2015]字符串树 可持久化trie 线段树合并 树链剖分 trie树

    LINK:字符串树 先说比较简单的正解.由于我没有从最简单的考虑答案的角度思考 所以... 下次还需要把所有角度都考察到. 求x~y的答案 考虑 求x~根+y~根-2*lca~根的答案. 那么问题变成 ...

  3. [bzoj4477 Jsoi2015]字符串树 (可持久化trie)

    传送门 Solution 复习下tire( ̄▽ ̄)/ 裸的可持久化tire,我用树剖求了下LCA Code #include <cstdio> #include <cstring&g ...

  4. BZOJ 4477: [Jsoi2015]字符串树 可持久化trie树

    这个是真——可持久化字典树..... code: #include <bits/stdc++.h> #define N 100006 #define setIO(s) freopen(s& ...

  5. 4.24 省选模拟赛 欧珀瑞特 主席树 可持久化trie树

    很容易的一道题目.大概.不过我空间计算失误MLE了 我草草的计算了一下没想到GG了. 关键的是 我学了一个dalao的空间回收的方法 但是弄巧成拙了. 题目没有明确指出 在任意时刻数组长度为有限制什么 ...

  6. 【BZOJ-4212】神牛的养成计划 Trie树 + 可持久化Trie树

    4212: 神牛的养成计划 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 136  Solved: 27[Submit][Status][Discus ...

  7. 【BZOJ4212】神牛的养成计划 Trie树+可持久化Trie树

    [BZOJ4212]神牛的养成计划 Description Hzwer成功培育出神牛细胞,可最终培育出的生物体却让他大失所望...... 后来,他从某同校女神 牛处知道,原来他培育的细胞发生了基因突变 ...

  8. P4585-[FJOI2015]火星商店问题【线段树,可持久化Trie】

    正题 题目链接:https://www.luogu.com.cn/problem/P4585 题目大意 \(n\)个集合,开始每个集合中有一个数字. 开启新的一天并且往集合\(s\)中插入数字\(v\ ...

  9. BZOJ4477 JSOI2015字符串树(可持久化trie)

    树上建可持久化trie即可,有点过于裸了.darkbzoj过了然而在bzoj一直wa,不知道哪有锅. #include<iostream> #include<cstdio> # ...

  10. 【BZOJ4477】[JSOI2015]字符串树(Trie树)

    [BZOJ4477][JSOI2015]字符串树(Trie树) 题面 BZOJ 题解 对于每个点维护其到根节点的所有字符串构成的\(Trie\),显然可持久化一下就很好写了. 然后每次询问就是\(u+ ...

随机推荐

  1. 爬虫学习(三)——get请求参数解析

    get请求:        用户输入搜索的内容,发送请求,将请求的内容保存起来.        get请求的本质是在地址栏中输入参数进行的一种请求方式. 解析参数使用urllib.parse impo ...

  2. 二十九、MySQL 序列使用

    MySQL 序列使用 MySQL 序列是一组整数:1, 2, 3, ...,由于一张数据表只能有一个字段自增主键, 如果你想实现其他字段也实现自动增加,就可以使用MySQL序列来实现. 本章我们将介绍 ...

  3. .pyc是什么鬼

    .pyc是个什么鬼? 1. Python是一门解释型语言? 我初学Python时,听到的关于Python的第一句话就是,Python是一门解释性语言,我就这样一直相信下去,直到发现了*.pyc文件的存 ...

  4. 解决Uva网站打开慢的问题

    https://blog.csdn.net/richenyunqi/article/details/80990535

  5. Kali 远程登陆SSH

    一.配置SSH 编辑/etc/ssh/sshd_config 将#PasswordAuthentication no的注释去掉,将NO修改为YES //可以用密码登陆 将PermitRootLogin ...

  6. Js中的假值_ES5中定义的ToBoolean方法强制类型转换后值为false

    你不知道的Javascript(中)--ToBoolean javascript中的值可以分为以下两类: 1.可以被强制类型转换为false的值 2.其他(被强制类型转换为true的值) 假值---以 ...

  7. Ntdsutil.exe

    Ntdsutil.exe 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! Ntdsutil.exe 是一个为 Active Directory 提供管理设施的命令行工具 ...

  8. 【palindrome partitioning II】cpp

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  9. 误删除pycharm项目中的文件,如何恢复?

    如果写代码的时候,不小心删除来某个文件夹或者文件,而且删除后回收站也找不到, 可以使用如下方法恢复: 选择 Local History -> Show History : 选中需要reset到的 ...

  10. hnust 搬书

    问题 G: 搬书 时间限制: 1 Sec  内存限制: 128 MB提交: 576  解决: 49[提交][状态][讨论版] 题目描述 XCQ队长要退役啦,由于队长常年刷题,机位上摆着各类算法书,一个 ...