赞
踩
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].
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
0
1
0
1
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
4
0
3
3 5 2
aaa
baaab
1 3
1 1
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; }
也看了一些题解,觉得这个有点意思
本人也是新手,也是在学习中,勿喷
转载请注明出处
欢迎有问题的小伙伴一起交流哦~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。