Coding Test - cpp/Greedy
[백준 1439: 뒤집기] - C++
에드윈H
2021. 2. 4. 13:42
1439번: 뒤집기
다솜이는 0과 1로만 이루어진 문자열 S를 가지고 있다. 다솜이는 이 문자열 S에 있는 모든 숫자를 전부 같게 만들려고 한다. 다솜이가 할 수 있는 행동은 S에서 연속된 하나 이상의 숫자를 잡고 모
www.acmicpc.net
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string n;
cin >> n;
char first = n[0];
int num1 = 0;
int num2 = 0;
if (first == '0')
{
num1++;
}
else
{
num2++;
}
int i = 0;
while(n[++i]!='\0')
{
if (n[i-1]!= n[i])
{
if ('0' == n[i])
{
num1++;
}
else
{
num2++;
}
}
}
int result = min(num1, num2);
cout << result << '\n';
return 0;
}