B. Segment Occurrences
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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] .

Input

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≤lirin ) — the arguments for the i -th query.

Output

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] .

Examples
Input

Copy
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output

Copy
0
1
0
1
Input

Copy
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output

Copy
4
0
3
Input

Copy
3 5 2
aaa
baaab
1 3
1 1
Output

Copy
0
0
Note

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 (前缀和)的更多相关文章

  1. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

  2. Educational Codeforces Round 48 (Rated for Div. 2)

    http://codeforces.com/contest/1016 A. 没想到这个也会TLE,太粗心了 B. 暴力就好了,多情况讨论又出错... 思路跟我一样的解法   为什么我做了那么多讨论,原 ...

  3. Educational Codeforces Round 48 (Rated for Div. 2)异或思维

    题:https://codeforces.com/contest/1016/problem/D 题意:有一个 n * m 的矩阵, 现在给你 n 个数, 第 i 个数 a[ i ] 代表 i 这一行所 ...

  4. 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 ...

  5. 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, ...

  6. 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 ...

  7. 【Educational Codeforces Round 48 (Rated for Div. 2) C】 Vasya And The Mushrooms

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然在没有一直往右走然后走到头再往上走一格再往左走到头之前. 肯定是一直在蛇形走位.. 这个蛇形走位的答案贡献可以预处理出来.很容易 ...

  8. 【Educational Codeforces Round 48 (Rated for Div. 2) D】Vasya And The Matrix

    [链接] 我是链接,点我呀:) [题意] 告诉你每一行.每一列的异或和. 让你求出一个符合要求的原矩阵. [题解] 显然应该有 a1^a2^....^an = b1^b2^....^bn 也即两边同时 ...

  9. Educational Codeforces Round 61 (Rated for Div. 2)-C. Painting the Fence 前缀和优化

    题意就是给出多个区间,要求去掉两个区间,使得剩下的区间覆盖范围最大. 当然比赛的时候还是没能做出来,不得不佩服大佬的各种姿势. 当时我想的是用线段树维护区间和,然后用单点判0,维护区间间断个数.然后打 ...

随机推荐

  1. Execution Context(EC) in ECMAScript

    参考资料 执行环境,作用域理解 深入理解JavaScript系列(2):揭秘命名函数表达式 深入理解JavaScript系列(12):变量对象(Variable Object) 深入理解JavaScr ...

  2. 2.4 AppDelegate 的 3 个生命周期

    Classed/AppDelegate.cpp 文件内容如下: #include "cocos2d.h" #include "CCEGLView.h" #inc ...

  3. 【leetcode刷题笔记】Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  4. NO3:步履蹒跚-完成第一章节学习

    第一章小记: 每个C程序都要求有一个main()函数(多于一个main()函数是不合法的(已犯错:在VS 2010一个项目里两个C文件都有main函数,不能编译通过,必须删除一个文件,永记)).mai ...

  5. uoj problem 21 缩进优化

    题目: 小O是一个热爱短代码的选手.在缩代码方面,他是一位身经百战的老手.世界各地的OJ上,很多题的最短解答排行榜都有他的身影.这令他感到十分愉悦. 最近,他突然发现,很多时候自己的程序明明看起来比别 ...

  6. 一步一步学RenderMonkey

    http://blog.csdn.net/tianhai110/article/details/5668832 转载请注明出处:http://blog.csdn.net/tianhai110/ 网上一 ...

  7. CentOS6 下Samba服务器的安装与配置

    原地址:http://www.cnblogs.com/mchina/archive/2012/12/18/2816717.html 一.简介 Samba是一个能让Linux系统应用Microsoft网 ...

  8. IDEA 热部署 + 下载jar包放到maven中

    IDEA 热部署: 1 :  POM中加入devtools的依赖,就可以实现热部署 <dependency> <groupId>org.springframework.boot ...

  9. eclipse tomcat 无法加载导入的web项目,There are no resources that can be added or removed from the server. .

    应该是项目自己的setting文件夹下的描述信息和.project文件的描述信息,不能适用于这个eclipse和tomcat. 解决方法: 1,找相同类型的工程(tomcat能引用的)2,把新建项目里 ...

  10. 开启struts2自带的开发模式常量

    在以前的开发中,当修改一些配置时总是不能及时地更新到服务器,我们总会重新部署或重启来更新改变的内容,在struts2中可以通过一个常量来达到此目的.即在struts.xml中的<struts&g ...