Just Shuffle the Input
InterviewTime limit3sMemory limit512 MB
Given strings s and t and a cyclic permutation p, find the fewest shuffles of t by p that make it a substring of s, or -1 if impossible.
- Level
Medium6 of 10
- Topics
- String matching, String, Hash map
- Solved
- No attempts yet
Statement
A permutation of size is a sequence of pairwise distinct numbers from to . We write for the -th element. denotes applied times, that is, . A permutation is cyclic if the smallest positive integer with equals .
You are given a string of length , a string of length , and a cyclic permutation of size . You want to become a substring of . To do this, you may apply the shuffle operation zero or more times. The shuffle operation replaces with such that the -th letter of equals the -th letter of for every from to .
A string is a substring of if there exists an with such that for every from to .
Determine whether you can obtain a substring of from . If you can, find the minimum number of shuffle operations needed.
Input
The first line contains two integers and (). The second line contains integers , the permutation you can apply. The next two lines contain the string of length and the string of length . Both strings consist of lowercase English letters. The given permutation is guaranteed to be cyclic.
Output
If you cannot obtain a substring of from , print . Otherwise, print the minimum number of shuffle operations needed.