#P4075. [SDOI2016] 模式字符串

    ID: 2980 远端评测题 2000ms 125MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>字符串2016各省省选点分治山东分治

[SDOI2016] 模式字符串

Description

You are given a tree TT with nn nodes, where each node has a character. The characters are uppercase letters A to Z. You are also given a pattern string SS of length mm, where each character is also an uppercase letter A to Z.

Alice wants to know how many ordered pairs of nodes (u,v)(u,v) satisfy that the string formed by the shortest path from uu to vv in TT can be obtained by repeating the pattern string SS some number of times.

Here the pair (u,v)(u,v) is ordered, meaning (u,v)(u,v) and (v,u)(v,u) are considered different.

Repeating a pattern string means concatenating several copies of the pattern string ss one after another (without overlap). For example, when S=S= PLUS, repeating it twice yields PLUSPLUS, repeating it three times yields PLUSPLUSPLUS. Note that the number of repetitions must be an integer. For example, when S=S= XYXY, since repetitions must be an integer number, XYXYXY cannot be regarded as obtained by repeating SS some number of times.

Input Format

There are multiple test cases.

The first line contains an integer CC, the total number of test cases.

For each test case:

  • The first line contains two integers, the number of nodes nn of the tree TT and the pattern length mm. Nodes are numbered from 11 to nn.
  • The next line gives nn uppercase letters (as a string of length nn), where the ii-th character corresponds to the ii-th node on the tree.
  • The next n1n-1 lines each contain two integers uu and vv, indicating an undirected edge of the tree.
  • The last line gives the pattern string SS, which is a string of length mm consisting of uppercase letters.

Output Format

Output CC lines, one for each test case.

Each line should contain one integer, the number of ordered pairs (u,v)(u,v) such that the string formed by the path from uu to vv is exactly an integer number of repetitions of the pattern string SS.

1
11 4
IODSSDSOIOI
1 2
2 3
3 4
1 5
5 6
6 7
3 8
8 9
6 10
10 11
SDOI
5

Hint

1C101 \leq C \leq 10, 3n1063 \leq \sum n \leq 10^6, 3m1063 \leq \sum m \leq 10^6.

Translated by ChatGPT 5