結果

問題 No.430 文字列検索
ユーザー tonyu0tonyu0
提出日時 2020-09-09 12:39:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 1,440 bytes
コンパイル時間 1,258 ms
コンパイル使用メモリ 119,216 KB
実行使用メモリ 8,504 KB
最終ジャッジ日時 2023-08-21 08:50:29
合計ジャッジ時間 2,957 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,272 KB
testcase_01 AC 55 ms
8,420 KB
testcase_02 AC 56 ms
8,476 KB
testcase_03 AC 54 ms
8,436 KB
testcase_04 AC 6 ms
8,408 KB
testcase_05 AC 6 ms
8,312 KB
testcase_06 AC 5 ms
8,268 KB
testcase_07 AC 5 ms
8,408 KB
testcase_08 AC 50 ms
8,496 KB
testcase_09 AC 5 ms
8,392 KB
testcase_10 AC 6 ms
8,300 KB
testcase_11 AC 55 ms
8,432 KB
testcase_12 AC 57 ms
8,372 KB
testcase_13 AC 56 ms
8,372 KB
testcase_14 AC 54 ms
8,432 KB
testcase_15 AC 54 ms
8,352 KB
testcase_16 AC 54 ms
8,420 KB
testcase_17 AC 55 ms
8,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
#define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i)

template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T>& a) {
  os << "{";
  for (size_t i = 0; i < a.size(); ++i) os << (i > 0 ? "," : "") << a[i];
  return os << "}";
}

class trie {
public:
  trie(size_t max_size) {
    size = 1; // only root
    next.assign(26, vector<int>(max_size, -1));
    end.assign(max_size, false);
  }

  int search(string s) {
    int now = 0;
    int res = 0;
    for (char c : s) {
      if (next[c - 'A'][now] == -1) return res + end[now];
      res += end[now];
      now = next[c - 'A'][now];
    }
    return end[now] + res;
  }
  void insert(string s) {
    int now = 0;
    for (char c : s) {
      if (next[c - 'A'][now] == -1) next[c - 'A'][now] = size++;
      now = next[c - 'A'][now];
    }
    end[now] = true;
  }

private:
  vector<vector<int>> next;
  vector<bool> end;
  int size;
};


int main() {
  string s, t;
  int n;
  cin >> s >> n;
  trie tr(50005);
  rep(i, 0, n) {
    cin >> t;
    tr.insert(t);
  }

  int ans = 0;
  rep(i, 0, s.size()) { ans += tr.search(s.substr(i, s.size() - i)); }
  cout << ans << '\n';
  return 0;
}
0