結果

問題 No.430 文字列検索
ユーザー goodbatongoodbaton
提出日時 2018-06-18 01:41:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 2,152 bytes
コンパイル時間 722 ms
コンパイル使用メモリ 91,744 KB
実行使用メモリ 8,584 KB
最終ジャッジ日時 2023-09-13 06:34:29
合計ジャッジ時間 1,754 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 10 ms
8,584 KB
testcase_02 AC 4 ms
4,704 KB
testcase_03 AC 4 ms
4,692 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 8 ms
6,768 KB
testcase_12 AC 10 ms
7,536 KB
testcase_13 AC 9 ms
7,536 KB
testcase_14 AC 7 ms
6,404 KB
testcase_15 AC 5 ms
5,448 KB
testcase_16 AC 5 ms
5,468 KB
testcase_17 AC 5 ms
5,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0