/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 1ms 540.0 KiB
#2 Wrong Answer 4ms 540.0 KiB

Code

#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;

// Function to check if a number is prime
bool isPrime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i <= sqrt(n); ++i) {
        if (n % i == 0) return false;
    }
    return true;
}

// Function to find the nearest prime number greater than or equal to n
int nearestPrime(int n) {
    while (!isPrime(n)) {
        ++n;
    }
    return n;
}

// Function to compute the minimum number of operations to make the string beautiful
int minOperationsToMakeBeautiful(int N, const string& S) {
    // Count occurrences of 'a', 'b', and 'c'
    int count_a = 0, count_b = 0, count_c = 0;
    for (char ch : S) {
        if (ch == 'a') count_a++;
        else if (ch == 'b') count_b++;
        else if (ch == 'c') count_c++;
    }

    // Find the nearest prime numbers greater than or equal to the counts
    int prime_a = nearestPrime(count_a);
    int prime_b = nearestPrime(count_b);
    int prime_c = nearestPrime(count_c);

    // Calculate the number of operations required to make each count a prime number
    int operations = abs(prime_a - count_a) + abs(prime_b - count_b) + abs(prime_c - count_c);

    return operations;
}

int main() {
    int T;  // Number of test cases
    cin >> T;

    while (T--) {
        int N;  // Length of the string
        cin >> N;
        string S;  // The string S
        cin >> S;

        // Get the minimum number of operations for each test case
        cout << minOperationsToMakeBeautiful(N, S) << endl;
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1158 Yet another Beautiful String
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 00:06:41
Judged At
2025-01-02 00:06:41
Judged By
Score
0
Total Time
4ms
Peak Memory
540.0 KiB