1350 - Trie

时间限制:1秒 内存限制:128兆

104 次提交 35 次通过
题目描述
In computer science, a trie, is an ordered tree data structure that is used to store an associative array where the keys are usually strings. Each position in the tree shows what key it is associated with and All the descendants of a node have a common prefix of the string associated with that node, have and only have one more character than its father node. the root of the trie is associated with the empty string. For example, if we put 6 words of “to”, “tea”, “ted”, “ten”, “a”, “inn” to a trie, it will be the form of figure.  In the computer science, a trie is a tree. For a tree, we define the leave node is the node without any descendants and only have an edge connect in. So in this example, the leave node is “to”, “tea”, “ted”, “ten”, “a” and “inn”. And we define the distance between two node is the minimum edge from a node to another node must pass. So the distance between “a” and “te” is 3, “to” and “inn” is 5. Finally, we define the value of a node is the sum of the node to the entire leave node’s distance. And the value of a tree is equal the value of a node which have the minimum value. Now give you a list of words. Put them into a trie, and calculate the value of this trie.
输入
The first line is T, indicate there are T cases. For each case, the first line is N, indicate there are N words. Next N lines, each line have a word only include the lower case letters and no two words are the same. (N<=50 and the length of a word less than 10)
输出
For each case output a line with the value of the trie.
样例输入
  1. 2
  2. 6
  3. a
  4. to
  5. tea
  6. ted
  7. ten
  8. inn
  9. 4
  10. sa
  11. sb
  12. sc
  13. sd
样例输出
  1. Case #1: 13
  2. Case #2: 5
  3.  
  4. /////////////////////////////////////
  5. For the second case, if the root has only one child, its a special leaf and must be calculate.
提示
来源
Hong Zehua, HUST Campus 2009
  1. /**
  2. 题意:给几个字符串,构成一个字典树,求哪个节点到每个叶子的距离最短的,最短是多少
  3. 做法:先建立一个字典树,然后求公共祖先(求得是所有点到叶子节点的最短的距离)
  4. **/
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <queue>
  11. using namespace std;
  12. #define maxn 100010
  13. int tot = ;
  14. const int MAXN = ;
  15. const int DEG = ;
  16. int num[maxn];
  17. int tot2 = ;
  18. int tot3 = ;
  19. int num1[maxn];
  20. int indegree[maxn];
  21. int mmap[maxn];
  22. int mm = ;
  23. struct Node
  24. {
  25. int x;
  26. int y;
  27. int flag;
  28. Node() {};
  29. Node(int _x, int _y) {
  30. x = _x;
  31. y = _y;
  32. flag = ;
  33. }
  34. } node[maxn << ];
  35. struct Trie
  36. {
  37. int next[maxn][], end[maxn];
  38. int root;
  39. int L;
  40. int newnode() {
  41. for(int i = ; i < ; i++) {
  42. next[L][i] = -;
  43. }
  44. end[L++] = ;
  45. return L - ;
  46. }
  47. void init() {
  48. L = ;
  49. root = newnode();
  50. }
  51. void insert(char buf[]) {
  52. int len = strlen(buf);
  53. int now = root;
  54. for(int i = ; i < len; i++) {
  55. if(next[now][buf[i] - 'a'] == -) {
  56. next[now][buf[i] - 'a'] = newnode();
  57. node[tot].y = next[now][buf[i] - 'a'] + ;
  58. node[tot].x = now + ;
  59. node[tot].flag = ;
  60. tot++;
  61. }
  62. now = next[now][buf[i] - 'a'];
  63. }
  64. node[tot - ].flag = ;
  65. num[tot2++] = node[tot - ].y;
  66. end[now]++;
  67. }
  68. };
  69. Trie ac;
  70. struct Edge
  71. {
  72. int to;
  73. int next;
  74. } edge[MAXN * ];
  75. int head[MAXN], tot1;
  76. void addedge(int u, int v)
  77. {
  78. edge[tot1].to = v;
  79. edge[tot1].next = head[u];
  80. head[u] = tot1++;
  81. }
  82. void init()
  83. {
  84. tot1 = ;
  85. memset(head, -, sizeof(head));
  86. }
  87. int fa[MAXN << ][DEG];
  88. int deg[MAXN << ];
  89. void bfs(int root)
  90. {
  91. queue<int>que;
  92. deg[root] = ;
  93. fa[root][] = root;
  94. que.push(root);
  95. while(!que.empty())
  96. {
  97. int tmp = que.front();
  98. que.pop();
  99. for(int i = ; i < DEG; i++) {
  100. fa[tmp][i] = fa[fa[tmp][i - ]][i - ];
  101. }
  102. for(int i = head[tmp]; i != -; i = edge[i].next)
  103. { int v = edge[i].to;
  104. if(v == fa[tmp][]) {
  105. continue;
  106. }
  107. deg[v] = deg[tmp] + ;
  108. fa[v][] = tmp;
  109. que.push(v);
  110. }
  111. }
  112. }
  113. int LCA(int u, int v)
  114. {
  115. if(deg[u] > deg[v]) {
  116. swap(u, v);
  117. }
  118. int hu = deg[u], hv = deg[v];
  119. int tu = u, tv = v;
  120. for(int det = hv - hu, i = ; det; det >>= , i++)
  121. if(det & ) {
  122. tv = fa[tv][i];
  123. }
  124. if(tu == tv) {
  125. return tu;
  126. }
  127. for(int i = DEG - ; i >= ; i--)
  128. {
  129. if(fa[tu][i] == fa[tv][i]) {
  130. continue;
  131. }
  132. tu = fa[tu][i];
  133. tv = fa[tv][i];
  134. }
  135. return fa[tu][];
  136. }
  137. bool flag[MAXN];
  138. char buf[];
  139. int main()
  140. {
  141. //freopen("in.txt", "r", stdin);
  142. int Case = ;
  143. int T;
  144. scanf("%d", &T);
  145. while(T--)
  146. {
  147. int n;
  148. tot = ; ///边的数目
  149. tot2 = ; ///叶子节点的数目 如果根节点存在Case 2 中的情况也加进去
  150. tot3 = ; ///不是叶子的节点的节点
  151. mm = ;
  152. memset(node, , sizeof(node));
  153. memset(indegree, , sizeof(indegree));
  154. memset(flag, , sizeof(flag));
  155. scanf("%d", &n);
  156. ac.init();
  157. for(int i = ; i < n; i++)
  158. {
  159. scanf("%s", buf);
  160. ac.insert(buf);
  161. }
  162. init();
  163. int yy = ;
  164. for(int i = ; i < tot; i++)
  165. {
  166. addedge(node[i].x, node[i].y);
  167. addedge(node[i].y, node[i].x);
  168. indegree[node[i].x] ++;
  169. indegree[node[i].y] ++;
  170. yy = max(yy, max(node[i].x, node[i].y));
  171. }
  172. for(int i = ; i < tot; i++) {
  173. if(indegree[node[i].x] == ) {
  174. mmap[mm++] = node[i].x;
  175. }
  176. else {
  177. num1[tot3++] = node[i].x;
  178. }
  179. if(indegree[node[i].y] == ) {
  180. mmap[mm++] = node[i].y;
  181. }
  182. else {
  183. num1[tot3++] = node[i].y;
  184. }
  185. }
  186. sort(mmap, mmap + mm);
  187. sort(num1, num1 + tot3);
  188. int newmm = unique(mmap, mmap + mm) - mmap;
  189. int newtot3 = unique(num1, num1 + tot3) - num1;
  190. bfs();
  191. int mmin = 0xfffffff;
  192. for(int i = ; i <= yy; i++)
  193. {
  194. int temp = ;
  195. for(int j = ; j < newmm; j++)
  196. {
  197. if(i != mmap[j]) {
  198. int tt = LCA(i, mmap[j]);
  199. int res = ;
  200. res = abs(deg[i] - deg[tt]) + abs(deg[mmap[j]] - deg[tt]);
  201. temp += res;
  202. }
  203. }
  204. mmin = min(mmin, temp);
  205. }
  206. printf("Case #%d: %d\n", Case++, mmin);
  207. }
  208. return ;
  209. }

