본문 바로가기
  • Let's study
PS/백준

[C++] 백준 5357: Dedupe

by 코딩고수이고파 2023. 5. 9.

문제

https://www.acmicpc.net/problem/5357

 

5357번: Dedupe

Redundancy in this world is pointless. Let’s get rid of all redundancy. For example AAABB is redundant. Why not just use AB? Given a string, remove all consecutive letters that are the same.

www.acmicpc.net

 

해결방법

입력받은 문자열의 index 1부터 끝까지 확인하는데 i번째 문자i-1번째 문자같으면 i번째 문제를 삭제하고 i--를 해주고 for문이 다 끝나면 문자열을 출력해주면 된다.

 

코드

#include<iostream>
#include<string>

using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int t;
	cin >> t;

	string str;
	while (t--) {
		cin >> str;

		for (int i = 1; i < str.length(); i++) {
			if (str[i] == str[i - 1]) {
				str.erase(i,1);
				i--;
			}
		}

		cout << str << '\n';
	}

	return 0;
}

'PS > 백준' 카테고리의 다른 글

백준 18698: The Walking Adam  (0) 2023.05.11
[C++] 백준 5300: Fill the Rowboats!  (0) 2023.05.10
[C++] 백준 11257: IT Passport Examination  (0) 2023.05.08
[C++] 백준 3765: Celebrity jeopardy  (0) 2023.05.07
[C++] 백준 21638: SMS from MCHS  (0) 2023.05.06

댓글