작성일 :

문제 링크

9443번 - Arrangement of Contest

설명

문제 제목들의 첫 글자를 이용해 A부터 연속으로 몇 개의 문제를 만들 수 있는지 구하는 문제입니다.


접근법

각 제목의 첫 글자가 어떤 알파벳인지 표시해둡니다.

이후, A부터 시작해서 연속으로 존재하는 알파벳의 개수를 세어 출력합니다.


Code

C#

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

class Program {
  static void Main() {
    var n = int.Parse(Console.ReadLine()!);
    var seen = new bool[26];
    for (var i = 0; i < n; i++) {
      var s = Console.ReadLine()!;
      var idx = s[0] - 'A';
      if (idx >= 0 && idx < 26) seen[idx] = true;
    }

    var ans = 0;
    while (ans < 26 && seen[ans])
      ans++;
    Console.WriteLine(ans);
  }
}

C++

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

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

  int n; cin >> n;
  bool seen[26] = {0, };
  for (int i = 0; i < n; i++) {
    string s; cin >> s;
    int idx = s[0] - 'A';
    if (0 <= idx && idx < 26) seen[idx] = true;
  }

  int ans = 0;
  while (ans < 26 && seen[ans])
    ans++;
  cout << ans << "\n";

  return 0;
}

Tags: 9443, BOJ, C#, C++, 구현, 문자열, 백준, 알고리즘

Categories: ,