#GL0000317. Chuỗi dịch chuyển (Shifting String)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chuỗi dịch chuyển (Shifting String)
Shifting String
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a string of lowercase English letters and a permutation of the integers from to . One operation simultaneously replaces all characters according to
The new string replaces the previous one after each operation. Find the smallest positive number of operations needed to recover the original string. Such a number always exists and may require 64-bit arithmetic.
Input
Line 1 contains . Line 2 contains , a string of exactly lowercase Latin letters. Line 3 contains integers , which form a permutation of .
Output
Print the minimum positive number of operations needed to restore the original string.
Subtasks
- Subtask 1 (20 points): ; consists of lowercase letters; is a permutation.
- Subtask 2 (30 points): ; consists of lowercase letters; is a permutation.
- Subtask 3 (50 points): ; consists of lowercase letters; is a permutation.
Examples
Example 1
Input
5
ababa
3 4 5 2 1
Output
1
Explanation
After one operation, the characters at positions again form ababa, so the minimum positive answer is .
Example 2
Input
5
ababa
2 1 4 5 3
Output
6
Explanation
Positions have period (ab), while the cycle on positions has period (aba). Both return simultaneously after operations.