QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#635550#9454. String of CCPCucup-team1264#AC ✓42ms3988kbC++203.5kb2024-10-12 20:08:372024-10-12 20:08:37

Judging History

你现在查看的是最新测评结果

  • [2024-10-12 20:08:37]
  • 评测
  • 测评结果:AC
  • 用时:42ms
  • 内存:3988kb
  • [2024-10-12 20:08:37]
  • 提交

answer

// https://www.youtube.com/watch?v=CrymicX875M
// Angel of mercy
// How did you move me
// Why am I on my feet again

#ifndef ONLINE_JUDGE
#include "templates/debug.hpp"
#else
#define debug(...)
#endif

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using u64 = uint64_t;

vector<int> prefixFunction(const string& s) {
    int n = s.length();
    vector<int> pi(n);
    for (int i = 1; i < n; i++) {
        int j = pi[i - 1];
        while (j > 0 && s[i] != s[j]) j = pi[j - 1];
        if (s[i] == s[j]) j++;
        pi[i] = j;
    }
    return pi;
}

vector<array<int, 26>> computeAutomaton(string s) {
    s += '#';
    int n = s.size();
    vector<int> pi = prefixFunction(s);
    vector<array<int, 26>> res(n);
    for (int i = 0; i < n; i++) {
        for (int c = 0; c < 26; c++) {
            if (i > 0 && 'A' + c != s[i]) res[i][c] = res[pi[i - 1]][c];
            else res[i][c] = i + ('A' + c == s[i]);
        }
    }
    return res;
}

void solve() {
    int n; cin >> n;
    string s; cin >> s;
    // atmost 2 'C' and 2 'P'
    constexpr int INF = 0x3f3f3f3f;
    string t = "CCPC";
    auto automaton = computeAutomaton(t);
    using T = array<array<int, 4>, 4>;
    T dp;
    for (auto &v: dp) for (auto &v: v) v = -INF;
    dp[0][0] = 0;

    for (int i = 0; i < n; i++) {
        T ndp;
        for (auto &v: ndp) for (auto &v: v) v = -INF;
        for (int cc = 0; cc < 4; cc++) {
            for (int pf = 0; pf < 4; pf++) {
                if (dp[pf][cc] == -INF) continue;
                int val = dp[pf][cc];
                // normal transition
                char c = s[i];
                int npf = automaton[pf][c - 'A'], nval = val;
                if (npf == 4) nval++, npf = 1;
                ndp[npf][cc] = max(ndp[npf][cc], nval);
                // insert a 'C'
                if (cc + 1 < 4) {
                    c = 'C';
                    npf = automaton[pf][c - 'A'], nval = val;
                    if (npf == 4) nval++, npf = 1;
                    dp[npf][cc + 1] = max(dp[npf][cc + 1], nval - cc);
                    c = 'P';
                    npf = automaton[pf][c - 'A'], nval = val;
                    dp[npf][cc + 1] = max(dp[npf][cc + 1], nval - cc);
                }
            }
        }
        dp.swap(ndp);
    }
    for (int cc = 0; cc < 4; cc++) {
        for (int pf = 0; pf < 4; pf++) {
            if (dp[pf][cc] == -INF) continue;
            int val = dp[pf][cc];
            // normal transition
            char c = s[n];
            int npf = automaton[pf][c - 'A'], nval = val;
            if (npf == 4) nval++, npf = 1;
            // insert a 'C'
            if (cc + 1 < 4) {
                c = 'C';
                npf = automaton[pf][c - 'A'], nval = val;
                if (npf == 4) nval++, npf = 1;
                dp[npf][cc + 1] = max(dp[npf][cc + 1], nval - cc);
                c = 'P';
                npf = automaton[pf][c - 'A'], nval = val;
                dp[npf][cc + 1] = max(dp[npf][cc + 1], nval - cc);
            }
        }
    }
    int ans = -INF;
    for (int pf = 0; pf < 4; pf++) {
        for (int cc = 0; cc < 4; cc++) {
            ans = max(ans, dp[pf][cc]);
        }
    }
    cout << ans << '\n';
}
#undef int

// Make bold hypotheses and verify carefully
int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    };
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3592kb

input:

3
3
CCC
5
CCCCP
4
CPCP

output:

1
1
1

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 42ms
memory: 3988kb

input:

20003
5
PCCPC
10
CPPPPCPCPC
4
CPPC
11
CCPPCPPPCCP
17
PPPPCPCCCCCPCCCCC
10
PPCCPCPPCP
9
CPCCCCPPC
11
PCPPPPCCPPP
15
CPCPPPPCCPCPCCC
11
PCCPPCCPCPP
9
PCPCCPPCP
10
CCPCPPPCPP
14
CCCCPPPCPCPCPP
2
CC
12
CCPCPPPPPCPP
6
CPPPPP
12
PCCPCCCCCPCC
16
CPCCPCCPPCCCCPPC
7
CPPPCPC
16
PPPPPCCPCPCPCPPC
13
PPPCPCCCCPP...

output:

1
1
0
1
2
1
1
1
2
2
1
1
1
0
1
0
3
2
1
2
1
2
2
0
1
2
3
1
1
3
1
2
2
1
0
0
0
3
1
0
0
1
1
2
0
1
1
0
1
2
0
1
0
1
0
3
1
1
0
2
1
3
2
2
0
2
2
0
0
2
1
1
3
3
1
3
1
2
0
1
1
0
1
2
2
1
1
2
1
3
1
1
3
1
2
2
0
1
0
3
0
1
1
2
2
0
2
1
1
2
2
0
3
1
1
1
1
2
1
2
0
1
1
0
3
0
3
1
1
0
0
1
0
3
0
1
1
1
1
2
2
1
1
0
0
1
2
0
1
2
...

result:

ok 20003 lines

Extra Test:

score: 0
Extra Test Passed