题目链接 字母树

(以每个点为根遍历,插入到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. 如何使用jmeter做接口测试

    1.传参:key=value形式 2.传参:json格式 3.jmeter上传文件 4.jmeter传cookie 或者使用 HTTP Cookie管理器

  2. ESP8266入门学习笔记1:资料获取

    乐鑫官网:https://www.espressif.com/zh-hans/products/hardware/esp8266ex/overview 乐鑫资料:https://www.espress ...

  3. 转:跟我一起写Makefile (PDF重制版)

    原文地址:http://seisman.info/how-to-write-makefile.html 其它一些问题  不妨看一下:http://blog.csdn.net/huyansoft/art ...

  4. LeetCode(189) Rotate Array

    题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...

  5. hdu 6318

    Long long ago, there was an integer sequence a.Tonyfang think this sequence is messy, so he will cou ...

  6. Mysql新建数据库、删除数据库

    新建数据库 create database db_name; //db_name为新建数据库的名字 mysql> create database db_name; Query OK, row a ...

  7. UVa 10723 LCS变形 Cyborg Genes

    题解转自: UVA 10723 Cyborg Genes - Staginner - 博客园 首先这个题目肯定是按最长公共子序列的形式进行dp的,因为只有保证消去的一部分是最长公共子序列才能保证最后生 ...

  8. fiddler 抓包数据不会自动下拉解决方法

    选中 view 里面的 AutoScroll Session List 即可

  9. STP

    生成树协议  spanning-tree protocol     网络中额外添加的链路连接着路由器和交换机 会引起流量的环路   当一个交换机的连接丢失时 另一条链路能快速地取代失败的链路  并且不 ...

  10. 【Go】Panic函数

    panic(运行时恐慌)是一种只会在程序运行时才回抛出来的异常.在panic被抛出之后,如果没有在程序里添加任何保护措施的话,程序就会在打印出panic的详情,终止运行. 如果一个panic是无意间引 ...