문제 바로가기
접근방법
- 2개가 연속으로 같으면 지워준다.
- 여기서 인덱스 에러 나지않게 조심해야 한다.
슈도코드
if str[i-1] == str[i]
str.erase(i-1,2)
풀이
#include <iostream>
#include <string>
using namespace std;
int test_case = 10, strLength, j;
string str, answer;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
for (int i = 1; i <= test_case; i++) {
cin >> strLength;
cin >> str;
answer = "";
j = 1;
while (j != str.size()) {
if (str[j - 1] == str[j]) {
str.erase(j - 1, 2);
if (j == 1 || j == 2) j = 1;
else j -= 2;
}
else
j++;
}
cout << "#" << i << " " << str << "\n";
}
return 0;
}