Time limit per test: 1.0 seconds
Memory limit: 256 megabytes
子串的定义是在一个字符串中连续出现的一段字符。这里,我们使用 s[l…r] 来表示 s 字符串从 l 到 r (闭区间)的子串。在本题中,字符串下标从 0 开始。显然,对于长度为 n 的字符串共有 n(n+1)2 个子串。
对于一个给定的字符串 s ,唐纳德给出 q 次询问,第 i 次询问包括三个参数 li,ri,zi ,问在 s[li…ri] 的所有子串中共有多少个恰好为 zi 。
Input
输入具有如下形式:
sql1 r1 z1l2 r2 z2⋮lq rq zq
第一行一个字符串 s 。
第二行一个整数 q 。
接下来每行:首先两个整数 li,ri ( 0≤li≤ri<|s| ),然后是一个非空字符串 zi 。整数和整数,整数和字符串间以单空格隔开。
字符串中只会出现 26 个小写英文字母。
数据规模约定:
- 对于 Easy 档: 1≤|s|≤100,q≤∑|zi|≤100 。
- 对于 Hard 档: 1≤|s|≤105,q≤∑|zi|≤105 。
Output
对于每次询问,输出一个整数,表示答案。
Examples
thisisagarbagecompetitionhahaha 5 0 30 a 1 5 is 25 30 hah 6 12 ag 7 12 ag
6 2 2 2 1
思路:
水题,模拟一遍即可。
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
int main()
{
string str,z,temp;
int q,i,l,r,len,num;
cin>>str;
cin>>q;
while(q--)
{
scanf("%d%d",&l,&r);
cin>>z;
len=z.length();
num=0;
for(i=l;i<=r-len+1;i++)
{
temp=str.substr(i,len);
if(temp==z)
num++;
}
cout<<num<<endl;
}
return 0;
}