创建于 2026/07/17 · 更新于 2026/07/17
Colin is learning isomorphism theory recently and tries to apply it to string problems!
Define two strings to be cyclically isomorphic if and only if they satisfy all the following conditions:
For example, strings abcb and babc are cyclically isomorphic since we can cyclically right-shift the first string once to get the second string.
Now you are given non-empty pattern strings , and another non-empty text string .
Colin raises questions; each question is described by an interval . Colin wants to know how many integers satisfy all the following conditions:
Recall that represents the length of string , and represents the substring of from the -th character to the -th character(including both boundaries). represents an empty string, and empty strings are always equal.
The first line contains two integers , representing the number of pattern strings and the number of questions.
For the following lines, each line contains a non-empty string consisting of lowercase letters, representing the -th pattern string. It's guaranteed that .
The following line contains a non-empty string consisting of lowercase letters.
For the following lines, each line contains two integers , describing the -th question.
Output lines; the -th line contains a single integer, representing the answer for the -th question.
5 5
baab
aa
aaab
ab
abaaa
bbaabababb
7 9
1 4
3 7
7 9
5 10
1
0
1
1
0