간단한 그리디 문제이다.
앞에 나온 문자를 기억하고 입력 받은 문자열을 거꾸로 반복문을 돌아 앞에 나온 문자와 다른 문자가 있으면 cnt + 1을 해준다. 여기서 직전에 나온 문자와도 달라야한다.
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
string str;
cin >> str;
char first = str[0];
char before = 'A';
int cnt = 1;
for (int i = str.size() - 1; i >= 0; i--) {
if (first != str[i] && before != str[i])
cnt++;
before = str[i];
}
cout << cnt;
}