Priceless

[백준] 1439번: 뒤집기 (C++) 본문

Algorithm & Test/백준

[백준] 1439번: 뒤집기 (C++)

Hyun__ 2023. 8. 16. 17:30

 

특별한 예외 처리가 필요할 줄 알았지만

생각보다 너무 쉬워서 빨리 해결할 수 있었다

 

개수가 적은 수를 바꿔야 하므로

값이 바뀌는 횟수를 세고 난 후 절반을 나누면 된다

// baekjoon 1439

#include<iostream>
#include<algorithm>
#include<string>

using namespace std;

// codes for fast I/O
void init(){
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(false);
}


int main(){
    init();
    
    string input;
    cin >> input;
    
    int temp = input[0];
    int n = input.length();
    
    int switching = 0;
    
    for(int i = 1; i < n; i++){
        if (input[i] != temp) {
            switching++;
        }
        temp = input[i];
    }
    
    cout << (switching + 1) / 2;
}