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,维护区间间断个数.然后打 ...
 
随机推荐
- ATL com的dll文件与tlb文件
			
一..tlb文件: 只有COM组件才有tlb文件,普通dll文件没有. 包含内容: 1.它包含了COM类和接口的GUID值,接口的函数声明信息,并不是接口的实现文件.相当于类和接口的头文件. tlb文 ...
 - 关于CString与VARIANT(CComVariant)之间的转化
			
一.VARIANT.CComVariant类与CString是什么: CString是MFC定义的字符串类,VARIANT是COM标准为了使COM组件能够被各种语言使用(vc++.vb.java.py ...
 - freeMarker(二)——模板开发指南之入门
			
学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 模板开发指南-入门 1.模板+数据模型=输出 假设在一个在线商店的应 ...
 - NOIp2018集训test-10-18 (bike day4)
			
这是一套简单题,这几天的考试让bike老爷感觉很绝望,说实话我也确实不知道还能怎么更简单了. 这几天的题换做llj.sxy应该都能轻松AK吧,至少随便考个250+应该不是问题吧,我越来越觉得觉得我跟他 ...
 - JVM体系结构之三:方法区之1
			
一.简介 方法区在JVM中也是一个非常重要的区域,它与堆一样,是被线程共享的区域.在方法区中,存储了每个类的信息(包括类的名称.方法信息.字段信息).静态变量.常量以及编译器编译后的代码等. 方法区( ...
 - mybatis 学习三 mapper xml 配置信息
			
mapper xml 映射文件 1,select 标签 简单是用就这样,其中resultType 代表从这条语句中返回的期望类型的类的完全限定名或别名.也可以使用resultMap对应的id ...
 - cadence spb 16.5 破解过程实例和使用感受_赤松子耶_新浪博客
			
cadence spb 16.5 破解过程实例和使用感受_赤松子耶_新浪博客 Cadence Allegro16.5详细安装具体的步骤 1.下载SPB16.5下来后,点setup.exe,先安装第一项 ...
 - 差一点搞混了Transactional注解
			
今天给我的Srping业务层加如下Service和Transactional注解: @Service @Scope(BeanDefinition.SCOPE_SINGLETON) @Transacti ...
 - TCP/IP的3次握手和4次握手
			
在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接,如图1所示. (1) 第一次握手:建立连接时,客户端A发送SYN包(SYN=j)到服务器B,并进入SYN_SEND状态,等 ...
 - Android的系统结构简述
			
(该图片来自网络) Android系统结构主要分为四层,从上到下依次为,Application层,Application Framework层,lib层,Linux kernel层,下面对这四层进行简 ...