[백준 5698] Tautogram (C#, C++) - soo:bak
작성일 :
문제 링크
설명
주어진 문장이 Tautogram
인지 여부를 판단하는 문제입니다.
Tautogram
은 모든 단어가 같은 알파벳으로 시작하는 문장을 의미합니다.
예를 들어, "Sam Simmonds speaks softly"
는 모든 단어가 S
로 시작하므로 Tautogram
입니다.
문장은 공백으로 구분된 단어들로 구성되며, 대소문자는 구분하지 않습니다.
즉, 알파벳만 같으면 T
와 t
는 동일하게 취급합니다.
입력의 마지막에는 종료 조건으로 *
이 주어집니다.
접근법
먼저 한 문장을 입력받은 뒤, 단어들을 공백을 기준으로 나누어 각각의 첫 글자를 소문자로 변환하여 비교합니다.
- 첫 번째 단어의 첫 글자를 기준으로 삼고,
- 이후 모든 단어가 이 기준과 동일한지 확인합니다.
이 작업을 모든 문장에 대해 반복하며, 기준에 맞으면 Y
, 그렇지 않으면 N
을 출력합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
class Program {
static void Main() {
while (true) {
var line = Console.ReadLine();
if (line == "*") break;
var words = line.Split();
char first = char.ToLower(words[0][0]);
bool isTautogram = true;
foreach (var word in words) {
if (char.ToLower(word[0]) != first) {
isTautogram = false;
break;
}
}
Console.WriteLine(isTautogram ? "Y" : "N");
}
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
while (true) {
string line; getline(cin, line);
if (line == "*") break;
char alpha = tolower(line[0]);
bool isT = true;
size_t idx = 0;
while (true) {
idx = line.find(" ", idx);
if (idx == string::npos) break;
if (tolower(line[++idx]) != alpha) isT = false;
}
cout << (isT ? "Y" : "N") << "\n";
}
return 0;
}