CF1045J.Moonwalk challenge

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Since astronauts from BubbleCup XI mission finished their mission on the Moon and are big fans of famous singer, they decided to spend some fun time before returning to the Earth and hence created a so called "Moonwalk challenge" game.

Teams of astronauts are given the map of craters on the Moon and direct bidirectional paths from some craters to others that are safe for "Moonwalking". Each of those direct paths is colored in one color and there is unique path between each two craters. Goal of the game is to find two craters such that given array of colors appears most times as continuous subarray on the path between those two craters (overlapping appearances should be counted).

To help your favorite team win, you should make a program that, given the map, answers the queries of the following type: For two craters and array of colors answer how many times given array appears as continuous subarray on the path from the first crater to the second.

Colors are represented as lowercase English alphabet letters.

输入格式

In the first line, integer NN (2N105)(2 \leq N \leq 10^5) — number of craters on the Moon. Craters are numerated with numbers 11 to NN .

In next N1N-1 lines, three values u,v,Lu, v, L (1u,vN,L{a,...,z})(1 \leq u, v \leq N, L \in \{a, ..., z\}) — denoting that there is a direct path with color LL between craters uu and vv .

Next line contains integer QQ (1Q105)(1 \leq Q \leq 10^5) — number of queries.

Next QQ lines contain three values u,vu, v (1u,vN)(1 \leq u, v \leq N) and SS (S100)(|S| \leq 100) , where uu and vv are the two cratersfor which you should find how many times array of colors SS (represented as string) appears on the path from uu to vv .

输出格式

For each query output one number that represents number of occurrences of array S on the path from uu to vv .

输入输出样例

  • 输入#1

    6
    2 3 g
    3 4 n
    5 3 o
    6 1 n
    1 2 d
    7
    1 6 n
    6 4 dg
    6 4 n
    2 5 og
    1 2 d
    6 5 go
    2 3 g
    

    输出#1

    1
    1
    2
    0
    1
    1
    1
    
首页