結果

問題 No.430 文字列検索
ユーザー aaaaaaaa
提出日時 2019-03-03 04:10:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,424 bytes
コンパイル時間 588 ms
コンパイル使用メモリ 70,864 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 17:37:49
合計ジャッジ時間 12,412 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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 + i};
    }
  }
  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++;
    } else {
      if (next_pos == -1) {
        break;
      }
    }
  }

  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 << std::endl;
}
0