B. Segment Occurrences
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 (1≤i≤|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 ttin a substring s[li..ri]s[li..ri].
Input
The first line contains three integer numbers nn, mm and qq (1≤n,m≤1031≤n,m≤103, 1≤q≤1051≤q≤105) — the length of string ss, the length of string tt and the number of queries, respectively.
The second line is a string s (|s|=n|s|=n), consisting only of lowercase Latin letters.
The third line is a string t (|t|=m|t|=m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers lili and riri (1≤li≤ri≤n1≤li≤ri≤n) — the arguments for the ii-th query.
Output
Print q 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
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
output
0
1
0
1
input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
output
4
0
3
input
3 5 2
aaa
baaab
1 3
1 1
output
0
0
题大意:输入字符串s和t、各自的长度和q个询问,询问由第l到r个字母构成的s的子串中,包含t的个数
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<fstream>
#include<math.h>
#include<stack>
#include<bitset>
using namespace std;
typedef long long ll;
const double eps=0.000000001;
int n,m,q;
char s[1005];
char t[1005];
int nt[1005];
int ans[1005];
void getNext(char*t,int *nt){
int i=0;
nt[0]=-1;
int k=-1;
while(i<strlen(t)){
if(k==-1||t[i]==t[k]){
i++;
k++;
nt[i]=k;
}
else{
k=nt[k];
}
}
}
int KMP(char*a,char*b,int sp){
int i=sp;
int j=0;
while(i<n&&j<m){
if(j==-1||a[i]==b[j]){
i++;
j++;
}
else{
j=nt[j];
}
}
if(j<strlen(b))return -1;
else return i-j;
}
int main(){
while(~scanf("%d%d%d",&n,&m,&q)){
scanf("%s",&s);
scanf("%s",&t);
getNext(t,nt);
memset(ans,0,sizeof(ans));
int p=0;
p=KMP(s,t,0);
//cout<<p<<endl;
while(p!=-1){
for(int j=p+1;j<=n;j++){
ans[j]++;
//cout<<j<<" &&&&"<<endl;
}
p++;
p=KMP(s,t,p);
}
/*
for(int i=1;i<=n;i++){
cout<<ans[i]<<" ";
}
cout<<endl;
*/
int a,b;
for(int i=0;i<q;i++){
scanf("%d%d",&a,&b);
if(b-a+1<m){
printf("0\n");
continue;
}
printf("%d\n",ans[b-m+1]-ans[a-1]);
}
}
return 0;
}