The ABCD Murderer
Time limit2sMemory limit512 MB
Find the fewest word occurrences needed to cover a target text exactly when cut-outs may overlap on matching text, or report -1 if impossible.
- Level
Hard8 of 10
- Topics
- String matching, Array, Dynamic programming, Queue
- Solved
- No attempts yet
Problem
Oscar enjoys watching crime movies very much. He admires all those villains for how creative some of them can get. He would like to show off some of his creativity, too. Sadly, he is quite inexperienced and cannot come up with his own kind of trick. So he would like to get inspired by some old trick. He always loved watching criminals cut out letters from newspapers and put them together to form a ransom text. However, Oscar is not a total rip off, so he came up with his own variant of this trick. He feels that putting the text together letter by letter is simply too boring and time consuming. So he decided to create his ransom note by cutting out whole words.
Oscar bought several mainstream newspapers, so he has a practically unlimited source of paper to cut the words from. He can cut out any particular word as many times as he wishes. However, he is still limited by the set of words which appear in the newspapers. The trouble is that some words are just not used in the newspapers at all. To make his job a bit easier, he decided to erase all punctuation and all whitespace characters from the ransom note and ignore the case of characters. He also allows the cut-out words to overlap, as far as the overlapping parts contain the same text. Oscar now wonders how many words at minimum he has to cut out from the newspapers to put together the ransom note.
Input
The first input line contains an integer (), the number of words found in the newspapers.
The next line contains the text of the ransom note. The text is not empty and consists of lowercase English letters only. It is at most characters long.
Each of the next lines contains one word appearing in the newspapers, in lowercase English letters. None of these words is empty or longer than the text of the ransom note. All words appearing in the newspapers are listed in the input at least once. Sum of lengths of all words is not greater than .
Output
Output the minimal number of words Oscar has to cut out from the newspapers to compose his ransom note. If it is not possible to compose the ransom note, print . Each word has to be counted as many times as it is physically cut out from a newspaper.