Educational Codeforces Round 48 (Rated for Div. 2) B 1016B 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 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 (1i|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 (1n,m103 , 1q105 ) — 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 (1lirin ) — 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.

 

 

题目大意:就是给两个字符串s t,然后q次查询,给出 [l, r], 问t出现的次数。

刚开始做这道题感觉就是瞎写,没有好好思考,下面给出官方的思路:首先看一下单纯的做法。q次查询,每次从 i 属于 [l, r-m+1] 然后遍历,看是否和t一样。时间复杂度(q*m*n).

注意到t只能从s的n个位置开始,我们可以预处理t出现的位置,然后前缀和维护出现次数,这样的话,每次查询都是O(1).

 

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <string.h>
 5 #include <stdlib.h>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <algorithm>
12 #include <sstream>
13 #include <stack>
14 using namespace std;
15 typedef long long ll;
16 const int inf = 0x3f3f3f3f;
17 
18 const int N = 1000 + 7; 
19 int pre[N];
20 
21 int main() {
22     //freopen("in.txt", "r", stdin);
23     int n, m, q;
24     scanf("%d%d%d", &n, &m, &q);
25     string s, t;
26     cin >> s >> t;
27     for(int i = 0; i < n - m + 1; i++) {//从s中找t开始的位置 
28         bool flag = true;
29         for(int j = 0; j < m; j++) {
30             if(s[i + j] != t[j])
31                 flag = false;
32         }
33         pre[i+1] = pre[i] + flag;//前缀和 
34     } 
35     for(int i = max(0, n - m + 1); i < n; i++)//上面终止条件,n-m+1 后面的pre还没有值 
36         pre[i+1] = pre[i];
37     for(int i = 0; i < q; i++) {
38         int l, r;
39         scanf("%d%d", &l, &r); 
40         l--, r -= m - 1;//r -= m-1 变成起始位置(本次次数),l-- 变成上次出现次数
41         printf("%d\n", l <= r ? pre[r] - pre[l] : 0);
42     }
43 }

 


 

posted @ 2018-08-06 09:40  Frontierone  阅读(180)  评论(0编辑  收藏  举报