#P4075. [SDOI2016] 模式字符串
[SDOI2016] 模式字符串
Description
You are given a tree with nodes, where each node has a character. The characters are uppercase letters A to Z. You are also given a pattern string of length , where each character is also an uppercase letter A to Z.
Alice wants to know how many ordered pairs of nodes satisfy that the string formed by the shortest path from to in can be obtained by repeating the pattern string some number of times.
Here the pair is ordered, meaning and are considered different.
Repeating a pattern string means concatenating several copies of the pattern string one after another (without overlap). For example, when 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 XYXY, since repetitions must be an integer number, XYXYXY cannot be regarded as obtained by repeating some number of times.
Input Format
There are multiple test cases.
The first line contains an integer , the total number of test cases.
For each test case:
- The first line contains two integers, the number of nodes of the tree and the pattern length . Nodes are numbered from to .
- The next line gives uppercase letters (as a string of length ), where the -th character corresponds to the -th node on the tree.
- The next lines each contain two integers and , indicating an undirected edge of the tree.
- The last line gives the pattern string , which is a string of length consisting of uppercase letters.
Output Format
Output lines, one for each test case.
Each line should contain one integer, the number of ordered pairs such that the string formed by the path from to is exactly an integer number of repetitions of the pattern string .
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
, , .
Translated by ChatGPT 5
京公网安备 11011102002149号