알고리즘
백준 1283번 단축키 지정 C++
영춘권의달인
2022. 4. 20. 10:53
getline으로 입력을 받고 set에 지정된 단축키들을 저장하면서 문제를 해결하였다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
using ll = long long;
set<char> st;
bool IsExsist(char ch) {
auto it = st.find(tolower(ch));
if (it == st.end()) return false;
return true;
}
void PrintAns(string str, int idx = -1) {
for (int i = 0; i < str.length(); i++) {
if (i == idx) {
cout << '[' << str[i] << ']';
}
else cout << str[i];
}
cout << "\n";
}
void InsertAnsPrint(string str, int idx) {
st.insert(tolower(str[idx]));
PrintAns(str, idx);
}
bool Step1(string str) {
bool ret = false;
if (!IsExsist(str[0])) {
InsertAnsPrint(str, 0);
return true;
}
for (int i = 1; i < str.length(); i++) {
if (str[i] == ' ') {
if (i + 1 >= str.length()) return false;
if (IsExsist(str[i + 1])) continue;
InsertAnsPrint(str, i + 1);
return true;
}
}
return false;
}
bool Step2(string str) {
for (int i = 0; i < str.length(); i++) {
if (IsExsist(str[i])) continue;
if (str[i] == ' ') continue;
InsertAnsPrint(str, i);
return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
string str;
cin >> n;
cin.ignore(99999, '\n');
for (int i = 0; i < n; i++) {
getline(cin, str);
if (Step1(str)) continue;
if (Step2(str)) continue;
cout << str << "\n";
}
};