[백준 23367] Dickensian Dictionary (C#, C++) - soo:bak
작성일 :
문제 링크
23367번 - Dickensian Dictionary
설명
영문 소문자 단어가 왼손(qwertasdfgzxcvb)과 오른손(yuiophjklnm) 타이핑이 번갈아가며 나타나는지 판단해, 맞으면 “yes”, 아니면 “no”를 출력하는 문제입니다.
접근법
각 문자가 왼손인지 오른손인지 판별한 뒤, 인접한 두 문자의 손이 같으면 실패입니다.
끝까지 번갈아 나오면 “yes”를 출력합니다.
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
24
25
using System;
class Program {
static bool IsLeft(char c) {
const string left = "qwertasdfgzxcvb";
return left.IndexOf(c) != -1;
}
static void Main() {
var s = Console.ReadLine()!;
var prevLeft = IsLeft(s[0]);
var ok = true;
for (var i = 1; i < s.Length; i++) {
var curLeft = IsLeft(s[i]);
if (curLeft == prevLeft) {
ok = false;
break;
}
prevLeft = curLeft;
}
Console.WriteLine(ok ? "yes" : "no");
}
}
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
25
26
27
28
#include <bits/stdc++.h>
using namespace std;
bool isLeft(char c) {
static const string left = "qwertasdfgzxcvb";
return left.find(c) != string::npos;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s; cin >> s;
bool prev_left = isLeft(s[0]);
bool ok = true;
for (size_t i = 1; i < s.size(); i++) {
bool cur_left = isLeft(s[i]);
if (cur_left == prev_left) {
ok = false;
break;
}
prev_left = cur_left;
}
cout << (ok ? "yes" : "no") << "\n";
return 0;
}