작성일 :

문제 링크

25277번 - Culture shock

설명

말한 단어들이 주어질 때, 금지된 대명사가 등장한 횟수를 출력하는 문제입니다.


접근법

금지 단어가 4개로 고정되어 있으므로, 이를 집합에 저장해두면 각 단어의 금지 여부를 빠르게 확인할 수 있습니다.

입력된 모든 단어를 순회하면서 집합에 포함되는지 검사하고, 포함되면 횟수를 증가시킵니다.


Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;

class Program {
  static void Main() {
    var n = int.Parse(Console.ReadLine()!);
    var words = Console.ReadLine()!.Split();
    var bad = new HashSet<string> { "he", "she", "him", "her" };

    var cnt = 0;
    for (var i = 0; i < n; i++) {
      if (bad.Contains(words[i])) cnt++;
    }

    Console.WriteLine(cnt);
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; cin >> n;
  unordered_set<string> bad = {"he", "she", "him", "her"};

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    string w; cin >> w;
    if (bad.count(w)) cnt++;
  }

  cout << cnt << "\n";

  return 0;
}