본문 바로가기

Algorithm

(C++) - 백준(BOJ) 2810번 : 컵홀더

반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
using namespace std;
 
 
//왼쪽끝에 커플석이 오른쪽 끝에 일반석
//왼쪽끝에 일반석이 오른쪽 끝에 커플석
int main() {
    string n;
    int ans = 1;
    int l_cnt = 0;
    int word;
    cin >> word >> n;
 
    for (int i = 0; i < word; i++)
    {
        if (n[i] == 'L')
        {
            if (l_cnt % 2 == 1//L이 처음 나왔을 경우
            {
                l_cnt++;
                continue;
            }
            else //L이 두번째 나왔을 경우
            {
                l_cnt++;
                ans += 1;
                
            }
        }
        else if (n[i] == 'S')
        {
            ans += 1;
        }
    }
    //예외처리 : 정답이 사람 수 보다 많으면 컵홀더에 컵을 꽂을 수 있는 최대 인원을 구하는 문제이므로 정답은 사람 수가 된다.
    if (ans > word)
        ans = word;
    cout << ans << '\n';
}