結果

問題 No.430 文字列検索
ユーザー tonyu0tonyu0
提出日時 2019-11-01 21:46:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 1,344 bytes
コンパイル時間 763 ms
コンパイル使用メモリ 90,900 KB
実行使用メモリ 8,048 KB
最終ジャッジ日時 2023-10-13 00:59:50
合計ジャッジ時間 2,853 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 56 ms
8,048 KB
testcase_02 AC 50 ms
4,644 KB
testcase_03 AC 55 ms
4,764 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 48 ms
4,348 KB
testcase_09 AC 2 ms
4,356 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 63 ms
6,648 KB
testcase_12 AC 59 ms
7,012 KB
testcase_13 AC 60 ms
7,076 KB
testcase_14 AC 58 ms
6,248 KB
testcase_15 AC 52 ms
5,268 KB
testcase_16 AC 54 ms
5,328 KB
testcase_17 AC 53 ms
5,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <iomanip>
#include <cmath>
#include <map>
using namespace std;
using ll = long long;

class Trie {
  public:
    Trie(){ root = makeNode(); }

    void insert(string s) {
      Node* now = root;
      for(int i = 0; i < (int)s.size(); ++i) {
        int next = s[i] - 'A';
        if(now->child[next] == nullptr) now->child[next] = makeNode();
        now = now->child[next];
      }
      now->end = true;
    }

    int search(string s) {
      Node* now = root;
      int ret = 0;
      for(int i = 0; i < (int)s.size(); ++i) {
        ret += now->end;
        int next = s[i] - 'A';
        if(now->child[next] == nullptr) return ret;
        now = now->child[next];
      }
      return ret + now->end;
    }
  private:
    struct Node {
      Node* child[26];
      bool end;
    };

    Node* root;
    Node* makeNode() {
      Node* node = new Node;
      node->end = false;
      for(int i = 0; i < 26; ++i) node->child[i] = nullptr;
      return node;
    }
};

string S, T;
int N;
int main() {
  cin >> S >> N;
  Trie trie;
  for(int i = 0; i < N; ++i) {
    cin >> T;
    trie.insert(T);
  }

  int ans = 0;
  for(int i = 0; i < (int)S.size(); ++i) {
    ans += trie.search(S.substr(i, (int)S.size() - i));
  }
  cout << ans << endl;
  return 0;
}
0