警示后人100->85
查看原帖
警示后人100->85
150956
StillEmpty楼主2023/10/30 22:20

考场版:

#include <bits/stdc++.h>
using namespace std;

const int N = 2e6;
int n, f[N][26], cnt[N];
string s;

int main() {
    ios::sync_with_stdio(false);
    freopen("game.in", "r", stdin);
    freopen("game.out", "w", stdout);
    cin >> n >> s;
    memset(f, 0xff, sizeof(f));
    f[n-1][s[n-1]-'a'] = n-1;
    for(int i = n-2; i >= 0; --i) {
        int ni = f[i+1][s[i]-'a'];
        if(ni == -1) {
            f[i][s[i]-'a'] = i;
            continue;
        }
        cnt[i] = cnt[ni+1]+1;
        memcpy(f[i], f[ni+1], sizeof(f[i]));
        f[i][s[i]-'a'] = i;
    }
    long long ans = 0;
    for(int i = 0; i < n; ++i) ans += cnt[i];
    cout << ans << endl;
    return 0;
}

fix:\text{fix:}\\ int n, f[N][26], cnt[N]; →\\\to\\ int n, f[N+1][26], cnt[N+1];

代码中 ni 表示使 [i, ni] 串合法且最小的数,本代码的问题在于 ni 可能为 n-1,直接溢出。

2023/10/30 22:20
加载中...