結果

問題 No.430 文字列検索
ユーザー yuppe19 😺
提出日時 2019-04-21 21:49:37
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,245 bytes
コンパイル時間 666 ms
コンパイル使用メモリ 80,980 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-11-10 00:23:50
合計ジャッジ時間 1,382 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

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