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个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- 【第二周】Java实现英语文章词频统计(改进1)
本周根据杨老师的spec对英语文章词频统计进行了改进 1.需求分析: 对英文文章中的英文单词进行词频统计并按照有大到小的顺序输出, 2.算法思想: (1)构建一个类用于存放英文单词及其出现的次数 cl ...
- (十一)Jmeter另一种调试工具 HTTP Mirror Server
之前我介绍过Jmeter的一种调试工具Debug Sampler,它可以输出Jmeter的变量.属性甚至是系统属性而不用发送真实的请求到服务器.既然这样,那么HTTP Mirror Server又是做 ...
- SSH框架配置
--------------------------------applicationContext.xml-------------------------------- <?xml vers ...
- Geek荣耀大会总结
0.0 首先没有被抽中, 其次可乐真难喝,再次我没有去拍无人机合影,再再次还是很受打击的. 1.0 其实 对geek 和1024大会无感,主要原因 没有三倍加班费的节日在我眼里都不是节日. 上面只是简 ...
- window redis php(必须版本>=5.4) 安装
1.下载redis的win版客户端 下载地址: http://code.google.com/p/servicestack/wiki/RedisWindowsDownload 2.选择32bit,64 ...
- ACM解题之在线翻译 Give Me the Number
Give Me the Number Time Limit: 2 Seconds Memory Limit: 65536 KB ...
- docker+mesos+marathon
前言 (Core) [root@docker-slave ~]# uname -r 3.10.0-229.4.2.el7.x86_64 [root@docker-slave ~]# uname -m ...
- 【.Net】输出的字符靠右对齐
先看下面的这组字符,如果输出来,它是无法靠右对齐: " }; foreach (string s in s1) { string s2 = s; Console.WriteLine(s2); ...
- python的N个小功能(高斯模糊原理及实践)
原理: 二维高斯函数 1) 为了计算权重矩阵,需要设定σ的值.假定σ=1.5,则模糊半径为1的权重矩阵如下: 2) 这9个点的权重总和等于0.4787147,如果只计算 ...
- Python敏感地址扫描和爬取工具
0×01 说明: 为了方便信息安全测评工作,及时收集敏感地址(初衷是爬取api地址),所以写了这么个小工具.两个简单的功能(目录扫描和url地址爬取). 0×02 使用参数: python spide ...