公式

A - A Healthy Breakfast 解説 by Cyanmond


初心者の方へ

  • AtCoder をはじめたばかりで何をしたらよいか分からない方は、まずは practice contest の問題 A「Welcome to AtCoder」を解いてみてください。基本的な入出力の方法が載っています。
  • また、プログラミングコンテストの問題に慣れていない方は、AtCoder Beginners Selection の問題をいくつか解いてみることをおすすめします。
  • C++入門 AtCoder Programming Guide for beginners (APG4b) は、競技プログラミングのための C++ 入門用コンテンツです。

例えば、以下のような手順でこの問題を解くことができます。

  1. 文字列変数 s を用意し、 s に標準入力から入力を読み込む。
  2. for 文を用いるなどして、 R M がそれぞれ s のどこに含まれるかを調べる。 それぞれ index を変数 pos_r pos_m に保存する。
  3. if 文で pos_r < pos_m かどうかを判定し、そうであれば Yes 、そうでなければ No を出力する。

他にも、 \(S\) としてあり得る文字列はたかだか \(6\) 通りしかないので、全て答えを手で求めておいて場合分けで解くこともできます。

C++ による実装例

#include <string>
#include <iostream>

using namespace std;

int main() {
    string s;
    cin >> s;
    int pos_r = -1, pos_m = -1;
    for (int i = 0; i < 3; ++i) {
        if (s[i] == 'R') {
            pos_r = i;
        }
        if (s[i] == 'M') {
            pos_m = i;
        }
    }
    if (pos_r < pos_m) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}

投稿日時:
最終更新: