Segment Occurrences(string find函数)
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函数)的更多相关文章
- 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 ...
- java String.split()函数的用法分析
java String.split()函数的用法分析 栏目:Java基础 作者:admin 日期:2015-04-06 评论:0 点击: 3,195 次 在java.lang包中有String.spl ...
- string.capwords()函数
string.capwords()函数 string.capwords()函数,有需要的朋友可以参考下. 代码 : import syssys.path.append("C:/Python2 ...
- public static void main(String[] args){}函数诠释
public static void main(String[] args){}函数诠释 主函数的一般写法如下: public static void main(String[] args){-} 下 ...
- Js对象转String的函数 和 JSON转String
js对象转string的函数 function obj2str(o){ var r = []; if(typeof o =="string") return "" ...
- String.Split()函数
我们在上次学习到了 String.Join函数(http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx),当中用到了String.SPl ...
- java中string.trim()函数的使用
java中string.trim()函数的的作用是去掉字符串开头和结尾的空格,防止不必要的空格导致的错误. public static void main(String arg[]){ String ...
- String.Split()函数 多种使用实例
我们在上次学习到了 String.Join函数(http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx),其中用到了String.SPl ...
- string的函数的学习
1.string类型的构造函数和对象的定义 string s3 : 把string s2 拷贝的 s3 string s4 : 把数组首地址或者字符串首地址strArr 从0开始截取到第n个字母 st ...
- Torch-RNN运行过程中的坑 [2](Lua的string sub函数,读取中文失败,乱码?)
0.踩坑背景 仍然是torch-rnn/LanguageModel.lua文件中的一些问题,仍然是这个狗血的LM:encode_string函数: function LM:encode_string( ...
随机推荐
- Matplotlib:plt.text()给图形添加数据标签
1.数据可视化呈现的最基础图形就是:柱状图.水平条形图.折线图等等: 在python的matplotlib库中分别可用bar.barh.plot函数来构建它们,再使用xticks与yticks(设置坐 ...
- centos7系统下hostname解析
hostnamectl 是在 centos7以上版本 中新增加的命令,它是用来修改主机名称的,centos7 修改主机名称会比以往容易许多. 首先了解下这个命令 # hostnamectl -h -h ...
- 八皇后问题的Python实现和C#实现
看到八皇后问题的解决思路, 感觉很喜欢. 我用C#实现的版本之前贴在了百度百科上(https://baike.baidu.com/item/%E5%85%AB%E7%9A%87%E5%90%8E%E9 ...
- 17秋 软件工程 第六次作业 Beta冲刺 Scrum1
17秋 软件工程 第六次作业 Beta冲刺 Scrum1 各个成员冲刺期间完成的任务 重新梳理项目架构与当前进展,并且对我们的Alpha版本项目进行完整测试,将测试过程中发现的问题列入Github i ...
- docker in docker 出现 libltdl.so.7 问题
# docker ps -adocker: error while loading shared libraries: libltdl.so.7: cannot open shared object ...
- M100 (1) 运行
软件环境设置指南 本指南详细介绍了使用Onboard SDK所需的软件环境. https://developer.dji.com/onboard-sdk/documentation/developme ...
- HttpMessageNotReadableException(一)
1.今天移动端调用接口时候出现下面异常 org.springframework.http.converter.HttpMessageNotReadableException: JSON parse e ...
- MP实战系列(五)之封装方法讲解
mybatis plus封装的方法怎么用?以及它们对应的sql是那些sql?及其什么情况用? 这些需要说下,以下我将会将我常用的说下,不是常用的,可能提以下或者不提. 根据主键查询 UserEntit ...
- 【Codeforces 464D】World of Darkraft - 2
Codeforces 464 D 首先我们知道这K个装备是互不干扰的,就是说如果一个装备升级了或者卖掉了,不会对其它装备的挣到的钱产生任何影响.所以我们就考虑单独处理某一个装备挣到的钱. 那么就设\( ...
- Android学习之基础知识八—Android广播机制
一.广播机制简介 Android提供了一套完整的API,允许应用程序自由的发送和接受广播,发送广播借助于我们之前学过的:Intent,而接收广播需要借助于广播接收器(Broadcast Receive ...