結果
問題 |
No.430 文字列検索
|
ユーザー |
|
提出日時 | 2019-11-01 21:46:42 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 62 ms / 2,000 ms |
コード長 | 1,344 bytes |
コンパイル時間 | 858 ms |
コンパイル使用メモリ | 92,500 KB |
実行使用メモリ | 8,064 KB |
最終ジャッジ日時 | 2024-11-10 00:36:22 |
合計ジャッジ時間 | 1,857 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 14 |
ソースコード
#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; }