알고리즘

[알고리즘] 백준 1522 - 문자열 교환 (C++)

blueberrysoda 2024. 8. 31. 23:56

[문제 링크] : https://www.acmicpc.net/problem/1522

#include <iostream>
using namespace std;

int main(){
    string S;
    cin >> S;

    int A = 0, Ans = S.size();
    for(auto c : S){
        if(c == 'a'){
            A++;
        }
    }

    for(int i=0; i<S.size(); i++){
        int cnt = A;
        int tmp = 0;
        for(int j=i; j<i+S.size(); j++){
            if(cnt == 0) break;
            if(S[j % S.size()] == 'b'){
                tmp++;
            }
            cnt--;
        }
        Ans = min(Ans, tmp);
    }

    cout << Ans << "\n";
    return 0;
}