You are given two strings s and t
, both consisting only of lowercase Latin letters.
The substring s[l..r]
is the string which is obtained by taking characters sl,sl+1,…,sr
without changing the order.
Each of the occurrences of string a
in a string b is a position i (1≤i≤|b|−|a|+1) such that b[i..i+|a|−1]=a (|a| is the length of string a
).
You are asked q
queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[li..ri]
.
Input
The first line contains three integer numbers n
, m and q (1≤n,m≤103, 1≤q≤105) — the length of string s, the length of string t
and the number of queries, respectively.
The second line is a string s
(|s|=n
), consisting only of lowercase Latin letters.
The third line is a string t
(|t|=m
), consisting only of lowercase Latin letters.
Each of the next q
lines contains two integer numbers li and ri (1≤li≤ri≤n) — the arguments for the i
-th query.
Output
Print q
lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring 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.
#include<bits/stdc++.h>
#define lowbit(x) (x&(-x))
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const int maxn=1e3+10;
char str[maxn],s[maxn];
struct node{
char ch;
int val;
node(){}
node(char _ch,int _val):ch(_ch),val(_val){}
};
node stk[maxn];
int top;
int f[maxn];
int tr[maxn];
int len1,len2;
void update(int x,int v){
// printf("x:%d\n",x);
while(x<=len1){
tr[x]+=v;
x+=lowbit(x);
}
//rep(i,1,len2+1)printf("i:%d %d\n",i,tr[i]);
}
int get_sum(int x){
int ans=0;
while(x>0){
ans+=tr[x];
x-=lowbit(x);
}
return ans;
}
/*
自己匹配自己的时候,谁是模版串?
[0,i-1]
*/
int fail[maxn];
void getFail(char* P,int len){
fail[0]=fail[1]=0;
for(int i=1;i<len;i++){
//目的就是找以i号字符结尾的串 的后缀和模版串的前缀的最大长度
int j=fail[i];//因为“模版串"没有i号字符,所以i肯定失配。fail[i]的含义就是以i-1号字符结尾的串中 1.模版的最大的前缀和文本后缀的匹配成功的长度,2.也就是状态机现在的下标 ,是正在匹配的那个模版的J号字符(从0开始)
while(j&&P[i]!=P[j])j=fail[j];//没有对应的J;寻找和P[i]匹配的位置J
fail[i+1]=(P[i]==P[j]?j+1:0);//如果找到了,长度就是J+1,没有找到长度就是0,
}
}
int main()
{
int m;
scanf("%d %d %d",&len1,&len2,&m);
scanf(" %s",str);
scanf(" %s",s);
getFail(s,len2);
int j=0;
for(int i=0;i<len1;i++){
while(j&&s[j]!=str[i])j=fail[j];
if(s[j]==str[i])++j;
if(j==len2){
int x=i-len2+1;
update(x+1,1);
// printf("i:%d %c\n",x+1,str[x]);
}
}
rep(i,0,m){
int l,r;
scanf("%d %d",&l,&r);
//printf("l:%d r:%d\n",l,r);
if(r-l+1<len2){
printf("0\n");
}
else{
printf("%d\n",get_sum(r-len2+1)-get_sum(l-1));
}
}
return 0;
}