알고리즘
백준 20310번 타노스 C++
영춘권의달인
2023. 1. 15. 14:27
https://www.acmicpc.net/problem/20310
20310번: 타노스
어느 날, 타노스는 0과 1로 이루어진 문자열 $S$를 보았다. 신기하게도, $S$가 포함하는 0의 개수와 $S$가 포함하는 1의 개수는 모두 짝수라고 한다. 갑자기 심술이 난 타노스는 $S$를 구성하는 문자
www.acmicpc.net
0은 앞에서부터 0의 절반의 개수, 1은 뒤에서부터 1의 절반의 개수에 위치한 문자만 출력하였다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
vector<bool> v;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int cnt_0 = 0;
int cnt_1 = 0;
string str;
cin >> str;
v = vector<bool>(str.length(), false);
for (int i = 0; i < str.length(); i++) {
if (str[i] == '0') cnt_0++;
else cnt_1++;
}
int targetCnt = cnt_0 / 2;
int cnt = 0;
for (int i = 0; i < str.length(); i++) {
if (cnt == targetCnt) break;
if (str[i] != '0') continue;
v[i] = true;
cnt++;
}
targetCnt = cnt_1 / 2;
cnt = 0;
for (int i = str.length() - 1; i >= 0; i--) {
if (cnt == targetCnt) break;
if (str[i] != '1') continue;
v[i] = true;
cnt++;
}
for (int i = 0; i < str.length(); i++) {
if (v[i] == false) continue;
cout << str[i];
}
}