Official

A - Probably English Editorial by physics0523


初心者の方へ
  • プログラミングの学習を始めたばかりで何から手をつけるべきかわからない方は、まずは practice contest の問題A「Welcome to AtCoder」をお試しください。言語ごとに解答例が掲載されています。
  • また、プログラミングコンテストの問題に慣れていない方は、 AtCoder Beginners Selection の問題をいくつか試すことをおすすめします。
  • 競プロ典型 90 問 では、プログラミングコンテストで扱われる典型的な 90 問の問題に挑戦可能です。
  • C++入門 AtCoder Programming Guide for beginners (APG4b) は、競技プログラマー向けのC++入門用コンテンツです。

今回の問題では、 \(N\) 個の単語に対して \(5\) つの単語のどれかと合致するか判定することを繰り返す問題です。
for 文の中に if 文を \(5\) つ並べてもよいですが、もう少し簡単な方法として、「 if 文の中で変化するものだけを配列に入れて for 文を回す」というものがあります。
具体的には、 word = {"and", "not", "that", "the", "you"} という配列を作った上で、 if(W_i == word[i]){result = true;} と判定する if 文を \(i=0,1,2,3,4\) の範囲でループする for 文の中に入れるというものです。
実装例も参照してください。

実装例 (C++):

#include<bits/stdc++.h>

using namespace std;

vector<string> word={"and", "not", "that", "the", "you"};

int main(){
  int n;
  cin >> n;
  bool res=false;
  for(int i=0;i<n;i++){
    string s;
    cin >> s;
    for(auto &nx : word){
      if(s==nx){res=true;}
    }
  }
  if(res){cout << "Yes\n";}else{cout << "No\n";}
  return 0;
}

posted:
last update: