Rats
Time limit0.75sMemory limit256 MB
Given an infinite periodic string A and a set S of strings, find the minimum number of strings from S whose concatenation forms a string B that tiles the line identically to A.
- Level
Hard8 of 10
- Topics
- String, Graph, Shortest path, String matching
- Solved
- No attempts yet
Problem
You are given an infinite line covered with a periodically repeating string . There are infinitely many concatenated copies of the string on the line. The line has no beginning and no end. You are also given a set of strings. You must build a new string by concatenating strings from . The string must satisfy the following conditions:
- After covering a new empty infinite line with infinitely many concatenations of the string , the line must be identical to the line covered with .
- If there are several valid strings or several valid ways to build , choose and its construction that use the fewest strings from .
You may use the same string from several times, but each use counts as a new string. You may concatenate the strings in any order, but you may not change the order of letters inside a string. If no string can be built, print .
Input
- The first line contains the string .
- The second line contains the integer , the number of strings in the set .
- Each of the next lines contains one string from . The -th line contains the string .
- The sum of the lengths of the strings in is at most .
Output
Print one integer: the minimum number of strings from needed to build the string .
Hint
You can use one string "b" and two strings "a" to build = "aba":
- ...baabaabaabaa...
- .....abaabaabaaba...