Hongjun Likes Strings
Time limit2sMemory limit512 MB
For each of up to 100000 queries, find the shortest substring of a fixed string S that contains both given short patterns A and B, allowing overlap.
- Level
Hard8 of 10
- Topics
- String, String matching, Prefix sum, Binary search
- Solved
- No attempts yet
Problem
Hongjun likes strings, so he keeps making up problems about them.
Here is one of them. Given a string and two strings and , find the shortest contiguous substring of that contains both and as substrings. The occurrence of and the occurrence of are allowed to overlap.
Hongjun is smart, so he solved that at once. Then he thought of a harder version, in which the two strings and arrive as separate questions. He could not find a fast way to answer them, so he decided that short and would let him solve it quickly.
Help Hongjun and write a program that answers every question.
Input
The first line contains a string of length at most .
The second line contains the number of questions , an integer with .
Each of the next lines contains two strings and separated by a space. The length of and the length of are between and .
, , and consist of lowercase English letters only.
Output
Print the answers to the questions, one per line. On each line print the minimum length of a contiguous substring of that contains both and as substrings. If no such contiguous substring exists, print .