QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#639828#9454. String of CCPCvwxyzAC ✓160ms3716kbC++201.8kb2024-10-13 23:05:102024-10-13 23:05:10

Judging History

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

  • [2024-10-13 23:05:10]
  • 评测
  • 测评结果:AC
  • 用时:160ms
  • 内存:3716kb
  • [2024-10-13 23:05:10]
  • 提交

answer

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    int T;
    cin >> T;
    while (T--) {
        int N;
        cin >> N;
        string S;
        cin >> S;
        
        // Count initial "CCPC" occurrences
        int cnt = 0;
        for (int i = 0; i <= N - 4; ++i) {
            if (S.substr(i, 4) == "CCPC") {
                ++cnt;
            }
        }
        
        int ans = cnt;

        // Try replacing each position with 'C' or 'P'
        for (char s : {'C', 'P'}) {
            for (int i = 0; i <= N; ++i) {
                int c = cnt;
                string S0 = "", S1 = "";
                
                // Construct the modified substrings S0 and S1
                for (int j = i - 3; j < i; ++j) {
                    if (0 <= j && j < N) {
                        S0 += S[j];
                        S1 += S[j];
                    }
                }
                S1 += s; // Replace one position with s
                
                for (int j = i; j < i + 3; ++j) {
                    if (0 <= j && j < N) {
                        S0 += S[j];
                        S1 += S[j];
                    }
                }

                // Recompute number of "CCPC" in S0
                for (int j = 0; j <= (int)S0.size() - 4; ++j) {
                    if (S0.substr(j, 4) == "CCPC") {
                        --c;
                    }
                }

                // Recompute number of "CCPC" in S1
                for (int j = 0; j <= (int)S1.size() - 4; ++j) {
                    if (S1.substr(j, 4) == "CCPC") {
                        ++c;
                    }
                }
                
                ans = max(ans, c);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
3
CCC
5
CCCCP
4
CPCP

output:

1
1
1

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 160ms
memory: 3716kb

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