오랜만에 풀어보는 그리디 알고리즘 문제
딱히 그리디 알고리즘 같지는 않다. 다른 분들의 풀이를 참고할 필요가 있다.
#include <iostream>
#include <string>
using namespace std;
string input_string()
{
string S;
cin >> S;
return S;
}
void find_result(string S)
{
int zero_count = 0, one_count = 0;
int i, length = S.length();
char current;
//1로 맞추는 경우, 0으로 맞추는 경우 비교 : 그리디 알고리즘은 아닌 듯
if (S[0] == '0')
{
zero_count++;
}
else
{
one_count++;
}
for (i = 1; i < length; i++)
{
if (S[i] == S[i - 1])
{
;
}
else
{
if (S[i] == '0')
{
zero_count++;
}
else
{
one_count++;
}
}
}
//cout << zero_count << "\n" << one_count << "\n";
cout << min(zero_count, one_count) << "\n";
return;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string S;
S = input_string();
find_result(S);
return 0;
}