※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 5357번 문제인 Dedupe이다.
문제는 아래 링크를 확인하자.
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
주어진 문자열에서 "이전 문자와 다른 문자"를 문자열에 주어진 순서대로 출력하는 것으로 문제를 해결할 수 있다.
문자열의 첫 문자에는 "이전 문자"가 없으므로 간단한 예외처리를 해주자.
아래는 제출한 소스코드이다.
#include <iostream>
#include <string>
using namespace std;
int T;
int slen;
string s;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> T;
while (T--) {
string s; cin >> s;
int slen = s.length();
cout << s[0];
for (int i = 1; i < slen; i++) {
if (s[i - 1] != s[i]) cout << s[i];
}
cout << '\n';
}
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 26529 // C++] Bunnies (0) | 2022.12.19 |
---|---|
[BOJ 2372 // Ada] Livestock Count (0) | 2022.12.19 |
[BOJ 26561 // C++] Population (0) | 2022.12.19 |
[BOJ 24450 // C++] 国土分割 (Land Division) (0) | 2022.12.19 |
[BOJ 6825 // C++] Body Mass Index (0) | 2022.12.19 |