博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Educational Codeforces Round 48 (Rated for Div. 2) B 1016B Segment Occurrences (前缀和)
阅读量:6379 次
发布时间:2019-06-23

本文共 2766 字,大约阅读时间需要 9 分钟。

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 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
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 }

 

 

 

转载于:https://www.cnblogs.com/ACMerszl/p/9572937.html

你可能感兴趣的文章
二叉树(一)
查看>>
函数的递归
查看>>
JavaScript之将JS代码放在什么位置最合适
查看>>
【“零起点”--百度地图手机SDK】如何使用离线地图?
查看>>
深拷贝与浅拷贝复习
查看>>
各种参数的响应时间
查看>>
SQL Server 索引重建脚本
查看>>
23:LVS客户端配置脚本案例
查看>>
Android播放本地视频
查看>>
80. Hibernate 5.0命名策略使用naming-strategy 不起作用【从零开始学Spring Boot】
查看>>
not found command:svn
查看>>
addEventListener和attachEvent小结
查看>>
IPHONE 开发 4 -- 深入理解iPhone OS/SDK与Objective-C 2.0
查看>>
在windows平台下获取精确经过时间
查看>>
SQL Server的还原(2)——STOPAT
查看>>
IOS(http几种请求)
查看>>
【转】域名解析相关概念
查看>>
hdu 1232:畅通工程(数据结构,树,并查集)
查看>>
在.NET中实现彩色光标/动画光标和自定义光标[转]
查看>>
freemarker错误七
查看>>