작성일 :

문제 링크

9771번 - Word Searching

설명

첫 줄의 단어가 이후 모든 줄에서 부분 문자열로 몇 번 등장하는지 세는 문제입니다. 겹치는 경우도 각각 세며, 대소문자를 구분합니다.


접근법

각 줄에서 단어를 찾을 때마다 발견 위치 다음부터 다시 검색하여 겹치는 경우도 셉니다. 모든 줄에서 발견한 횟수를 합산해 출력합니다.


Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;

class Program {
  static void Main() {
    var word = Console.ReadLine()!;
    var ans = 0;

    string? line;
    while ((line = Console.ReadLine()) != null) {
      for (var pos = 0; ; pos++) {
        var idx = line.IndexOf(word, pos, StringComparison.Ordinal);
        if (idx == -1) break;
        ans++;
        pos = idx;
      }
    }

    Console.WriteLine(ans);
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
using namespace std;

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

  string word; getline(cin, word);
  int ans = 0;

  string line;
  while (getline(cin, line)) {
    int pos = 0;
    while (true) {
      int idx = line.find(word, pos);
      if (idx == (int)string::npos) break;
      ans++;
      pos = idx + 1;
    }
  }

  cout << ans << "\n";

  return 0;
}