結果
問題 |
No.430 文字列検索
|
ユーザー |
![]() |
提出日時 | 2017-07-21 23:43:42 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 51 ms / 2,000 ms |
コード長 | 2,487 bytes |
コンパイル時間 | 6,619 ms |
コンパイル使用メモリ | 165,540 KB |
最終ジャッジ日時 | 2025-01-05 01:44:19 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
純コード判定しない問題か言語 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 14 |
コンパイルメッセージ
main.cpp: In function 'int main()': main.cpp:88:24: warning: 'matches' may be used uninitialized [-Wmaybe-uninitialized] 88 | cout << aho.match(s) << endl; | ^ main.cpp:64:13: note: 'matches' was declared here 64 | int matches; | ^~~~~~~
ソースコード
#include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <map> #include <set> #include <queue> #include <algorithm> #include <iomanip> #include <cassert> using namespace std; #define GET_ARG(a,b,c,F,...) F #define REP3(i,s,e) for (i = s; i <= e; i++) #define REP2(i,n) REP3 (i,0,(int)(n)-1) #define REP(...) GET_ARG (__VA_ARGS__,REP3,REP2) (__VA_ARGS__) #define RREP3(i,s,e) for (i = s; i >= e; i--) #define RREP2(i,n) RREP3 (i,(int)(n)-1,0) #define RREP(...) GET_ARG (__VA_ARGS__,RREP3,RREP2) (__VA_ARGS__) #define DEBUG(x) cerr << #x ": " << x << endl struct Aho { vector<map<char,int>> states; vector<int> fail_path; vector<unordered_set<string>> matched; Aho(vector<string> dict) { states.push_back({}); for (auto s: dict) { int now = 0; for (auto c: s) { if (!states[now].count(c)) { states[now][c] = states.size(); states.push_back({}); } now = states[now][c]; } matched.resize(states.size()); matched[now].insert(s); } fail_path.resize(states.size()); queue<int> q; q.push(0); while (!q.empty()) { auto now = q.front(); q.pop(); for (auto p: states[now]) { char c = p.first; int next = p.second; q.push(next); if (now == 0) continue; int fail = fail_path[now]; while (fail > 0 && !states[fail].count(c)) fail = fail_path[fail]; if (states[fail].count(c)) { int x = states[fail][c]; fail_path[next] = x; matched[next].insert(matched[x].begin(),matched[x].end()); } } } } int match(string s) { int matches; int now = 0; for (auto c: s) { while (now > 0 && !states[now].count(c)) now = fail_path[now]; if (states[now].count(c)) { now = states[now][c]; matches += matched[now].size(); } } return matches; } }; int main(void) { int i, j, k, m; string s; cin >> s >> m; vector<string> dict; while (m--) { string t; cin >> t; dict.push_back(t); } Aho aho = Aho(dict); cout << aho.match(s) << endl; return 0; }