주어진 문자열을 탐색하면서 n에 해당하는 IO패턴의 개수를 카운팅한 후 출력한다.
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
string str;
vector<int> res;
int result = 0;
cin >> n >> m >> str;
int count = 0;
bool pass = true;
for (int i = 0; i < str.size(); i++){
if ((pass && str[i] == 'I') || (!pass && str[i] == 'O'))
{
count++;
pass = !pass;
}
else if(str[i] == 'I')
{
if(2*n+1 <= count){
if(str[i]=='O')
count--;
res.push_back(count);
}
count = 1;
pass = false;
}else{
if(2*n+1 <= count){
if(str[i]=='O')
count--;
res.push_back(count);
}
count = 0;
pass = true;
}
}
if(2*n+1 <= count)
res.push_back(count);
for (int i = 0; i < res.size(); i++){
result += (res[i] - (n * 2 - 1)) / 2;
}
cout << result << "\n";
return 0;
}
'알고리즘 문제풀이' 카테고리의 다른 글
[백준] 11403번 경로 찾기 c++ (0) | 2022.12.25 |
---|---|
[백준] 14500 테트로미노 c++ (0) | 2022.12.24 |
[백준] 5430번 AC c++ (0) | 2022.12.10 |
[백준] 10026번 적록색양 c++ (0) | 2022.11.30 |
[백준] 1992번 쿼드트리 c++ (0) | 2022.11.29 |