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 tt in a substring s[li..ri]s[li..ri].
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 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 (1≤li≤ri≤n1≤li≤ri≤n) — the arguments for the ii-th query.
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].
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
0
1
0
1
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
4
0
3
3 5 2
aaa
baaab
1 3
1 1
0
0
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 }