B. Segment Occurrences

B. Segment Occurrences
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two strings ss and tt, both consisting only of lowercase Latin letters.

The substring s[l..r]s[l..r] is the string which is obtained by taking characters sl,sl+1,,srsl,sl+1,…,sr without changing the order.

Each of the occurrences of string aa in a string bb is a position ii (1i|b||a|+11≤i≤|b|−|a|+1) such that b[i..i+|a|1]=ab[i..i+|a|−1]=a (|a||a| is the length of string aa).

You are asked qq queries: for the ii-th query you are required to calculate the number of occurrences of string tt in a substring s[li..ri]s[li..ri].

Input

The first line contains three integer numbers nn, mm and qq (1n,m1031≤n,m≤103, 1q1051≤q≤105) — the length of string ss, the length of string ttand the number of queries, respectively.

The second line is a string ss (|s|=n|s|=n), consisting only of lowercase Latin letters.

The third line is a string tt (|t|=m|t|=m), consisting only of lowercase Latin letters.

Each of the next qq lines contains two integer numbers lili and riri (1lirin1≤li≤ri≤n) — the arguments for the ii-th query.

Output

Print qq lines — the ii-th line should contain the answer to the ii-th query, that is the number of occurrences of string tt in a substring s[li..ri]s[li..ri].

Examples
input
Copy
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
output
Copy
0
1
0
1
input
Copy
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
output
Copy
4
0
3
input
Copy
3 5 2
aaa
baaab
1 3
1 1
output
Copy
0
0
Note

In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.

给两个字符串s,t,之后给出s的一个区间,问这个子串中存在多少个子串与t相同

前缀和相减,但是又带有一定技巧.

 1 #include <bits/stdc++.h>
 2 #define ll long long int
 3 #define inf 0x3f3f3f3f
 4 #define N 1000005
 5 using namespace std;
 6 int a[1005];
 7 int n,m,k;
 8 int main(){
 9     cin>>n>>m>>k;
10     string s,ss;
11     cin>>s>>ss;
12     for(int i=0;i<=n-m;i++){
13         bool flag = true;
14         for(int j=0;j<m;j++){
15             if(s[i+j]!=ss[j]){
16                 flag = false;
17                 break;
18             }
19         }
20         if(flag){
21             a[i+1]++;
22         }
23     }
24     for(int i=1;i<=n;i++){
25         a[i] += a[i-1];
26     }
27     for(int i=0;i<k;i++){
28         int x,y;
29         cin>>x>>y;
30         y = y-m+1;
31         if(x>y){
32             cout<<"0"<<endl;
33         }else{
34             cout<<a[y]-a[x-1]<<endl;
35         }
36     }
37     return 0;
38 }

猜你喜欢

转载自www.cnblogs.com/zllwxm123/p/9424965.html