https://www.acmicpc.net/problem/10769
🔔 문제 :
주어진 문자열에
:-) :-( 이 이모티콘이 있는지 찾는 문제입니다.
이모티콘이 한 번 도 안나오면 none
두개의 이모티콘이 똑같은 수로 나오면 unsure
:-) 이모티콘이 더 많으면 happy
:-( 이모티콘이 더 많으면 sad를 출력하는 문제입니다.
🔔 Kick Point :
문자열 슬라이싱으로 알아내는 문제로 O(n)의 시간 복잡도를 이용하여 문제를 풀어야합니다.
문자를 탐색해 가며 ':' 이 나왔을 때, -)인지 -( 인지에 따라서 구분해주는 cnt와 isFlag 변수를 조정합니다.
최종적으로 원하는 값을 출력해주면 됩니다.
🔔 Code :
#include <iostream>
#include <string>
using namespace std;
int main() {
string str; getline(cin, str);
int cnt(0);
bool isFlag = false;
for (string::iterator i = str.begin(); i != str.end(); i++) {
if (*i == ':' && *(i + 1) == '-') {
if (*(i + 2) == ')'){
cnt++;
isFlag = true;
}
else if (*(i + 2) == '(') {
cnt--;
isFlag = true;
}
}
}
if (cnt > 0) cout << "happy";
else if (cnt == 0 && isFlag) cout << "unsure";
else if (cnt == 0 && !isFlag) cout << "none";
else cout << "sad";
}