#P13960. [ICPC 2023 Nanjing R] 后缀结构
[ICPC 2023 Nanjing R] 后缀结构
Description
For a string , let be the prefix . In particular, is empty string.
For two strings and , let be the concatenation .
You are given a string of length and a tree with vertices labeled with rooted at vertex . Each edge is associated with a character. Please note that in this problem, the alphabet may contain more than characters.
Consider the following function $$f(i,j) = \max{d(x) \mid s_x\text{ is a suffix of }s_i+ \mathrm{pre}(t,j)}$$ where be the concatenation of characters on the shortest path from root to vertex and be the number of edges on the shortest path from the root to vertex .
Your task is to compute the values of where .
Note that is the empty string and empty string is a suffix of any string.
Input Format
There are multiple test cases. The first line of the input contains an integer indicating the number of test cases. For each test case:
The first line contains two integers and ().
The second line contains integers () where indicates the parent of vertex .
The third line contains integers () where indicates that the edge from vertex to vertex is associated with the -th character from the alphabet. It is guaranteed that or for all .
The fourth line contains integers () where is the -th character of string .
It is guaranteed that neither the sum of nor the sum of will exceed .
Output Format
For each test case output one line containing integers separated by a space.
Please, DO NOT output extra spaces at the end of each line, or your solution may be considered incorrect!
2
11 3
0 1 2 0 4 5 4 6 0 9 10
1 3 2 2 1 3 4 1 3 2 1
3 2 4
5 16
0 0 0 1 4
1 2 3 2 2
2 1 3 3 2 1 3 2 1 3 2 2 1 1 2 1
17 26 22
8 5 5 5 5 5 5 5 5 5 5 5 5 5 10 5
Hint
Let's calculate and in the first sample test case to help you further understand. We have so . As is its longest suffix existing in the tree, . Also and is its longest suffix existing in the tree, so .
京公网安备 11011102002149号