작성일 :

문제 링크

7790번 - Joke

설명

입력 전체에서 “joke”가 부분 문자열로 몇 번 등장하는지 세는 문제입니다.


접근법

먼저 입력을 끝까지 읽어 하나의 문자열로 합칩니다.

다음으로 각 위치에서 “joke”가 시작되는지 확인하며 개수를 셉니다.

마지막으로 누적된 개수를 출력합니다.



Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
using System;

class Program {
  static void Main() {
    var text = Console.In.ReadToEnd();
    var cnt = 0;
    for (var i = 0; i + 3 < text.Length; i++) {
      if (text[i] == 'j' && text[i + 1] == 'o' && text[i + 2] == 'k' && text[i + 3] == 'e')
        cnt++;
    }
    Console.WriteLine(cnt);
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  string text((istreambuf_iterator<char>(cin)), istreambuf_iterator<char>());
  int cnt = 0;
  for (int i = 0; i + 3 < (int)text.size(); i++) {
    if (text[i] == 'j' && text[i + 1] == 'o' && text[i + 2] == 'k' && text[i + 3] == 'e')
      cnt++;
  }
  cout << cnt << "\n";

  return 0;
}