#P6088. [JSOI2015] 字符串树
[JSOI2015] 字符串树
Description
A string tree is essentially still a tree, i.e., a connected undirected acyclic graph with nodes and edges, where nodes are numbered from to . Different from an ordinary tree, each edge in the tree corresponds to a string.
When Mengmeng and JYY are playing under the tree, Mengmeng decides to test JYY. Each time, Mengmeng writes down a string and two nodes . JYY needs to immediately answer: on the shortest path between and (i.e., the path with the fewest edges; since it is a tree, this path is unique), how many edge strings have as a prefix.
Although JYY is good at programming, he is not good at string processing. So he asks you to help solve Mengmeng's problem.
Input Format
The first line contains an integer , representing the number of nodes in the string tree.
The next lines each contain two integers , followed by a string , meaning there is an edge directly connecting node and node , and the string on this edge is . The input guarantees that the given graph is a valid tree.
The next line contains an integer , representing the number of questions from Mengmeng.
The next lines each contain two integers , followed by a string , meaning the question asks: on the shortest path between node and node , how many edge strings have as a prefix.
Output Format
Output lines. Each line should contain the answer to the corresponding question.
4
1 2 ab
2 4 ac
1 3 bc
3
1 4 a
3 4 b
3 2 ab
2
1
1
Hint
For of the testdata, . The total length of all input strings does not exceed , and all strings contain only lowercase letters a~z.
Translated by ChatGPT 5
京公网安备 11011102002149号