B. Segment Occurrences
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou are given two strings t, both consisting only of lowercase Latin letters.
The substring sl,sl+1,…,sr without changing the order.
Each of the occurrences of string a).
You are asked s[li..ri].
Input
The first line contains three integer numbers tand the number of queries, respectively.
The second line is a string |s|=n), consisting only of lowercase Latin letters.
The third line is a string |t|=m), consisting only of lowercase Latin letters.
Each of the next i-th query.
Output
Print 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 }