프로그래밍 공부

BOJ/백준 25497 기술 연계마스터 임스 본문

Problem Solving/Baekjoon Online Judge

BOJ/백준 25497 기술 연계마스터 임스

khj1999 2024. 8. 10. 18:13

https://www.acmicpc.net/problem/25497

 

조건을 잘못이해해서 빙빙 둘러갔다.

L-R 이나 S-K 사이에 L 또는 S가 들어가서  LSRK 이런식으로도 되는 거였는데 잘못 이해해서
예를 들어 R앞에 S가 나오면 break를 거는 방식으로 처음에 코딩을 했어서 삽질을 좀 했다.

#include <iostream>
#include <stack>

using namespace std;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n; string str;
    int ans = 0;
    stack<char> lr, sk;
    cin >> n;
    cin >> str;

    for(int i = 0; i < n; i++){
        if(str[i] == 'L' || str[i] == 'S'){
            if(str[i] == 'L') lr.push(str[i]);
            else sk.push(str[i]);
        }
        else if(str[i] == 'R' || str[i] == 'K'){
            if(str[i] == 'R'){
                if(!lr.empty()){
                    ans++;
                    lr.pop();
                }
                else break;
            }
            else {
                if(!sk.empty()){
                    ans++;
                    sk.pop();
                }
                else break;
            }
        }
        else {
            ans++;
        }
    }

    cout << ans << '\n';
    return 0;
}
Comments