結果
| 問題 | No.430 文字列検索 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-10-02 22:45:17 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 10 ms / 2,000 ms |
| コード長 | 1,320 bytes |
| 記録 | |
| コンパイル時間 | 1,626 ms |
| コンパイル使用メモリ | 170,456 KB |
| 実行使用メモリ | 6,216 KB |
| 最終ジャッジ日時 | 2024-11-10 00:02:13 |
| 合計ジャッジ時間 | 2,345 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct AhoCorasick {
vector<vector<int>> succ;
vector<int> fail, accept;
char from, to;
AhoCorasick(char from = 'a', char to = 'z') :
from(from),
to(to),
succ(1, vector<int>(to - from + 1)),
accept(1) {
}
void add(string s) {
int curr = 0;
for (char c : s) {
c -= from;
if (succ[curr][c] == 0) {
succ[curr][c] = succ.size();
succ.push_back(vector<int>(to - from + 1));
accept.push_back(0);
}
curr = succ[curr][c];
}
accept[curr]++;
}
void build() {
queue<int> q;
for (int c = 0; c <= to - from; c++) {
if (succ[0][c] != 0) {
q.push(succ[0][c]);
}
}
fail.resize(succ.size());
while (!q.empty()) {
int curr = q.front(); q.pop();
for (int c = 0; c <= to - from; c++) {
int next = succ[curr][c];
if (next != 0) {
q.push(next);
fail[next] = succ[fail[curr]][c];
} else {
succ[curr][c] = succ[fail[curr]][c];
}
}
accept[curr] += accept[fail[curr]];
}
}
};
int main() {
string S;
cin >> S;
int m;
cin >> m;
AhoCorasick aho('A', 'Z');
for (int ii = 0; ii < m; ii++) {
string c;
cin >> c;
aho.add(c);
}
aho.build();
int v = 0;
int ans = 0;
for (char ch : S) {
v = aho.succ[v][ch - 'A'];
ans += aho.accept[v];
}
cout << ans << endl;
}