結果

問題 No.430 文字列検索
ユーザー aaaaaaaa
提出日時 2019-03-12 02:42:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,355 bytes
コンパイル時間 546 ms
コンパイル使用メモリ 70,012 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-05 20:19:22
合計ジャッジ時間 17,397 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 277 ms
4,376 KB
testcase_02 TLE -
testcase_03 AC 1,189 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1,729 ms
4,376 KB
testcase_12 AC 1,724 ms
4,376 KB
testcase_13 AC 1,725 ms
4,376 KB
testcase_14 TLE -
testcase_15 AC 1,923 ms
4,380 KB
testcase_16 AC 1,511 ms
4,376 KB
testcase_17 AC 1,476 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <utility>

using namespace std;

int find_next_head(const std::string& str, const std::string& ptn, int begin) {
  for (uint i = begin; i < str.size(); ++i) {
    if (str[i] == ptn[0]) {
      return i;
    }
  }
  return -1;
}

std::pair<bool, int> find_next_substr(const std::string& str,
                                      const std::string& ptn, int begin) {
  int pos = find_next_head(str, ptn, begin);
  if (pos == -1 || str.size() < begin + ptn.size()) {
    return {false, -1};
  }

  for (uint i = 0; i < ptn.size(); ++i) {
    if (str[pos + i] != ptn[i]) {
      return {false, pos + 1};
    }
  }
  return {true, pos + 1};
}

int count_substr(const std::string& str, const std::string& ptn) {
  int count = 0;

  std::pair<bool, int> m = find_next_substr(str, ptn, 0);
  bool found = m.first;
  int next_pos = m.second;

  if (found) {
    count++;
  }

  while (next_pos != -1) {
    m = find_next_substr(str, ptn, next_pos);
    found = m.first;
    next_pos = m.second;
    if (found) {
      count++;
    }
  }

  return found ? count + 1 : count;
}

int main() {
  string S, *C;
  int M;
  cin >> S >> M;
  C = new string[M];
  for (int i = 0; i < M; ++i) {
    cin >> C[i];
  }

  int sum = 0;
  for (int i = 0; i < M; ++i) {
    sum += count_substr(S, C[i]);
  }
  cout << sum << endl;
}
0