CodeForces - 1016B
You are given two strings ss and tt, both consisting only of lowercase Latin letters.
The substring s[l..r]s[l..r] is the string which is obtained by taking characters sl,sl+1,…,srsl,sl+1,…,sr without changing the order.
Each of the occurrences of string aa in a string bb is a position ii (1≤i≤|b|−|a|+11≤i≤|b|−|a|+1) such that b[i..i+|a|−1]=ab[i..i+|a|−1]=a (|a||a| is the length of string aa).
You are asked qq queries: for the ii-th query you are required to calculate the number of occurrences of string tt in a substring s[li..ri]s[li..ri].
Input
The first line contains three integer numbers nn, mm and qq (1≤n,m≤1031≤n,m≤103, 1≤q≤1051≤q≤105) — the length of string ss, the length of string tt and the number of queries, respectively.
The second line is a string ss (|s|=n|s|=n), consisting only of lowercase Latin letters.
The third line is a string tt (|t|=m|t|=m), consisting only of lowercase Latin letters.
Each of the next qq lines contains two integer numbers lili and riri (1≤li≤ri≤n1≤li≤ri≤n) — the arguments for the ii-th query.
Output
Print qq lines — the ii-th line should contain the answer to the ii-th query, that is the number of occurrences of string tt in a substring s[li..ri]s[li..ri].
Examples
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
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
刚开始不会做,是因为只想到了用前缀和,没想到应该怎么去用,根据题意,只有当string t 全部在在所查区间的时候,答案才加一,
所以说如果从查询区间的头部向后遍历的话,不但要知道从哪个字母开始,还要判断字符串t何时结束,前缀和不太好维护。
因此,我们从查询区间的尾部向前遍历,只要找到字符串t结束的地方,只需要判断字符串t的开头是否在查询区间之内即可,这样的前缀和容易维护。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
const double e=exp();
const int N = ; char con[],sub[];
int check[];
int main()
{
int i,p,j,n,m,q;
int flag,a,b,ans;
scanf("%d%d%d",&n,&m,&q);
scanf(" %s",con+);
scanf(" %s",sub+); for(i=;i<=n-m+;i++)
{
p=;
flag=;
for(j=i;j<=i+m;j++)
{
if(p>m)
{
flag=;
break;
}
if(con[j]!=sub[p])
break;
p++;
}
if(flag==)
check[j-]=;
}
for(i=;i<=q;i++)
{
ans=;
scanf("%d%d",&a,&b);
for(j=a;j<=b;j++)
{
if(check[j]==&&j-(m-)>=a)
ans++;
}
printf("%d\n",ans);
}
return ;
}
CodeForces - 1016B的更多相关文章
- 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 stand ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- NFS 它的目的就是想让不同的机器、不同的作业系统可以彼此分享个别的档案啦
NFS即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源.在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件, ...
- 0302思考&回答
看完这两个网页,我们可以看出it行业始终是一门热门行业,在现在这个人潮汹涌的人才市场,面对严峻的就业形势,我们应该拿什么去参见招聘?人多而工作职位有限,这警醒我们必须拥有一技之长,否则则会被淘汰.如果 ...
- c99标准的restrict关键字
参考自restrict restrict解释 restrict关键字出现于C99标准,wiki上的解释restrict from wiki. In the C programming language ...
- 2nd 简单四则运算更新
简单四则运算更新 功能:由随机数决定出题为10个以内的数字,并确定是否出现括号(仅限一对),顺序输出表达式,并用栈的方式进行计算,判断正误.其他功能有待进一步实现. 头文件 #include < ...
- Node.js系列——(4)优势及场景
背景 之前几篇系列文章简单介绍了node.js的安装配置及基本操作: Node.js系列--(1)安装配置与基本使用 Node.js系列--(2)发起get/post请求 Node.js系列--(3) ...
- tooltips & click copy
tooltips & click copy shit antd & tooltips & click copy https://codesandbox.io/s/zx4wo7y ...
- windows下面安装redis
一.下载windows版本的Redis 链接:https://pan.baidu.com/s/1i6X2klv 密码:j4pi 二.安装Redis 这里下载的是Redis-x64-3.2.100版本, ...
- MethodHandle
JDK7为间接调用方法引入新的API,在java.lang.invoke包下,可以看作为反射的升级版,但它不像反射API那样显得冗长.繁重 主要的类 MethodHandle 方法句柄.对可直接执行的 ...
- BZOJ5071 小A的数字
设f[i]为选择i对i-1和i+1所带来的贡献.则有f[i-1]+f[i+1]+a[i]-2f[i]=b[i],特殊地,f[2]+a[1]=b[1],f[n-1]+a[n]-2f[n]=b[n].可以 ...
- servlet中doGet()和doPost()的区别
1.生成方式 get方法有四种: ①直接在URL地址栏中输入URL ②网页中的超链接 ③form中method为get ④form中method为空时,默认是get提交 post只知道有一种:form ...