728x90
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
문제 풀이
이 문제는 이전 값과 다른 값이 들어올 경우, 바뀌는 값을 출력해주는 문제이다.
처음 문자부터 탐색해야하므로, 임의로 이전 값을 공백으로 저장해주었다.
정답 코드
C++
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0)
#define ll long long
int main() {
fast;
int n;
string s;
cin >> n;
while (n--) {
char prev = ' ';
cin >> s;
for (int i = 0; i < s.length(); i++) {
if (prev != s[i]) {
cout << s[i];
prev = s[i];
}
}
cout << "\n";
}
}
python
for _ in range(int(input())) :
prev = ''
s = input()
for i in range(len(s)) :
if prev != s[i] :
print(s[i], end = "")
prev = s[i]
print()
728x90
'Algorithm > PS' 카테고리의 다른 글
[BOJ/백준] 5524 - 입실 관리 [C++/python] (0) | 2023.03.02 |
---|---|
[BOJ/백준] 5358 - Football Team [C++/python] (0) | 2023.03.02 |
[BOJ/백준] 5341 - Pyramids [C++/python] (0) | 2023.03.02 |
[BOJ/백준] 5300 - Fill the Rowboats! [C++/python] (0) | 2023.03.02 |
[BOJ/백준] 4589 - Gnome Sequencing [C++/python] (0) | 2023.03.01 |
728x90
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
문제 풀이
이 문제는 이전 값과 다른 값이 들어올 경우, 바뀌는 값을 출력해주는 문제이다.
처음 문자부터 탐색해야하므로, 임의로 이전 값을 공백으로 저장해주었다.
정답 코드
C++
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0)
#define ll long long
int main() {
fast;
int n;
string s;
cin >> n;
while (n--) {
char prev = ' ';
cin >> s;
for (int i = 0; i < s.length(); i++) {
if (prev != s[i]) {
cout << s[i];
prev = s[i];
}
}
cout << "\n";
}
}
python
for _ in range(int(input())) :
prev = ''
s = input()
for i in range(len(s)) :
if prev != s[i] :
print(s[i], end = "")
prev = s[i]
print()
728x90
'Algorithm > PS' 카테고리의 다른 글
[BOJ/백준] 5524 - 입실 관리 [C++/python] (0) | 2023.03.02 |
---|---|
[BOJ/백준] 5358 - Football Team [C++/python] (0) | 2023.03.02 |
[BOJ/백준] 5341 - Pyramids [C++/python] (0) | 2023.03.02 |
[BOJ/백준] 5300 - Fill the Rowboats! [C++/python] (0) | 2023.03.02 |
[BOJ/백준] 4589 - Gnome Sequencing [C++/python] (0) | 2023.03.01 |