Official

A - 11/22 String Editorial by en_translator


For beginners

This problem asks if you can treat strings properly.
To check if an input string \(S\) of length \(N\) is a \(11/22\) string, it is sufficient to check if all of the following conditions are satisfied.

  • \(N\) is odd.
  • The \(1\)-st through \(\left(\frac{N+1}{2}-1\right)\)-th characters are 1.
  • The \(\left(\frac{N+1}{2}\right)\)-th character is /.
  • The \(\left(\frac{N+1}{2} + 1\right)\)-th characters are 2.

To check if these conditions are met, it is sufficient to find the character at a given position one by one.
In C++, one can use the std::string type to handle strings easily, so we recommend to use the std::string type.

  • Sample code (C++): in this sample code, we use a function. By using a function, repeated similar process can be implemented all at once.
#include <iostream>
#include <string>
using namespace std;

void No() {
  cout << "No" << "\n";
  exit(0);
}

int main() {
  int N;
  string S;
  cin >> N >> S;
  if (N % 2 == 0) No();
  for (int i = 0; i < (N + 1) / 2 - 1; i++) {
    if (S[i] != '1') No();
  }
  if (S[(N + 1) / 2 - 1] != '/') No();
  for (int i = (N + 1) / 2; i < N; i++) {
    if (S[i] != '2') No();
  }
  cout << "Yes" << "\n";
}

posted:
last update: