작성일 :

문제 링크

4575번 - Refrigerator Magnets

설명

각 문장에 같은 알파벳이 두 번 이상 등장하지 않을 때만 그대로 출력하는 문제입니다.


접근법

한 줄씩 읽다가 END가 나오면 종료합니다.
공백을 제외하고 알파벳의 등장 여부를 확인해 중복이 없으면 그 줄을 출력합니다.


Code

C#

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

class Program {
  static void Main() {
    var sb = new StringBuilder();
    string? line;
    while ((line = Console.ReadLine()) != null) {
      if (line == "END") break;
      var seen = new bool[26];
      var ok = true;
      foreach (var ch in line) {
        if (ch == ' ') continue;
        var idx = ch - 'A';
        if (seen[idx]) { ok = false; break; }
        seen[idx] = true;
      }
      if (ok) sb.AppendLine(line);
    }
    Console.Write(sb);
  }
}

C++

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

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

  string line;
  while (getline(cin, line)) {
    if (line == "END") break;
    bool seen[26] = {};
    bool ok = true;
    for (char ch : line) {
      if (ch == ' ') continue;
      int idx = ch - 'A';
      if (seen[idx]) { ok = false; break; }
      seen[idx] = true;
    }
    if (ok) cout << line << "\n";
  }

  return 0;
}