Description

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≤li≤ri≤n) — 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].

Sample Input
Input

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

Output

0
1
0
1

Input

15 2 3
abacabadabacaba
ba
1 15
3 4
2 14

Output

4
0
3

Input

3 5 2
aaa
baaab
1 3
1 1

Output

0
0

Hint

In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.

题目意思:给出一条母串,给出一条子串,查询母串的某一个区间,问该区间有多少条子串。

解题思路:我是用string中的find来做的,find可以有两个参数,一个是子串,一个是在母串中的位置,也就是开始查询的位置,先利用find找到所有子串的起始位置,相当于打了一张表,之后查询区间,就是访问该区间中合法的子串起始位置的数量,注意子串的长度+起始位置不能越界!

 #include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,m,q,i,j,l,r,len;
int counts;
int vis[];
string s1,s2;
cin>>n>>m>>q;
cin>>s1>>s2;
len=s2.size();
memset(vis,,sizeof(vis));
string::size_type pos=;
while((pos=s1.find(s2,pos))!=string::npos)
{
vis[pos+]=pos+;
pos++;
}///打表标记母串中出现子串的位置
for(i=;i<=q;i++)
{
counts=;
scanf("%d%d",&l,&r);
for(j=l;j<=r;j++)
{
if(vis[j]!=&&vis[j]+len-<=r)///查询区间内出现子串并且不会越界
{
counts++;
}
}
printf("%d\n",counts);
}
return ;
}

Segment Occurrences(string find函数)的更多相关文章

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

  2. java String.split()函数的用法分析

    java String.split()函数的用法分析 栏目:Java基础 作者:admin 日期:2015-04-06 评论:0 点击: 3,195 次 在java.lang包中有String.spl ...

  3. string.capwords()函数

    string.capwords()函数 string.capwords()函数,有需要的朋友可以参考下. 代码 : import syssys.path.append("C:/Python2 ...

  4. public static void main(String[] args){}函数诠释

    public static void main(String[] args){}函数诠释 主函数的一般写法如下: public static void main(String[] args){-} 下 ...

  5. Js对象转String的函数 和 JSON转String

    js对象转string的函数 function obj2str(o){ var r = []; if(typeof o =="string") return "" ...

  6. String.Split()函数

    我们在上次学习到了 String.Join函数(http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx),当中用到了String.SPl ...

  7. java中string.trim()函数的使用

    java中string.trim()函数的的作用是去掉字符串开头和结尾的空格,防止不必要的空格导致的错误. public static void main(String arg[]){ String ...

  8. String.Split()函数 多种使用实例

    我们在上次学习到了 String.Join函数(http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx),其中用到了String.SPl ...

  9. string的函数的学习

    1.string类型的构造函数和对象的定义 string s3 : 把string s2 拷贝的 s3 string s4 : 把数组首地址或者字符串首地址strArr 从0开始截取到第n个字母 st ...

  10. Torch-RNN运行过程中的坑 [2](Lua的string sub函数,读取中文失败,乱码?)

    0.踩坑背景 仍然是torch-rnn/LanguageModel.lua文件中的一些问题,仍然是这个狗血的LM:encode_string函数: function LM:encode_string( ...

随机推荐

  1. oracle启动的三个阶段

    startup nomount 时,数据库状态为 started; alter database mount 时, 状态为 mounted; alter database open 时,状态为 ope ...

  2. input输入的数据只允许整数和浮点型数据

    //第一步:引入jquery //第二步:input输入框绑定该函数 例如:<input type="text" name="price" onInput ...

  3. Jersey常用注解解释 @DET、@PUT、@POST 、@DELETE等

    uri : ... /resource/{id} public voide method(@PathParam("id") String userId){} uri :  .../ ...

  4. Session共享的四种方法

    1. 基于NFS的Session共享 NFS是Net FileSystem的简称,最早由Sun公司为解决Unix网络主机间的目录共享而研发. 这个方案实现最为简单,无需做过多的二次开发,仅需将共享目录 ...

  5. LCA树上倍增

    LCA就是最近公共祖先,比如 节点10和11的LCA就是8,9和3的LCA就是3. 我们这里讲一下用树上倍增来求LCA. 大家都可以写出暴力解法,两个节点依次一步一步往上爬,直到爬到了相同的一个节点. ...

  6. PHP批量抓取远程网页图片并存到本地实现方法和源码

    做为一个仿站工作者,当遇到网站有版权时甚至加密的时候,WEBZIP也熄火,怎么扣取网页上的图片和背景图片呢.有时候,可能会想到用火狐,这款浏览器好像一个强大的BUG,文章有版权,屏蔽右键,火狐丝毫也不 ...

  7. Z30云台PC控制问题

    https://developer.dji.com/cn/onboard-sdk/ 我们的需求: 完成PC程序控制Z30相机云台 关键问题: 前提:遥控器控制飞机+图像观看+调焦 1 PC能够接收到图 ...

  8. 如何使用bootstrap框架

    Bootstrap是前端工程师比较常用的框架.插件,根据它的定义,Bootstrap就是用于前端开发的一个模板,就是别人做好了我们直接可以搬过来直接使用或者根据自己需要略加修改设计自己的页面效果的成品 ...

  9. windows系统下Disconf web安装-分布式配置管理平台

    文章参考自 http://blog.csdn.net/syc001/article/details/78128117 https://www.cnblogs.com/mrluo735/p/632271 ...

  10. vue 如何在循环中绑定v-model

    vue 如何在循环中绑定v-model 我现在有这么一个需求,页面上有多项输入框,但是具体有多少项,我也不知道,它是通过"新增一项"按钮点击事件,点击一下,就新增一项:如下图这个样 ...