Official
A - 11/22 String Editorial by en_translator
For beginners
- If you are new to learning programming and do not know where to start, please try Problem A "Welcome to AtCoder" from practice contest. There you can find a sample code for each language.
- Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in "AtCoder Beginners Selection".
- 「C++入門 AtCoder Programming Guide for beginners (APG4b)」 is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.
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: