题目链接 字母树

(以每个点为根遍历,插入到trie中,统计答案即可)——SamZhang

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N = 5010; struct node{
int son[26];
int cnt, tot;
int sum[27];
} trie[N]; int n, m, q; vector <int> v[N];
vector <char> ch[N]; int ans[N][N]; void build_trie(int x, int fa, int p){
++trie[p].cnt;
int i = -1;
for (auto u : v[x]){
++i;
if (u == fa) continue;
int c = ch[x][i] - 'a';
if (trie[p].son[c] == 0){
++m;
trie[p].son[c] = m;
} build_trie(u, x, trie[p].son[c]);
}
} void get_answer(int x, int fa, int p, int cnt, int ans[]){
ans[x] = cnt;
cnt += trie[p].cnt;
int i = -1;
for (auto u : v[x]){
++i;
if (u == fa) continue;
int c = ch[x][i] - 'a';
get_answer(u, x, trie[p].son[c], cnt + trie[p].sum[c], ans);
}
} int main(){ scanf("%d%d", &n, &q);
rep(i, 1, n - 1){
int x, y;
char z[10];
scanf("%d%d%s", &x, &y, z);
v[x].push_back(y);
v[y].push_back(x);
ch[x].push_back(z[0]);
ch[y].push_back(z[0]);
} rep(i, 1, n){
m = 0;
memset(trie, 0, sizeof trie);
build_trie(i, 0, 0);
dec(j, m, 0){
trie[j].tot = trie[j].cnt;
rep(k, 0, 25){
trie[j].sum[k + 1] = trie[j].sum[k];
if (trie[j].son[k] > 0){
trie[j].tot += trie[trie[j].son[k]].tot;
trie[j].sum[k + 1] += trie[trie[j].son[k]].tot;
}
} rep(k, 1, 25){
trie[trie[j].son[k - 1]].cnt;
} } get_answer(i, 0, 0, 0, ans[i]);
} while (q--){
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", ans[x][y] - 1);
} return 0;
}

LibieOJ 6170 字母树 (Trie)的更多相关文章

  1. Atitit 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树 attilax理解与总结

    Atitit 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树 attilax理解与总结 1.1. 树形结构-- 一对多的关系1 1.2. 树的相关术语: 1 1.3. 常见的树形结构 ...

  2. 字典树(Trie Tree)

    在图示中,键标注在节点中,值标注在节点之下.每一个完整的英文单词对应一个特定的整数.Trie 可以看作是一个确定有限状态自动机,尽管边上的符号一般是隐含在分支的顺序中的.键不需要被显式地保存在节点中. ...

  3. 字典树(Trie树)的实现及应用

    >>字典树的概念 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树.与二叉查找树不同,Trie树的 ...

  4. [转载]字典树(trie树)、后缀树

    (1)字典树(Trie树) Trie是个简单但实用的数据结构,通常用于实现字典查询.我们做即时响应用户输入的AJAX搜索框时,就是Trie开始.本质上,Trie是一颗存储多个字符串的树.相邻节点间的边 ...

  5. [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序

    一. 题目 487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 274040   Accepted: 48891 ...

  6. 『字典树 trie』

    字典树 (trie) 字典树,又名\(trie\)树,是一种用于实现字符串快速检索的树形数据结构.核心思想为利用若干字符串的公共前缀来节约储存空间以及实现快速检索. \(trie\)树可以在\(O(( ...

  7. UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)

    题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...

  8. K:单词查找树(Trie)

      单词查找树,又称前缀树或字典树,是一种有序树,用于保存关联数组,其中的键通常是字符串.Trie可以看作是一个确定有限状态自动机(DFA).与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中 ...

  9. 字典树 trie

    Trie树        Trie树,就是字母树.Trie树是多叉树,每个节点为一个字母.其根节点为象征节点(就是说没有含义,但是存在这个节点),从根节点开始建立,每个节点至多为26个子节点(不要我说 ...

随机推荐

  1. 【android】【转发】Android中PX、DP、SP的区别

    转载 http://blog.csdn.net/donkor_/article/details/77680042 前言: 众所周知,Android厂商非常多,各种尺寸的android手机.平板层出不穷 ...

  2. 【mysql】mysql存储过程实例

    ```mysql DELIMITER $$   DROP PROCEDURE IF EXISTS `system_number_update` $$   CREATE DEFINER=`root`@` ...

  3. 多线程辅助类之CountDownLatch(三)

    CountDownLatch信号灯是一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待.它可以实现多线程的同步互斥功能,和wait和notify方法实现功能类似,具体 ...

  4. R-data.table

    data.table可以扩展和增强data.frame的功能,在分组操作和组合时访问速度更快. require(data.table) theDT = data.table(A=1:10, B=let ...

  5. leetcode-22-string

    521. Longest Uncommon Subsequence I find the longest uncommon subsequence of this group of two strin ...

  6. Divisibility by 25 CodeForces - 988E

    You are given an integer nn from 11 to 10181018 without leading zeroes. In one move you can swap any ...

  7. Spring MVC+Mybatis 多数据源配置及发现的几个问题

    1.CustomerContextHolder 数据源管理类,负责管理当前的多个数据源,基于ThreadLocal实现,对每个线程设置不同的目标数据源 public class CustomerCon ...

  8. HDU 4866 Shooting 扫描线 + 主席树

    题意: 在二维平面的第一象限有\(n(1 \leq n \leq 10^5)\)条平行于\(x\)轴的线段,接下来有\(m\)次射击\(x \, a \, b \, c\). 每次射击会获得一定的分数 ...

  9. Python动态属性和特性(一)

    在Python中,数据的属性和处理数据的方法统称为属性.其实,方式只是可调用的属性.除了这二者之外,我们还可以创建特性(property),在不改变类接口的前提下,使用存取方法(即读取值和设置值方法) ...

  10. springboot-vue-前后端数据交互

    前端项目: pom文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...