結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-05-16 21:57:08 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 10 ms / 2,000 ms |
| コード長 | 2,112 bytes |
| コンパイル時間 | 1,039 ms |
| コンパイル使用メモリ | 97,660 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-11-10 00:15:21 |
| 合計ジャッジ時間 | 1,693 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
class Trie {
public:
vector<vector<int> > V;
Trie() {
V.pb(vector<int>(26+1));
}
int find(string s) {
int cur = 0;
for (char ch : s) {
int c = ch - 'A';
cur = V[cur][c+1];
if (cur == 0) return -1;
}
return cur;
}
void insert(string s) {
int cur = 0;
for (char ch : s) {
int c = ch - 'A';
if (V[cur][c+1] == 0) {
V[cur][c+1] = V.size();
V.pb(vector<int>(26+1));
}
cur = V[cur][c+1];
}
}
};
class ACMatch {
public:
Trie t;
vector<int> acc;
vector<string> S;
void add_str(string s) {
t.insert(s);
S.pb(s);
}
void build() {
acc.resize(t.V.size(), 0);
for (string s : S) acc[t.find(s)]++;
queue<int> q;
rep(i, 26) {
if (!t.V[0][i+1]) continue;
t.V[t.V[0][i+1]][0] = 0;
q.push(t.V[0][i+1]);
}
while (!q.empty()) {
int k = q.front(); q.pop();
rep(i, 26) {
if (!t.V[k][i+1]) continue;
q.push(t.V[k][i+1]);
int pre = t.V[k][0];
while (pre && t.V[pre][i+1]==0) pre = t.V[pre][0];
t.V[t.V[k][i+1]][0] = t.V[pre][i+1];
acc[t.V[k][i+1]] += acc[t.V[pre][i+1]];
}
}
}
int match(string S) {
int sum = 0, cur = 0;
for (char ch : S) {
int c = ch - 'A';
while (cur && t.V[cur][c+1]==0) cur=t.V[cur][0];
cur = t.V[cur][c+1];
sum += acc[cur];
}
return sum;
}
};
string S;
int M;
signed main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> S >> M;
ACMatch acm;
rep(i, M) {
string s;
cin >> s;
acm.add_str(s);
}
acm.build();
cout << acm.match(S) << "\n";
return 0;
}