/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Wrong Answer 6ms 580.0 KiB
#3 Wrong Answer 3ms 536.0 KiB

Code

#include <bits/stdc++.h>


using namespace std;

int maxConsecutiveOnes(const string& S) {
    int maxOnes = 0;
    int currentOnes = 0;
    vector<int> segments;

    for (char c : S) {
        if (c == '1') {
            currentOnes++;
        } else {
            if (currentOnes > 0) {
                segments.push_back(currentOnes);
                maxOnes = max(maxOnes, currentOnes);
                currentOnes = 0;
            }
        }
    }
    // In case the string ends with 1s
    if (currentOnes > 0) {
        segments.push_back(currentOnes);
        maxOnes = max(maxOnes, currentOnes);
    }

    // The number of segments of 1s
    int numSegments = segments.size();

    // If there's only one segment of 1s, the answer is simple.
    if (numSegments <= 1) {
        return maxOnes;
    }


    int maxConsecutive = maxOnes;
    for (int i = 0; i < numSegments - 1; i++) {

        int combined = segments[i] + segments[i + 1];
        maxConsecutive = max(maxConsecutive, combined);
    }

    return maxConsecutive;
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--) {
        int N;
        cin >> N;
        string S;
        cin >> S;

        cout << maxConsecutiveOnes(S) << "\n";
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1113 Fliping Game
Contest
Brain Booster #7
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-05 15:17:55
Judged At
2024-11-11 02:31:00
Judged By
Score
5
Total Time
6ms
Peak Memory
580.0 KiB