Educational Codeforces Round 48 (Rated for Div. 2) B 1016B Segment Occurrences (前缀和)
2 seconds
256 megabytes
standard input
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 (1≤i≤|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] .
The first line contains three integer numbers n , m and q (1≤n,m≤103 , 1≤q≤105 ) — 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 (1≤li≤ri≤n ) — the arguments for the i -th query.
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] .
- 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,然后q次查询,给出 [l, r], 问t出现的次数。
刚开始做这道题感觉就是瞎写,没有好好思考,下面给出官方的思路:首先看一下单纯的做法。q次查询,每次从 i 属于 [l, r-m+1] 然后遍历,看是否和t一样。时间复杂度(q*m*n).
注意到t只能从s的n个位置开始,我们可以预处理t出现的位置,然后前缀和维护出现次数,这样的话,每次查询都是O(1).
- #include <iostream>
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #include <stdlib.h>
- #include <string>
- #include <vector>
- #include <set>
- #include <map>
- #include <queue>
- #include <algorithm>
- #include <sstream>
- #include <stack>
- using namespace std;
- typedef long long ll;
- const int inf = 0x3f3f3f3f;
- const int N = + ;
- int pre[N];
- int main() {
- //freopen("in.txt", "r", stdin);
- int n, m, q;
- scanf("%d%d%d", &n, &m, &q);
- string s, t;
- cin >> s >> t;
- for(int i = ; i < n - m + ; i++) {//从s中找t开始的位置
- bool flag = true;
- for(int j = ; j < m; j++) {
- if(s[i + j] != t[j])
- flag = false;
- }
- pre[i+] = pre[i] + flag;//前缀和
- }
- for(int i = max(, n - m + ); i < n; i++)//上面终止条件,n-m+1 后面的pre还没有值
- pre[i+] = pre[i];
- for(int i = ; i < q; i++) {
- int l, r;
- scanf("%d%d", &l, &r);
- l--, r -= m - ;//r -= m-1 变成起始位置(本次次数),l-- 变成上次出现次数
- printf("%d\n", l <= r ? pre[r] - pre[l] : );
- }
- }
Educational Codeforces Round 48 (Rated for Div. 2) B 1016B Segment Occurrences (前缀和)的更多相关文章
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
- Educational Codeforces Round 48 (Rated for Div. 2)
http://codeforces.com/contest/1016 A. 没想到这个也会TLE,太粗心了 B. 暴力就好了,多情况讨论又出错... 思路跟我一样的解法 为什么我做了那么多讨论,原 ...
- Educational Codeforces Round 48 (Rated for Div. 2)异或思维
题:https://codeforces.com/contest/1016/problem/D 题意:有一个 n * m 的矩阵, 现在给你 n 个数, 第 i 个数 a[ i ] 代表 i 这一行所 ...
- Educational Codeforces Round 48 (Rated for Div. 2)——A. Death Note ##
A. Death Note time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Educational Codeforces Round 48 (Rated for Div. 2)G. Appropriate Team
题意:求满足条件的(i,j)对数:\(gcd(v,a_i)=x,lcm(v,a_j)=y\) 题解:\(x|a_i,a_j|y\),\(x|y\),考虑质因子p,假设a_i中p次数为a,x中次数为b, ...
- Educational Codeforces Round 48 (Rated for Div. 2) D 1016D Vasya And The Matrix (构造)
D. Vasya And The Matrix time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 【Educational Codeforces Round 48 (Rated for Div. 2) C】 Vasya And The Mushrooms
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然在没有一直往右走然后走到头再往上走一格再往左走到头之前. 肯定是一直在蛇形走位.. 这个蛇形走位的答案贡献可以预处理出来.很容易 ...
- 【Educational Codeforces Round 48 (Rated for Div. 2) D】Vasya And The Matrix
[链接] 我是链接,点我呀:) [题意] 告诉你每一行.每一列的异或和. 让你求出一个符合要求的原矩阵. [题解] 显然应该有 a1^a2^....^an = b1^b2^....^bn 也即两边同时 ...
- Educational Codeforces Round 61 (Rated for Div. 2)-C. Painting the Fence 前缀和优化
题意就是给出多个区间,要求去掉两个区间,使得剩下的区间覆盖范围最大. 当然比赛的时候还是没能做出来,不得不佩服大佬的各种姿势. 当时我想的是用线段树维护区间和,然后用单点判0,维护区间间断个数.然后打 ...
随机推荐
- FileInputStream 把文件作为字节流进行读操作
//把文件作为字节流进行读操作 FileInputStream in = new FileInputStream(filename);//FileInputStream具体实现了在文件上读取数据
- 2018.5.29 从layout 到 PCBA
1 Gerber 这个网上有现成的教程:(不要写网上能找到的资料-敏捷开发) AD 导出Gerber :https://jingyan.baidu.com/article/3c48dd3494181c ...
- BEC listen and translation exercise 41
Its advantages are that it can be used for outside activities So my recommendation I'm afraid would ...
- django学习笔记(四)表单
1.若用户刷新一个包含POST表单的页面,那么请求将会重新发送造成重复. 这通常会造成非期望的结果,比如说重复的数据库记录.如果用户在POST表单之后被重定向至另外的页面,就不会造成重复的请求了.我们 ...
- OpenCV——饱和度调整
参考: 闲人阿发伯的博客 // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED ...
- bzoj 4822~4824 CQOI2017题解
老C的任务 题目大意: 维护一个二维平面,初始给出一些点及其权.多次询问某个矩形内的权和. n,m <= 100000 题解: 签到题. CDQ水一水. #include <cstdio& ...
- C# Hashtable赋值给另一个Hashtable时
c#中想将一个hashtable的元素全部给另一个hashbale时, 使用迭代一个一个元素赋值 如: ammus.Clear(); IDictionaryEnumerator ie = _temp. ...
- python文件操作 seek(),tell()
seek():移动文件读取指针到指定位置 tell():返回文件读取指针的位置 seek()的三种模式: (1)f.seek(p,0) 移动当文件第p个字节处,绝对位置 (2)f.seek(p,1) ...
- MyBatis总结(1)
MyBatis前身是ibatis,是一个数据持久层框架.封装优化了普通JDBC过程, 如数据库连接的创建.设置SQL语句参数.执行SQL语句.事务.结果映射以及资源释放等. MyBatis是一个支持普 ...
- 使用TRY CATCH进行SQL Server异常处理
TRY...CATCH是Sql Server 2005/2008令人印象深刻的新特性.提高了开发人员异常处理能力.没有理由不尝试一下Try.. Catch功能. * TRY 块 - 包含可能 ...