A15095 | Shifting String
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Polycarp found the string $s$ and the permutation $p$ . Their lengths turned out to be the same and equal to $n$ .
A permutation of $n$ elements — is an array of length $n$ , in which every integer from $1$ to $n$ occurs exactly once. For example, $[1, 2, 3]$ and $[4, 3, 5, 1, 2]$ are permutations, but $[1, 2, 4]$ , $[4, 3, 2, 1, 2]$ and $[0, 1, 2]$ are not.
In one operation he can multiply $s$ by $p$ , so he replaces $s$ with string $new$ , in which for any $i$ from $1$ to $n$ it is true that $new_i = s_{p_i}$ . For example, with $s=wmbe$ and $p = [3, 1, 4, 2]$ , after operation the string will turn to $s=s_3 s_1 s_4 s_2=bwem$ .
Polycarp wondered after how many operations the string would become equal to its initial value for the first time. Since it may take too long, he asks for your help in this matter.
It can be proved that the required number of operations always exists. It can be very large, so use a 64-bit integer type.
A permutation of $n$ elements — is an array of length $n$ , in which every integer from $1$ to $n$ occurs exactly once. For example, $[1, 2, 3]$ and $[4, 3, 5, 1, 2]$ are permutations, but $[1, 2, 4]$ , $[4, 3, 2, 1, 2]$ and $[0, 1, 2]$ are not.
In one operation he can multiply $s$ by $p$ , so he replaces $s$ with string $new$ , in which for any $i$ from $1$ to $n$ it is true that $new_i = s_{p_i}$ . For example, with $s=wmbe$ and $p = [3, 1, 4, 2]$ , after operation the string will turn to $s=s_3 s_1 s_4 s_2=bwem$ .
Polycarp wondered after how many operations the string would become equal to its initial value for the first time. Since it may take too long, he asks for your help in this matter.
It can be proved that the required number of operations always exists. It can be very large, so use a 64-bit integer type.
输入格式
The first line of input contains one integer $t$ ( $1 \le t \le 5000$ ) — the number of test cases in input.
The first line of each case contains single integer $n$ ( $1 \le n \le 200$ ) — the length of string and permutation.
The second line of each case contains a string $s$ of length $n$ , containing lowercase Latin letters.
The third line of each case contains $n$ integers — permutation $p$ ( $1 \le p_i \le n$ ), all $p_i$ are different.
The first line of each case contains single integer $n$ ( $1 \le n \le 200$ ) — the length of string and permutation.
The second line of each case contains a string $s$ of length $n$ , containing lowercase Latin letters.
The third line of each case contains $n$ integers — permutation $p$ ( $1 \le p_i \le n$ ), all $p_i$ are different.
输出格式
Output $t$ lines, each of which contains the answer to the corresponding test case of input. As an answer output single integer — the minimum number of operations, after which the string $s$ will become the same as it was before operations.
输入输出样例
输入 #1
3 5 ababa 3 4 5 2 1 5 ababa 2 1 4 5 3 10 codeforces 8 6 1 7 5 2 9 3 10 4
输出 #1
1 6 12
In the first sample operation doesn't change the string, so it will become the same as it was after $1$ operations.
In the second sample the string will change as follows:
- $s$ = babaa
- $s$ = abaab
- $s$ = baaba
- $s$ = abbaa
- $s$ = baaab
- $s$ = ababa
In the second sample the string will change as follows:
- $s$ = babaa
- $s$ = abaab
- $s$ = baaba
- $s$ = abbaa
- $s$ = baaab
- $s$ = ababa
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted