当前位置:   article > 正文

Segment Occurrences C++题解

occurrences c++

Segment Occurrences C++题解

Time limit:2000 ms Memory limit:262144 kB

描述

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

输入

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.

输出

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

样例1输入

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

样例1输出

0
1
0
1

样例2输入

15 2 3
abacabadabacaba
ba
1 15
3 4
2 14

样例2输出

4
0
3

样例3输入

3 5 2
aaa
baaab
1 3
1 1

样例3输出

0
0

提示

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

代码

#include <bits/stdc++.h>//万能头文件

using namespace std;

int main()
{
	int n, m, q, s[10000] = { 0 };
	int l, r, ans;//ans是总数,l是左边界,r是右边界
	string a, b;
	cin >> n >> m >> q;
	cin >> a >> b;

	//创建bitmap
	for (int i = 0; i <= n - m; i++) {
		if (a.substr(i, m) == b) {
			s[i] = 1;
		}
	}
	while (q--) {
		ans = 0;
		scanf("%d%d", &l, &r);
		for (int i = l - 1; i <= r - m; i++) {
			if (s[i]) {
				ans++;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

也看了一些题解,觉得这个有点意思
本人也是新手,也是在学习中,勿喷

转载请注明出处

欢迎有问题的小伙伴一起交流哦~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/71558
推荐阅读
相关标签
  

闽ICP备14008679号