작성일 :

문제 링크

26564번 - Poker Hand

설명

다섯 장 카드가 주어질 때, 같은 랭크가 몇 장까지 모여 있는지의 최댓값을 손패의 강도로 정의하고 이를 출력하는 문제입니다. 입력은 여러 데이터셋으로 주어집니다.


접근법

손패의 강도는 같은 랭크의 카드가 최대 몇 장인지로 결정됩니다. 다섯 장의 카드를 순회하며 각 랭크의 등장 횟수를 센 뒤, 가장 많이 등장한 횟수를 출력하면 됩니다.


Code

C#

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

class Program {
  static void Main() {
    var t = int.Parse(Console.ReadLine()!);
    for (var i = 0; i < t; i++) {
      var cards = Console.ReadLine()!.Split();
      var cnt = new Dictionary<char, int>();
      var max = 0;
      foreach (var card in cards) {
        var r = card[0];
        cnt.TryGetValue(r, out var cur);
        cur++;
        cnt[r] = cur;
        if (cur > max) max = cur;
      }
      Console.WriteLine(max);
    }
  }
}

C++

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

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

  int t; cin >> t;
  while (t--) {
    unordered_map<char, int> cnt;
    int best = 0;
    for (int i = 0; i < 5; i++) {
      string s; cin >> s;
      int c = ++cnt[s[0]];
      best = max(best, c);
    }
    cout << best << "\n";
  }

  return 0;
}