题解:P14018 [ICPC 2024 Nanjing R] 左移 3

· · 题解

很明显,这道题只需要暴力枚举就可以了,为了防止超时,我们取 n 和 m 的最小值,用前缀数组存下来,最后求最大值即可。

建议降橙

代码如下:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 4e5 + 5;
int t, n, m, ans, arr[N];
string s, s1;
inline void solve(){
    ans = 0;
    cin >> n >> m >> s;
    s += s, s = " " + s;
    for(int i = 1; i <= n + min(n, m) - 6; i++){
        arr[i] = arr[i - 1], s1 = "";
        for(int j = i; j <= i + 6; j++) s1 += s[j];
        if(s1 == "nanjing") arr[i]++;
    }
    for(int i = n + min(n, m) - 5; i <= n + min(n, m); i++){
        arr[i] = arr[i - 1];
    }
    for(int i = n; i <= n + min(n, m); i++) ans = max(ans, arr[i] - arr[i - n]);
    cout << ans << "\n";
    return ;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> t;
    while(t--) solve(); //记得多组
    return 0;
}

管理大大求过!