結果

問題 No.430 文字列検索
ユーザー minatominato
提出日時 2020-04-06 16:59:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 3,265 bytes
コンパイル時間 2,916 ms
コンパイル使用メモリ 198,676 KB
実行使用メモリ 20,096 KB
最終ジャッジ日時 2023-09-20 18:26:00
合計ジャッジ時間 4,062 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 54 ms
16,460 KB
testcase_02 AC 18 ms
11,848 KB
testcase_03 AC 17 ms
11,968 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 50 ms
17,880 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 5 ms
4,376 KB
testcase_11 AC 52 ms
19,152 KB
testcase_12 AC 55 ms
18,936 KB
testcase_13 AC 53 ms
18,876 KB
testcase_14 AC 49 ms
20,096 KB
testcase_15 AC 50 ms
19,956 KB
testcase_16 AC 39 ms
19,300 KB
testcase_17 AC 32 ms
19,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
#define ln '\n'
constexpr long long MOD = 1000000007LL;
//constexpr long long MOD = 998244353LL;
typedef long long ll;
typedef unsigned long long ull; 
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true;} return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true;} return false; }
///////////////////////////////////////////////////////////////////////////////////////////////////


template<class C>
struct SuffixAutomaton{
	struct State{
		int len,link;
		map<C,int> nex;
		State(int len,int link) : len(len), link(link) {}
	};
 
	vector<State> st;
    vector<long long> dp;
	int last;
 
	SuffixAutomaton() : last(0) {
		st.emplace_back(0,-1);
        dp.emplace_back(0);
	}
 
	int add(C c, long long val, int cur = -1) {
		if(cur == -1) {
			cur = last;
		}
 
		if(st[cur].nex.count(c) && st[st[cur].nex[c]].len == st[cur].len + 1) {
			int q = st[cur].nex[c];
            dp[q] += val;
			return last = q;
		}

        int p = cur;
		int nex = st.size();
		st.emplace_back(st[p].len+1,0);
        dp.emplace_back(val);
		while(p != -1 && !st[p].nex.count(c)) {
			st[p].nex[c] = nex;
			p = st[p].link;
		}
		if(p == -1){
            st[nex].link = 0;
			return last = nex;
		}
 
		int q = st[p].nex[c];
		if(st[p].len+1 == st[q].len) {
			st[nex].link = q;	
		} else {
			int clone = st.size();
			st.emplace_back(st[q]);
            dp.emplace_back(0);
			st[clone].len = st[p].len + 1;
 
			while (p != -1 && st[p].nex[c] == q) {
				st[p].nex[c] = clone;
				p = st[p].link;
			}
            st[q].link = st[nex].link = clone;
		}
		return last = nex;
	}
 
	
	vector<int> topologicalsort() {
		vector<int> ret;
		vector<int> deg(st.size());

		for(int i=0; i < st.size() ; i++) {
			for(auto &p:st[i].nex) deg[p.second]++;
		}
		queue<int> que;
		que.emplace(0);
		while(!que.empty()) {
			int v = que.front();
			que.pop();
			ret.emplace_back(v);
			for(auto &p : st[v].nex){
				if(--deg[p.second] == 0) que.emplace(p.second);
			}
		}
		return ret;
	}
 
	vector<int> suffixtree() {
		vector<int> ret;
 
		vector<vector<int>> G(st.size());
		for(int i = 1; i < st.size() ; i++) G[st[i].link].emplace_back(i);
		queue<int> que;
		que.emplace(0);
		while(!que.empty()) {
			int v = que.front();
			que.pop();
			ret.emplace_back(v);
			for(auto nv : G[v]) que.push(nv);
		}
		return ret;
	}
};

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    string S; cin >> S;
    int N = S.size();
    SuffixAutomaton<char> SA;
    rep(i,N) SA.add(S[i],1);
    auto A = SA.suffixtree();
    for (int i = A.size()-1; i > 0; i--) SA.dp[SA.st[A[i]].link] += SA.dp[A[i]];

    int M; cin >> M;
    ll ans = 0;
    while (M--) {
        string T; cin >> T;
        int L = T.size();
        int cur = 0;
        rep(i,L) {
            if (!SA.st[cur].nex.count(T[i])) {
                cur = -1;
                break;
            }
            cur = SA.st[cur].nex[T[i]];
        }
        ans += cur == -1 ? 0 : SA.dp[cur];
    }

    cout << ans << ln;
}
0