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,维护区间间断个数.然后打 ...
随机推荐
- C++中的前置和后置++
在C++中进行操作符重载的时候,前置++返回的是一个引用. 这就设计到了对于基本变量进行前置操作时候的理解了. 例如,对于int类型变量,在进行前置++的时候,是会将i进行加1,然后返回i的引用.而i ...
- Hibernate - POJO 类和数据库的映射文件*.hbm.xml
POJO 类和关系数据库之间的映射可以用一个XML文档来定义. 通过 POJO 类的数据库映射文件,Hibernate可以理解持久化类和数据表之间的对应关系,也可以理解持久化类属性与数据库表列之间的对 ...
- 【遍历二叉树】01二叉树的前序遍历【Binary Tree Preorder Traversal】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的前序遍历的 ...
- ACM学习历程—Hihocoder 1289 403 Forbidden(字典树 || (离线 && 排序 && 染色))
http://hihocoder.com/problemset/problem/1289 这题是这次微软笔试的第二题,过的人比第三题少一点,这题一眼看过去就是字符串匹配问题,应该可以使用字典树解决.不 ...
- ACM学习历程—UESTC 1222 Sudoku(矩阵)(2015CCPC H)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目大意就是构造一个行列和每个角的2*2都是1234的4*4矩阵. 用dfs暴力搜索,不过需要每一步进 ...
- bzoj 3172: [Tjoi2013]单词 fail树
题目大意: 一篇论文是由许多单词组成,现在想知道每个单词分别在论文中出现多少次. 题解: 我们首先考虑fail指针的含义 如果fail[x] = y,那么我们就知道y作为x的后缀在x中出现了一次 所以 ...
- SYS/BIOS实例分析
SYS/BIOS简介 SYS/BIOS是一个可扩展的实时内核(或者说是操作系统),其提供了许多模块化的APIs(应用程序接口),支持抢占式多线程,硬件抽象,实时分析和配置工具,其设计目的是为了最大限度 ...
- Python-Redis的Hash操作
Redis的hash是一个string类型的field和value的映射表,特别适合用于存储对象,每个hash可以存储40多亿键值对 hset(name, key, value):创建一个name的类 ...
- windows修改远程桌面RDP连接数
windows 2003在默认情况下最多只允许两个用户进行远程终端连接,当达到两个远程桌面连接的到时候,再有人尝试连接,就会提示已经达到最大终端数,无法连上了. 一.windows2003终端连接数修 ...
- 如何从光盘本地安装CentOS 7图形界面(Gnome GUI)
本例中通过在CentOS 7中修改repo文件,直接从光盘或者ISO镜像文件安装Gnome图形界面(Gnome GUI),从而避免耗时从官网或镜像下载. 1.首先确保光盘或者ISO镜像文件正确连接到客 ...