Official

B - Bouzu Mekuri Editorial by en_translator


We simulate the game that Takahashi and Aoki will play.

Just like each player eats the cards in order, we inspect each character of the string \(S\) in order from the beginning.
Let \(X\) be the index of the first occurrence of 1 in the string \(S\). (Here, we regard the first character as the \(0\)-th one, the succeeding character as the \(1\)-th one, \(\cdots\) and so on.)
Then, the player who eats the \(X\)-th card from the top in the deck will eat a bad card first, and consequently lose the game.
Since Takahashi is the first player who eats the card, so Takahashi will lose if \(X\) is even; Aoki will lose if \(X\) is odd.

Therefore, we can solve the problem by inspecting the string \(S\) from the beginning one by one and find the position where 1 appears for the first time. In order to implement this, we can use the “loop” structure of programming languages. In C++ language, the for statement realizes the repetition feature, for example.

The sample code in C++ language for this problem follows.

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
  int n;
  string s;

  cin >> n >> s;
  for(int i = 0; i < (int)s.size(); i++){
    if(s[i] == '1'){
      if(i % 2 == 0) cout << "Takahashi" << endl;
      else cout << "Aoki" << endl;
      break;
    }
  }

  return 0;
}

posted:
last update: