https://codeforces.com/contest/1016/problem/B
题目大意
给出 $n$ 长小写字符串 $s$ 和 $m$ 长小写字符串 $t$。给出 $q$ 组询问,每组询问给出 $l_i, r_i$,问 $s[l_i, r_i]$ 子串中 $t$ 出现了几次。
$1 \le n, m \le 1000, 1 \le q \le 10^5$。
简要题解
只要把所有匹配位置的尾巴标记为 $1$,这样就相当于询问 $[l_i + m - 1, r_i]$ 中有多少个 $1$,于是 KMP 处理匹配,然后前缀和处理一下就好了。
注意这题 $nm$ 的暴力匹配应该也可以因为范围给的很小。
复杂度
$T$:$O(n + m + q)$
$S$:$O(n + m)$
代码实现
#include <bits/stdc++.h>
using namespace std;
int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();
using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;
template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) {
return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) {
return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z
if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
if(x<z) { y=x; x=z; } else if(y<z) y=z; }
// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());
namespace KMP {
vector<char> to_vec(const string& s) {
vector<char> ans;
for (char c : s) ans.push_back(c);
return ans;
}
template<typename T>
vector<int> get_nxt(const vector<T>& a) {
int n = a.size();
vector<int> nxt(n + 1, -1);
for (int i = 0, j = -1; i < n; ) {
while (j != -1 && a[j] != a[i]) j = nxt[j];
nxt[++i] = ++j;
}
return nxt;
}
vector<int> get_nxt(const string& s) {
return get_nxt(to_vec(s));
}
/*
kmp
ans[i] maxlen of the prefix of t matches suffix of s[i]
*/
template<typename T>
vector<int> kmp(const vector<T>& s, const vector<T>& t) {
int n = s.size(), m = t.size();
vector<int> nxt = get_nxt(t);
vector<int> ans(n);
for (int i = 0, j = 0; i < n; i++) {
while (j != -1 && (j == m || s[i] != t[j])) j = nxt[j];
ans[i] = ++j;
}
return ans;
}
/*
kmp_cnt
ans[i] maxlen of the prefix of t matches suffix of s[i]
*/
template<typename T>
int kmp_cnt(const vector<T>& s, const vector<T>& t, bool overlap = true) {
int n = s.size(), m = t.size();
vector<int> len = kmp(s, t);
int ans = 0, pre = -1;
for (int i = 0; i < n; i++) {
if (len[i] == m && (overlap || i - pre >= m)) {
ans++;
pre = i;
}
}
return ans;
}
}
using namespace KMP;
/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/
void solve() {
int n, m, q; cin >> n >> m >> q;
string s, t; cin >> s >> t;
auto a = kmp(to_vec(s), to_vec(t));
vector<int> sum{0};
for (int i = 0; i < n; i++) {
sum.push_back(sum[i] + (a[i] == m));
}
int l, r;
for (int i = 0; i < q; i++) {
cin >> l >> r;
l += m - 1;
if (l > r) {
cout << "0\n";
} else {
cout << (sum[r] - sum[l - 1]) << '\n';
}
}
}
int main() {
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
Next: [CF] C. Vasya And The Mushrooms - Educational Codeforces Round 48 (Rated for Div. 2)