結果

問題 No.430 文字列検索
ユーザー どららどらら
提出日時 2016-10-02 22:48:27
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 2,950 bytes
コンパイル時間 974 ms
コンパイル使用メモリ 96,564 KB
実行使用メモリ 8,840 KB
最終ジャッジ日時 2023-08-15 17:22:19
合計ジャッジ時間 1,941 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 25 ms
8,840 KB
testcase_02 AC 6 ms
5,120 KB
testcase_03 AC 6 ms
4,700 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 11 ms
5,612 KB
testcase_12 AC 11 ms
6,080 KB
testcase_13 AC 12 ms
5,828 KB
testcase_14 AC 9 ms
5,364 KB
testcase_15 AC 8 ms
5,116 KB
testcase_16 AC 8 ms
5,504 KB
testcase_17 AC 8 ms
5,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
#include <queue>
#include <list>
#include <stack>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
class AhoCorasick {

  struct Node {
    std::map<char,Node> child;
    std::vector<size_t> pattern;
    Node* failure;
    Node():failure(NULL){}
  };
  Node root;

  void clear_tree(){
    root.child.clear();
    root.pattern.clear();
  }

  void generate_trie(const std::vector<std::string>& patterns){
    size_t n = patterns.size();

    for(size_t i=0; i<n; i++){
      Node* t = &root;
      for(size_t j=0; j<patterns[i].length(); j++){
        t = &(t->child[patterns[i][j]]);
      }
      t->pattern.push_back(i);
    }
  }

  void add_failure_edge(){
    std::queue<Node*> que;
    que.push(&root);

    while(!que.empty()){
      Node* t = que.front(); que.pop();
      std::map<char,Node>::iterator itr = t->child.begin();

      for(; itr != t->child.end(); ++itr){
        que.push(&(itr->second));
        char c = itr->first;
        Node* node = &(itr->second);

        Node* anode = t->failure;
        while(anode != NULL && anode->child.count(c)==0) anode = anode->failure;

        if(anode == NULL){
          node->failure = &root;
        }else{
          node->failure = &(anode->child[c]);
        }

        for(size_t i=0; i<node->failure->pattern.size(); i++){
          node->pattern.push_back(node->failure->pattern[i]);
        }
        
        std::vector<size_t>(node->pattern).swap(node->pattern);

      }
    }
  }

  void make_PMA(const std::vector<std::string>& patterns){
    clear_tree();
    generate_trie(patterns);
    add_failure_edge();
  }
  
public:
  AhoCorasick(const std::vector<std::string>& patterns){
    make_PMA(patterns);
  }

  std::vector< std::pair<size_t,size_t> > find(const std::string& text){
    std::vector< std::pair<size_t,size_t> > ret;
    Node* node = &root;
    
    for(size_t i=0; i<text.length(); i++){
      while(node != NULL && node->child.count(text[i])==0) node = node->failure;

      if(node == NULL) node = &root;
      else node = &(node->child[text[i]]);

      for(size_t j=0; j<node->pattern.size(); j++){
        size_t e = i;
        size_t pi = node->pattern[j];
        ret.push_back(std::make_pair(e,pi));
      }
    }
    return ret;
  }
};

int main(){
  ios::sync_with_stdio(false);
  string S;
  cin >> S;
  int M;
  cin >> M;
  vector<string> v;
  rep(i,M){
    string tmp;
    cin >> tmp;
    v.push_back(tmp);
  }

  AhoCorasick ac(v);
  vector< pair<size_t,size_t> > ret = ac.find(S);

  cout << ret.size() << endl;
  
  return 0;
}

0