안녕하세요. 오늘은 코리아 문자열을 만들거예요.

문제

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

아이디어

그리디하게 KOREA를 세면 됩니다.
idx를 0부터 시작해서 현재 문자가 idx%5번째 문자이면 idx++를 합니다. 맨 마지막에 idx가 정답이 됩니다.

소스코드

#include <string>
using namespace std;

int main(void)
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    string s, KOREA = "KOREA";
    int idx = 0;

    cin >> s;
    for (char c : s) if (c == KOREA[idx % 5]) idx++;
    cout << idx;
}


감사합니다.

0개의 댓글