解説
の末尾 文字が oj であれば Yes 、そうでないならば No と出力するような条件分岐を書けば良いです。
の長さが であるとき範囲外参照をする場合があるため、そのケースは事前に No と判定しておくと安全です。
実装例 (Python)
Python で文字列 の末尾 文字を取るときは S[-k:] と書くことができます。
よって、末尾 文字を取るときには S[-2:] と書けば良いです。
def solve(S):
if len(S) >= 2 and S[-2:] == "oj":
print("Yes")
else:
print("No")
T = int(input())
for _ in range(T):
S = input()
solve(S)
また、 str のメソッドである endswith を使うことでも判定することができます。
def solve(S):
if S.endswith("oj"):
print("Yes")
else:
print("No")
T = int(input())
for _ in range(T):
S = input()
solve(S)
実装例 (C++)
C++ で文字列 の末尾 文字を取るときは S.substr(S.size() - k) と書くことができます。
よって、末尾 文字を取るときには S.substr(S.size - 2) と書けば良いです。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::string;
void solve(string S) {
if (S.size() >= 2 && S.substr(S.size() - 2) == "oj") {
cout << "Yes" << '\n';
} else {
cout << "No" << '\n';
}
}
int main() {
int T; cin >> T;
for (int t = 0; t < T; t++) {
string S; cin >> S;
solve(S);
}
}
また、 std::string のメソッドである ends_with を使うことでも判定することができます。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::string;
void solve(string S) {
if (S.ends_with("oj")) {
cout << "Yes" << '\n';
} else {
cout << "No" << '\n';
}
}
int main() {
int T; cin >> T;
for (int t = 0; t < T; t++) {
string S; cin >> S;
solve(S);
}
}
ユーザー解説
ユーザー解説を書くユーザー解説はまだありません。