結果

問題 No.430 文字列検索
ユーザー 193s193s
提出日時 2017-05-17 00:31:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 2,590 bytes
コンパイル時間 1,250 ms
コンパイル使用メモリ 102,228 KB
実行使用メモリ 14,520 KB
最終ジャッジ日時 2023-10-17 12:35:16
合計ジャッジ時間 2,425 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 33 ms
14,520 KB
testcase_02 AC 7 ms
6,328 KB
testcase_03 AC 8 ms
6,328 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 23 ms
10,732 KB
testcase_12 AC 27 ms
11,976 KB
testcase_13 AC 26 ms
11,972 KB
testcase_14 AC 17 ms
9,836 KB
testcase_15 AC 12 ms
7,920 KB
testcase_16 AC 12 ms
7,916 KB
testcase_17 AC 12 ms
7,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
  vector<vector<int> > par;

  Trie() {
    V.pb(vector<int>(26+1));
    par.pb(vector<int>(26+1, 0));
  }

  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));
        par.pb(vector<int>(26+1, -1));
      }
      cur = V[cur][c+1];
    }
  }

  int back(int s, int c) {
    if (par[s][c] != -1) return par[s][c];
    if (V[s][c+1] != 0) return s;
    return par[s][c] = back(V[s][0], c);
  }
};

class ACMatch {
public:
  Trie t;
  vector<int> acc;
  vector<P> flow[26];
  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]];
      }
    }
    rep(c, 26) {
      rep(s, acc.size()) {
        int ns = t.back(s, c);
        ns = t.V[ns][c+1];
        flow[c].pb(P(ns, acc[ns]));
      }
    }
  }

  P move(int s, int c) {
    return flow[c][s];
  }

  int match(string S) {
    int sum = 0, cur = 0;
    for (char c : S) {
      P p = move(cur, c-'a');
      cur = p._1;
      sum += p._2;
    }
    return sum;
  }
};

string S;
int M;

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> S >> M;
  for (char &c : S) c += 0x20;
  ACMatch acm;
  rep(i, M) {
    string s;
    cin >> s;
    for (char &c : s) c += 0x20;
    acm.add_str(s);
  }
  acm.build();
  cout << acm.match(S) << "\n";
  return 0;
}
0