結果

問題 No.430 文字列検索
ユーザー tonyu0tonyu0
提出日時 2024-05-16 14:28:23
言語 C++23(gcc13)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 1,791 bytes
コンパイル時間 3,687 ms
コンパイル使用メモリ 113,060 KB
実行使用メモリ 8,448 KB
最終ジャッジ日時 2024-05-16 14:28:29
合計ジャッジ時間 5,363 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 59 ms
8,448 KB
testcase_02 AC 51 ms
6,940 KB
testcase_03 AC 52 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 47 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 59 ms
6,940 KB
testcase_12 AC 59 ms
7,296 KB
testcase_13 AC 58 ms
7,296 KB
testcase_14 AC 57 ms
6,944 KB
testcase_15 AC 46 ms
6,944 KB
testcase_16 AC 47 ms
6,940 KB
testcase_17 AC 48 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, j, n) for (ll i = j; i < (n); ++i)
#define rrep(i, j, n) for (ll i = (n) - 1; j <= i; --i)
#define all(a) a.begin(), a.end()
template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &a) {
  for (size_t i = 0; i < a.size(); ++i) os << (i > 0 ? " " : "") << a[i];
  return os << '\n';
}
template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &a) {
  for (T &x : a) { is >> x; }
  return is;
}

template <int set_size = 26, char base_char = 'a'>
class trie {
  struct Node {
    Node *next[set_size] = {nullptr};
    bool isleaf = false;
  };

public:
  trie() : nodes(1, new Node) {} // add the root node
  int ans = 0;
  bool search(const std::string &s) {
    Node *now = nodes[0];
    for (const char &c : s) {
      int i = c - base_char;
      ans += now->isleaf;
      if (!now->next[i]) { return false; }
      now = now->next[i];
    }
    ans += now->isleaf;
    return now->isleaf;
  }
  void insert(const std::string &s) {
    Node *now = nodes[0];
    for (const char &c : s) {
      int i = c - base_char;
      if (!now->next[i]) {
        nodes.push_back(new Node);
        now->next[i] = nodes.back();
      }
      // now->next[c]->count++;
      now = now->next[i];
    }
    now->isleaf = true;
  }

private:
  std::vector<Node *> nodes;
};

int main() {
  cin.tie(0)->sync_with_stdio(0);
  string s, t;
  int n;
  cin >> s >> n;

  trie<26, 'A'> trie;
  for (int i = 0; i < n; ++i) {
    cin >> t;
    trie.insert(t);
  }
  for (int i = 0; i < (int)s.size(); ++i)
    trie.search(s.substr(i, (int)s.size() - i));
  cout << trie.ans << endl;
}
0