hades

[Baekjoon] 5525번: IOIOI 본문

👊 PS/Algorithm

[Baekjoon] 5525번: IOIOI

hades1 2024. 7. 15. 12:00

🥅 문제

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

 

🔍 설계

IOIOIOIOI에서 n=1이라면 4, n=2라면 3, n=3이라면 2, n=4라면 1이 된다.

IOI의 개수가 4이므로, 결과값은 IOI 개수 + 1 - n이다.

 

👊 풀이

#include <iostream>
#include <string>
#include <deque>
using namespace std;

int n, m;
string s;
int result = 0;

int main(void)
{
	cin >> n >> m >> s;
	for (int i = 0; i < m; i++) {
		int count = 0;
		if (s[i] == 'I') {
			while (true) {
				if (s[i + 1] == 'O' && s[i + 2] == 'I') {
					count += 1;
					i = i + 2;
				}
				else {
					break;
				}
			}
		}
		if (count+1-n>0){
			result += count+1-n;
		}
	}
	cout << result << "\n";
	return 0;
}

 

📝 메모

IOI의 개수보다 n이 큰 경우, 음수가 되기 때문에 조건을 추가해야 한다.

'👊 PS > Algorithm' 카테고리의 다른 글

[Baekjoon] 7662번: 이중 우선순위 큐  (0) 2024.07.17
[Baekjoon] 7569번: 토마토  (0) 2024.07.16
[Baekjoon] 5430번: AC  (0) 2024.07.15
[Baekjoon] 2667번: 단지번호붙이기  (0) 2024.07.13
[Baekjoon] 2630번: 색종이 만들기  (0) 2024.07.11