문제) 백준 - Set, HashMap - 놀라운 문자열
-> https://www.acmicpc.net/problem/1972
1972번: 놀라운 문자열
대문자 알파벳으로만 이루어져 있는 문자열이 있다. 이 문자열에 대해서 ‘D-쌍’이라는 것을 정의할 수 있는데, 이 문자열에 포함되어 있는, 거리가 D인 두 문자를 순서대로 나열한 것을 이 문
www.acmicpc.net
입력으로 주어지는 문자열의 길이가 80을 넘지 않고 입력의 줄 수가 101줄을 넘지 않으므로 Set을 이용하여 D-쌍들을 하나씩 탐색하여 놀라운 문자열인지 판별할 수 있다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#include<unordered_map> | |
#define ll long long | |
#define endl "\n" | |
#define MAX 200001 | |
using namespace std; | |
typedef pair<int, int> p; | |
bool solve(string a, int n) { | |
//set을 이용하여 D-쌍 탐색 | |
set<string> strings; | |
for (int i = 0; i < a.size() - n - 1; ++i) { | |
string temp = ""; | |
temp += a[i]; | |
temp += a[i + n + 1]; | |
if (strings.count(temp) == 0) | |
strings.insert(temp); | |
else | |
return false; | |
} | |
return true; | |
} | |
bool surprising(string a) { | |
// 문자열의 길이가 1일 경우 놀라운 문자열 | |
if (a.size() == 1) return true; | |
for (int i = 0; i <= a.size() - 2; ++i) | |
if (!solve(a, i)) | |
return false; | |
return true; | |
} | |
int main() { | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); cout.tie(0); | |
while (1) { | |
string s; cin >> s; | |
// 입력의 마지막 줄 | |
if (s.size() == 1 && s[0] == '*') | |
break; | |
if (surprising(s)) | |
cout << s << " is surprising." << endl; | |
else | |
cout << s << " is NOT surprising."<< endl; | |
} | |
return 0; | |
} |
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 11653번 - 소인수 분해 (C++) 문제 및 풀이 (0) | 2021.09.27 |
---|---|
[백준] 14881번 - 물통 문제 (C++) 문제 및 풀이 (0) | 2021.09.24 |
[백준] 2631번 - 줄세우기 (C++) 문제 및 풀이 (0) | 2021.09.03 |
[백준] 5557번 - 1학년 (C++) 문제 및 풀이 (0) | 2021.07.27 |
[백준] 2186번 - 문자판 (C++) 문제 및 풀이 (0) | 2021.07.27 |
댓글