結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
goodbaton
|
| 提出日時 | 2018-06-18 01:41:35 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 9 ms / 2,000 ms |
| コード長 | 2,152 bytes |
| コンパイル時間 | 776 ms |
| コンパイル使用メモリ | 93,844 KB |
| 実行使用メモリ | 8,448 KB |
| 最終ジャッジ日時 | 2024-11-10 00:19:21 |
| 合計ジャッジ時間 | 1,221 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:97:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
97 | scanf("%s%d", s, &m);
| ~~~~~^~~~~~~~~~~~~~~
main.cpp:100:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
100 | scanf("%s", p);
| ~~~~~^~~~~~~~~
ソースコード
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <cassert>
typedef long long ll;
using namespace std;
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010
/* Aho Corasick */
struct ACNode{
int val;
ACNode *next[26], *failure;
ACNode():val(0) { memset(next,0,sizeof(next)); }
void insert(char *s){
if(!*s){ val++; return; }
int al = *s-'A';
if(next[al]==NULL) next[al] = new ACNode;
next[al]->insert(s+1);
}
ACNode *nextNode(char c){
int al = c - 'A';
if (next[al]) return next[al];
return failure == this ? this : failure->nextNode(c);
}
};
struct AhoCorasick{
ACNode *node;
AhoCorasick(){node = new ACNode;}
void insert(char *s) {
node->insert(s);
}
void build() {
queue<ACNode*> que;
que.push(node);
node->failure = node;
while(que.size()){
ACNode *p = que.front();
que.pop();
for(int i=0;i<26;i++){
if(p->next[i]){
ACNode *failure = p->failure;
while(!failure->next[i] && failure != node){
failure = failure->failure;
}
if (failure->next[i] && failure != p){
p->next[i]->failure = failure->next[i];
p->next[i]->val += failure->next[i]->val;
}else{
p->next[i]->failure = node;
}
que.push(p->next[i]);
}
}
}
}
};
int main(){
char s[SIZE], p[SIZE];
int m;
AhoCorasick ac;
scanf("%s%d", s, &m);
for(int i=0;i<m;i++){
scanf("%s", p);
ac.insert(p);
}
ac.build();
auto now = ac.node;
int ans = 0;
for(int i=0;s[i];i++){
now = now->nextNode(s[i]);
ans += now->val;
}
cout << ans << endl;
return 0;
}
goodbaton