[SWEA] C++ 1234. [S/W 문제해결 기본] 10일차 - 비밀번호(D3)

swb·2022년 11월 14일
0

SWEA

목록 보기
5/19

문제 바로가기

접근방법

  1. 2개가 연속으로 같으면 지워준다.
  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;
}
profile
개발 시작

0개의 댓글