Mock Grading
Time limit8sMemory limit1024 MB
Find the first question after which the summed bonuses of remembered strings matching the read answer suffix reach A, or report Fail.
- Level
Medium7 of 10
- Topics
- String matching, Trie, Prefix sum, Binary search
- Solved
- No attempts yet
Statement
The POSTECH multiple-choice exam has questions, and each question has 26 choices labeled with the uppercase letters A through Z. Writing the correct letters in order from question 1 to question gives the answer string of length .
students took the exam and submitted answers to every question. They do not remember every answer they submitted. Student remembers consecutive answers, and writing them in order gives the string .
After the exam, students compare their remembered strings with the published answer string to grade themselves. The letters of are read one by one from the front, and the confidence of the students rises by the following rules.
- Every student starts with confidence 0.
- If the last correct letters read so far match in order, the confidence of student rises by a positive integer . In other words, the suffix of length of the string read so far must match in order.
- While fewer than correct letters have been read, the confidence of student neither rises nor falls. Increases are independent of each other. So triggers a rise whenever the most recent letters match it, regardless of whether also appears earlier in or matches some other .
When the confidence of any of the students reaches or more, the self-grading is considered valid. Find the number of the question after which a student first has confidence or more.
Input
The first line contains , the number of questions in this year's quiz, , the number of students who took the exam, and the integer . (, , )
The next line contains the answer string of this year's exam. consists only of uppercase letters.
Each of the next lines contains , , and , separated by spaces. (, ) consists only of uppercase letters. The sum of all is at most 1000000. Different students may have the same .
Output
Print the number of the question at which the confidence of some student first reaches or more. If no student reaches or more after all answers are read, print Fail.