electicode
HomeCoursesResourcesProblemsNational OlympiadContestsLeaderboard
...
Problems/Alice and Bob playing an easy game/Editorial

Editorial: Alice and Bob playing an easy game

Notations & Definitions

  • NNN — the number of rounds in the game
  • AwinsA_{\text{wins}}Awins​ — the number of rounds won by Alice
  • BwinsB_{\text{wins}}Bwins​ — the number of rounds won by Bob
  • Since exactly one player wins each round: Awins+Bwins=NA_{\text{wins}} + B_{\text{wins}} = NAwins​+Bwins​=N

Subtask 1 (N=1N = 1N=1)

When there is only one round, the winner of that single round is the overall winner of the game.

Algorithm:

  1. Read the single character from the input
  2. If the character is A, Alice wins the game
  3. If the character is B, Bob wins the game

Time Complexity: O(1)O(1)O(1)
Space Complexity: O(1)O(1)O(1)

Subtask 2 (General Case, N≤9N \leq 9N≤9)

For the general case, we need to count how many rounds each player won and determine who has more wins.

Key Observation:
Since NNN is always odd, there cannot be a tie. Therefore, one player will always have strictly more wins than the other.

Algorithm:

  1. Read the number of rounds NNN
  2. Read the string of round results
  3. Count the number of A characters to get AwinsA_{\text{wins}}Awins​
  4. Count the number of B characters to get BwinsB_{\text{wins}}Bwins​ (alternatively, Bwins=N−AwinsB_{\text{wins}} = N - A_{\text{wins}})

Time Complexity: O(N)O(N)O(N) — for counting characters in the string
Space Complexity: O(N)O(N)O(N) — for storing the input string (or O(1)O(1)O(1) if we count while reading)


Code Solutions

← Back to Problem
Bwins​=N−Awins​
  • If Awins>BwinsA_{\text{wins}} > B_{\text{wins}}Awins​>Bwins​, output "Alice"
  • Otherwise, output "Bob"
  • #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        int N;
        string rounds;
        
        cin >> N >> rounds;
        
        int alice_wins = 0;
        int bob_wins = 0;
        
        for (char ch : rounds) {
            if (ch == 'A') {
                alice_wins++;
            } else {
                bob_wins++;
            }
        }
        
        if (alice_wins > bob_wins) {
            cout << "Alice" << endl;
        } else {
            cout << "Bob" << endl;
        }
        
        return 0;
    }