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 41 42 43 44 45 46 | #include<iostream> #include<queue> #include<vector> #include<string> #include<algorithm> using namespace std; int N, B, W; int BFS(string s) { queue<char> rock; int bcount = 0, wcount = 0; int MAXSIZE = 0; for (int i = 0; i < s.size(); i++) { rock.push(s[i]); if (s[i] == 'W') wcount++; if (s[i] == 'B') bcount++; // 항상 W+B<=N, 산책 중단조건 하얀색조약돌>=W개, 검은색조약돌<=B개 if ((wcount + bcount) <= N && wcount >= W && bcount <= B) { int Size = rock.size(); MAXSIZE = max(MAXSIZE, Size); } //하얀색 조약돌을 아무리 많이 주워도 W+B가 N보다 작을때만 개수를 세므로 , //검은색 조약돌의 개수만 주의하면 예외 처리가능. while (bcount > B) { if (rock.front() == 'W') { wcount--; rock.pop(); } else if (rock.front() == 'B') { bcount--; rock.pop(); } } } return MAXSIZE; } int main(void) { cin >> N >> B >> W; string s; cin >> s; cout << BFS(s) << endl; } | cs |
문제 출처:https://www.acmicpc.net/problem/15831
서브태스크문제는 채점이 재밌는 방식으로 되네요.
큐를 사용해서 쉽게 풀 수 있는 문제였습니다. 주의 해야되는 점은
1. W+B<=N
2. W는 하얀색 조약돌의 최소 개수.
였습니다.
'알고리즘 > BAEKJOON' 카테고리의 다른 글
백준 16462번 나교수님의 악필 (0) | 2019.01.08 |
---|---|
백준 2468번 안전 영역 (0) | 2019.01.05 |
백준 2583번 영역 구하기 (0) | 2019.01.04 |
백준 16466번 콘서트 (0) | 2019.01.03 |
백준 1193번 분수 찾기 (0) | 2019.01.03 |