Substring Search
Limits
1s, 512 MB
You are given n strings s1,s2,s3,…,sn and should process m queries. Each query is described by four integers i,l,r,j. It means that you need to find how many times substring si[l…r] occurs in sj.
- Let's define a substring as a contiguous subsegment of a string. For example, "acab" is a substring of "abacaba" (it starts in position 3 and ends in position 6), but "aa" or "d" isn't substrings of this string. So the substring of the string a from position l to position r is a[l…r]=alal+1…ar.
Input
The first line of the input contains an integer n(1≤n≤105)— number of strings.
Each of the following n lines contains a string si of lowercase Latin letters. It is guaranteed that sum of all the strings is no more than 105.
The following line contains an integer m(1≤m≤105)— number of queries.
Each of the following m lines contains four integers i,l,r,j(1≤i,j≤n,1≤l≤r≤∣si∣) as mentioned in the statement. Note that, all the indices are 1-based and i might be equal to j.
Output
For each query, print a single integer — the answer to the problem.
Sample
Input | Output |
---|
2
abc
bca
2
1 2 3 2
1 1 2 2
| 1
0
|
In the first query, i=1,l=2,r=3,j=2. 1ststring s1is abc, 2nd string s2 is bca, and s1[2…3]=bc. Now, for the first query, we need to find the number of times bc occurs in s2, which occurs only in s2[1…2]. Hence the answer to the first query is 1. |