| Codeforces Round 1136 (Div. 1) |
|---|
| Finished |
Patchouli Knowledge loves writing strings in her notebook, and her assistant has found a scramble spell — a permutation $$$p$$$ that transforms a string $$$x$$$ into the string $$$y$$$ with $$$y_i = x_{p_i}$$$ for all $$$1 \le i \le n$$$. Here $$$x$$$ and $$$y$$$ have length $$$n$$$, and $$$p$$$ is a permutation$$$^{\text{∗}}$$$ of $$$\{1, 2, \ldots, n\}$$$.
Define $$$a_k$$$ as the string obtained by first cyclically shifting $$$s$$$ to the left by $$$k$$$ positions, then applying the scramble spell with $$$p$$$.
Formally, let $$$s^{(k)}$$$ be the string such that $$$s^{(k)}_i = s_{(i + k - 1) \bmod n + 1}$$$ for $$$1 \le i \le n$$$. Then $$$a_{k,i} = s^{(k)}_{p_i}$$$.
Patchouli wants to know the maximum value of $$$\operatorname{lcp}(a_i, a_j)$$$ over all unordered pairs of strings in $$$[a_0, a_1, \ldots, a_{n-1}]$$$. That is, find $$$$$$ \max_{0 \le i \lt j \lt n} \operatorname{lcp}(a_i, a_j), $$$$$$ where $$$\operatorname{lcp}(x, y)$$$ is the length of the longest common prefix of strings $$$x$$$ and $$$y$$$.
$$$^{\text{∗}}$$$A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows.
The first line of each case contains an integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
The second line of each case contains a string $$$s$$$ of length $$$n$$$, consisting of lowercase English letters.
The third line of each case contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that $$$p_1, p_2, \ldots, p_n$$$ is a permutation of $$$\{1, 2, \ldots, n\}$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output a single integer — the maximum value of $$$\operatorname{lcp}(a_i, a_j)$$$ over all pairs of indices $$$0 \le i \lt j \lt n$$$.
32ab1 23aab3 2 110aaaaaaaabb1 3 5 7 9 2 4 6 8 10
018
In the first test case, $$$a_0 = \texttt{ab}$$$ and $$$a_1 = \texttt{ba}$$$, so the answer is $$$0$$$.
In the second test case, $$$a_0 = \texttt{baa}$$$, $$$a_1 = \texttt{aba}$$$, and $$$a_2 = \texttt{aab}$$$, so the answer is $$$1$$$.
In the third test case, $$$a_0$$$ and $$$a_1$$$ share the first $$$8$$$ characters and differ in the $$$9$$$-th one, while no two of the strings $$$a_0, a_1, \ldots, a_9$$$ share a prefix of length $$$9$$$. Hence, the answer is $$$8$$$.
| Name |
|---|


