작성일 :

문제 링크

25840번 - Sharing Birthdays

설명

여러 생일이 주어질 때, 서로 다른 생일의 개수를 출력하는 문제입니다.


접근법

집합 자료구조는 중복을 자동으로 제거하므로, 모든 생일을 집합에 넣으면 서로 다른 생일만 남습니다.

집합의 크기가 곧 서로 다른 생일의 개수입니다.


Code

C#

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

class Program {
  static void Main() {
    var n = int.Parse(Console.ReadLine()!);
    var set = new HashSet<string>();

    for (var i = 0; i < n; i++) {
      var s = Console.ReadLine()!;
      set.Add(s);
    }

    Console.WriteLine(set.Count);
  }
}

C++

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

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

  int n; cin >> n;
  unordered_set<string> st;

  for (int i = 0; i < n; i++) {
    string s; cin >> s;
    st.insert(s);
  }

  cout << st.size() << "\n";

  return 0;
}