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


pos ← 0
cnt ← 0
n ← |s| # 문자열 길이
while pos < n do
cnt ← cnt + 1
len ← min(3, n − pos)
loop # 이 안에서 “한 조각”을 확정할 때까지 반복
# 1) 새 조각이 '0'으로 시작하면 → 잘못 끊어진 경계이므로
if s[pos] == '0' then
pos ← pos − 1
len ← min(3, n − pos)
continue # 다시 같은 조각에서 재시도
end if
# 2) s[pos..pos+len−1] 을 숫자로 변환
num ← 0
for i in 0 … len−1 do
num ← num * 10 + (s[pos+i] − '0')
end for
# 3) 1 ≤ num ≤ 641 이면 이 길이로 확정하고 다음 조각으로
if num ≤ 641 then
pos ← pos + len
break # while‐loop 빠져나가서 다음 조각 시작
else
len ← len − 1 # 너무 크면 한 글자 줄여 다시 검사
end if
end loop
end while
print cnt
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
string s;
cin >> s;
int pos = 0;
int cnt = 0;
while(pos < int(s.length())){
cnt++;
int len = min(3, N - pos);
while(1){
int num = s[pos] - '0';
if(num == 0){
pos--;
len = min(3, N - pos);
continue;
}
for(int i = pos + 1; i < pos + len; i++){
num = num * 10 + (s[i] - '0');
}
if(num <= 641){
pos += len;
break;
} else len--;
}
}
cout << cnt;
return 0;
}