結果
問題 |
No.430 文字列検索
|
ユーザー |
|
提出日時 | 2020-09-09 12:39:49 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 76 ms / 2,000 ms |
コード長 | 1,440 bytes |
コンパイル時間 | 1,227 ms |
コンパイル使用メモリ | 115,636 KB |
最終ジャッジ日時 | 2025-01-14 08:50:04 |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 14 |
ソースコード
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <tuple> #include <vector> using namespace std; using ll = int64_t; #define rep(i, j, n) for (int i = j; i < (int)n; ++i) #define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i) template <typename T> std::ostream& operator<<(std::ostream& os, std::vector<T>& a) { os << "{"; for (size_t i = 0; i < a.size(); ++i) os << (i > 0 ? "," : "") << a[i]; return os << "}"; } class trie { public: trie(size_t max_size) { size = 1; // only root next.assign(26, vector<int>(max_size, -1)); end.assign(max_size, false); } int search(string s) { int now = 0; int res = 0; for (char c : s) { if (next[c - 'A'][now] == -1) return res + end[now]; res += end[now]; now = next[c - 'A'][now]; } return end[now] + res; } void insert(string s) { int now = 0; for (char c : s) { if (next[c - 'A'][now] == -1) next[c - 'A'][now] = size++; now = next[c - 'A'][now]; } end[now] = true; } private: vector<vector<int>> next; vector<bool> end; int size; }; int main() { string s, t; int n; cin >> s >> n; trie tr(50005); rep(i, 0, n) { cin >> t; tr.insert(t); } int ans = 0; rep(i, 0, s.size()) { ans += tr.search(s.substr(i, s.size() - i)); } cout << ans << '\n'; return 0; }