結果

問題 No.430 文字列検索
ユーザー yuppe19 😺yuppe19 😺
提出日時 2019-04-21 21:49:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 1,245 bytes
コンパイル時間 1,884 ms
コンパイル使用メモリ 80,388 KB
実行使用メモリ 7,424 KB
最終ジャッジ日時 2024-04-16 18:24:35
合計ジャッジ時間 1,905 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 29 ms
7,424 KB
testcase_02 AC 7 ms
5,376 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 23 ms
6,016 KB
testcase_12 AC 23 ms
6,272 KB
testcase_13 AC 23 ms
6,060 KB
testcase_14 AC 14 ms
5,376 KB
testcase_15 AC 9 ms
5,376 KB
testcase_16 AC 8 ms
5,376 KB
testcase_17 AC 8 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <vector>
using namespace std;

class Trie {
  struct Node {
    int x;
    map<char, Node*> chi;
    Node() : x(0) {}
    ~Node() { for(auto kv : chi) { delete kv.second; } }
    void insert(const string &s) {
      Node *cur = this;
      for(size_t i=0, n=s.size(); i<n; ++i) {
        Node **nxt = &(cur->chi[s[i]]);
        if(*nxt == nullptr) { *nxt = new Node; }
        cur = *nxt;
      }
      ++(cur->x);
    }
    int calc(const string &s) {
      Node *cur = this;
      int res = 0;
      for(char c : s) {
        Node **nxt = &(cur->chi[c]);
        if(*nxt == nullptr) { break; }
        cur = *nxt;
        res += cur->x;
      }
      return res;
    }
  };
 public:
  Node *root;
  Trie() { root = new Node; }
  ~Trie() { delete root; }
  void insert(const string &s) { root->insert(s); }
  int calc(const string &s) { return root->calc(s); }
};

int main(void) {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  string s; cin >> s;
  int M; cin >> M;
  Trie tree;
  for(int i=0; i<M; ++i) {
    string ci; cin >> ci;
    tree.insert(ci);
  }
  int res = 0;
  for(size_t k=0, n=s.size(); k<n; ++k) {
    res += tree.calc(s.substr(k, 10));
  }
  cout << res << '\n';
  return 0;
}
0