HUST-1350 Trie的更多相关文章

  1. 【LA3942-Remember the word 】Trie

    http://acm.hust.edu.cn/vjudge/problem/22109 题意:给定n个单词,一个字符串,问把这个字符串划分为若干个单词的连接(单词可重复使用)有多少种方案(mod200 ...

  2. 【hdu1251-统计难题】Trie

    http://acm.hust.edu.cn/vjudge/problem/16379 题意:给定多个单词,多次询问符合某前缀的单词有多少个. 题解:tire.数组开了5*10^6才A,不然就RE. ...

  3. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

  4. 基于trie树的具有联想功能的文本编辑器

    之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...

  5. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  6. hihocoder-1014 Trie树

    hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...

  7. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  8. Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5646   Accepted: 1226 Description In an ...

  9. 二分+DP+Trie HDOJ 5715 XOR 游戏

    题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

随机推荐

  1. linux学习(二)——汤哥的推荐书籍

    成为一名精通 Linux程序设计的高级程序员一直是不少朋友孜孜以求的目标. 根据中华英才网统计数据,北京地区 Linux 程序员月薪平均为 Windows程序员的 1.8 倍.Java 程序员的 2. ...

  2. JAXB使用方式

    JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向 ...

  3. Struts1之编码问题

    <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding=& ...

  4. 【bzoj1018】[SHOI2008]堵塞的交通traffic 线段树区间合并+STL-set

    题目描述 给出一张2*n的网格图,初始每条边都是不连通的.多次改变一条边的连通性或询问两个点是否连通. 输入 第一行只有一个整数C,表示网格的列数.接下来若干行,每行为一条交通信息,以单独的一行“Ex ...

  5. 8086汇编语言 调用声卡播放wav文件(sound blaster)

    开更 大概最后做了一个能播放无损音乐(无压缩.不需解码)的播放器 原理是基于dosbox的模拟声卡,通过硬件之间的相互通讯做到的 关于详细内容接下来再讲. 一.从dosbox入手 我们知道cpu可以直 ...

  6. Netscaler工作流程

    Netscaler工作流程 http://blog.51cto.com/caojin/1898310 Citrix Netscaler有很多功能模块来满足应用交付的需求,为了能够做好的配置和排错工作, ...

  7. [洛谷P3203][HNOI2010]弹飞绵羊

    题目大意:有$n$个节点,第$i$个节点有一个弹力系数$k_i$,当到达第$i$个点时,会弹到第$i+k_i$个节点,若没有这个节点($i+k_i>n$)就会被弹飞.有两个操作: $x:$询问从 ...

  8. [Leetcode] word ladder 单词阶梯

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  9. BZOJ4008. [HNOI2015]亚瑟王 期望概率dp

    看到这道题想什么? 一个好转移的状态由于T最多444所以把每个点控制在O(400000)以内,所以对于n和r最多乘一次因此猜f[n][r],f[r][n],首先一轮一轮的搞不好转移,那么先想一想f[n ...

  10. POJ1847:Tram(最短路)

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20116   Accepted: 7491 题目链接:http:/ ...