조금 더럽게 한 감이 없잖아 있다.
try를 3번 했는데,
1번째는 마지막 부분에서 고려 안한 부분,
2번째는 tempCount >= n의 조건일 때
3번째는 O이고 2일때도 2번째 조건을 삽입해야 한다는 점
그리고 std::string_view를 처음 써봤는데, 솔직히 string을 많이 다뤄야 할 때 중요할 듯 하다.
O(n)일 듯하고 공간 복잡도는 나름 사용한다.
#include <iostream>
#include <string>
int main() {
int n, m;
std::cin >> n >> m;
std::string s;
std::string_view sView;
std::cin >> s;
sView = s;
int count = 0;
int isPattern = 0; // 0이면 시작 안함, 1이면 i-1에서 I, 2면 i-1에서 O
int tempCount = 0;
for (int i = 0; i < m; i++) {
if (sView[i] == 'I' && !isPattern) {
isPattern = 1;
} else if (sView[i] == 'O' && isPattern == 1) {
isPattern = 2;
} else if (sView[i] == 'I' && isPattern == 2) {
tempCount++;
isPattern = 1;
} else if (sView[i] == 'I' && isPattern == 1) {
if (tempCount >= n) {
count += tempCount-n+1;
}
tempCount = 0;
isPattern = 1;
} else if (sView[i] == 'O' && isPattern == 2) {
if (tempCount >= n) {
count += tempCount-n+1;
}
isPattern = 0;
tempCount = 0;
}
}
if (tempCount >= n) {
count += tempCount-n+1;
}
std::cout << count << std::endl;
return 0;
}'코딩 > cpp' 카테고리의 다른 글
| [백준] 11403번 (0) | 2025.10.07 |
|---|---|
| [백준] 11286번 (0) | 2025.10.06 |
| [백준] 1389번 (0) | 2025.10.03 |
| [TWC 4.5Sota] Overloading vs. Overriding (0) | 2025.10.03 |
| c++ 공부 track (0) | 2025.10.03 |