A - I'm a teapot 解説 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".
- 「競プロ典型 90 問」(Typical 90 Problems of Competitive Programming) is a collection of typical 90 competitive programming problems; unfortunately, currently the problem statements are all Japanese.
- 「C++入門 AtCoder Programming Guide for beginners (APG4b)」 is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.
- 「Python入門 AtCoder Programming Guide for beginners (APG4bPython)」 is a Python tutorial for competitive programmers. Again, this is only in Japanese.
First, if the length of \(S\) is less than 3, the answer is No
.
Otherwise, the answer is Yes
if the following three conditions are all satisfied, and No
otherwise:
- The \((N-2)\)-th character of \(S\) is
t
. - The \((N-1)\)-th character of \(S\) is
e
. - The \(N\)-th character of \(S\) is
a
.
We can inspect those characters one by one.
Notice that if you try to access the \((N-2)\)-th character when the length of \(S\) is less than \(3\), some languages or situations may produce a runtime error called out-of-bounds array access. (To put informally, the index becomes negative, which causes an attempt to inspect outside the string, leading to an error.) Thus, make sure to check the string length before accessing to the string to avoid out-of-bounds array access.
As an alternative solution, one can also extract the last three characters using substr
function in C++ or the slice feature in Python, and compare it with tea
.
- Sample code (C++)
#include <iostream>
#include <string>
using namespace std;
int main() {
int N;
string S;
cin >> N >> S;
if (N < 3) {
cout << "No\n";
} else {
if (S[N - 3] == 't' and S[N - 2] == 'e' and S[N - 1] == 'a') {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
}
投稿日時:
最終更新: