結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
ferin
|
| 提出日時 | 2018-10-05 13:38:09 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 16 ms / 2,000 ms |
| コード長 | 3,595 bytes |
| コンパイル時間 | 1,633 ms |
| コンパイル使用メモリ | 174,832 KB |
| 実行使用メモリ | 10,260 KB |
| 最終ジャッジ日時 | 2024-11-10 00:20:11 |
| 合計ジャッジ時間 | 2,285 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using PII = pair<int, int>;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using VVV = vector<VV<T>>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back
const ll INF = (1LL<<60);
const int MOD = 1000000007;
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
out<<'('<<a.first<<','<<a.second<<')';
return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
out<<'[';
REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
out<<']';
return out;
}
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
// 文字の種類数をtemplate引数で渡す
template <int types = 26>
struct AhoCorasick {
// trie木のnode
struct node {
node *fail;
V<node*> next;
V<int> matched;
node() : fail(nullptr), next(types, nullptr) {}
};
// 辞書のサイズ
int sz;
// trie木の根
node *root;
// vectorを結合
V<int> unite(const V<int> &a, const V<int> &b) {
V<int> ret;
set_union(ALL(a), ALL(b), back_inserter(ret));
return ret;
}
// 文字と数字の対応付けをする関数
function<int(char)> trans;
// 初期化
AhoCorasick() {}
AhoCorasick(V<string> pattern, function<int(char)> f = [](char c){return c-'a';}) {
trans = f;
build(pattern);
}
// 文字列集合patternからtrie木っぽいオートマトンを作成
void build(V<string> pattern) {
sz = pattern.size(), root = new node;
node *now;
root->fail = root;
REP(i, sz) {
now = root;
for(const auto &c: pattern[i]) {
if(now->next[trans(c)] == nullptr) {
now->next[trans(c)] = new node;
}
now = now->next[trans(c)];
}
now->matched.PB(i);
}
queue<node*> que;
REP(i, types) {
if(root->next[i] == nullptr) {
root->next[i] = root;
} else {
root->next[i]->fail = root;
que.push(root->next[i]);
}
}
while(que.size()) {
now = que.front(); que.pop();
REP(i, types) {
if(now->next[i] != nullptr) {
node *nxt = now->fail;
while(!nxt->next[i]) nxt = nxt->fail;
now->next[i]->fail = nxt->next[i];
now->next[i]->matched = unite(now->next[i]->matched, nxt->next[i]->matched);
que.push(now->next[i]);
}
}
}
}
// 一文字ずつ照合していく
node* next(node* p, const char c) {
while(p->next[trans(c)] == nullptr) p = p->fail;
return p->next[trans(c)];
}
// 文字列s中に辞書と一致する部分列がどれだけあるか
V<int> match(const string s) {
V<int> res(sz);
node *now = root;
for(auto c : s) {
now = next(now, c);
for(auto i : now->matched) res[i]++;
}
return res;
}
};
signed main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
string s;
cin >> s;
int n;
cin >> n;
V<string> v(n);
REP(i, n) cin >> v[i];
AhoCorasick<26> aho(v, [&](char c){ return c-'A'; });
auto ret = aho.match(s);
int ans = 0;
for(int i: ret) ans += i;
cout << ans << endl;
return 0;
}
ferin