結果

問題 No.430 文字列検索
ユーザー leaf_1415leaf_1415
提出日時 2020-05-08 02:12:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 2,604 bytes
コンパイル時間 666 ms
コンパイル使用メモリ 82,576 KB
実行使用メモリ 7,904 KB
最終ジャッジ日時 2023-09-16 08:16:31
合計ジャッジ時間 1,547 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 9 ms
7,904 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 7 ms
5,420 KB
testcase_12 AC 8 ms
5,412 KB
testcase_13 AC 8 ms
5,304 KB
testcase_14 AC 6 ms
5,288 KB
testcase_15 AC 6 ms
5,416 KB
testcase_16 AC 6 ms
5,488 KB
testcase_17 AC 6 ms
5,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<llint, llint> P;

struct AhoCorasick{
	struct TrieNode{
		int num, sum;
		int next[26], suff;
		TrieNode(){
			num = sum = 0;
			for(int i = 0; i < 26; i++) next[i] = -1;
		}
	};
	vector<TrieNode> trie;
	
	AhoCorasick(){
		init();
	}
	void init(){
		trie.clear();
		trie.push_back(TrieNode());
	}
	int seek(string &s)
	{
		int p = 0;
		for(int i = 0; i < s.size(); i++){
			int c = s[i]-'a';
			if(trie[p].next[c] == -1){
				trie.push_back(TrieNode());
				trie[p].next[c] = (int)trie.size()-1;
			}
			p = trie[p].next[c];
		}
		return p;
	}
	
	int trans(int v, int c)
	{
		if(trie[v].next[c] != -1) return trie[v].next[c];
		if(v == 0) return 0;
		return trans(trie[v].suff, c);
	}
	
	void addString(string &s, int x = 1)
	{
		int p = seek(s);
		trie[p].num += x;
	}
	
	void makeSuffixLink()
	{
		queue<int> Q;
		Q.push(0);
		trie[0].suff = 0;
		
		int v;
		while(Q.size()){
			v = Q.front();
			Q.pop();
			trie[v].sum = trie[v].num + trie[trie[v].suff].sum;
			for(int i = 0; i < 26; i++){
				int u = trie[v].next[i];
				if(u == -1) continue;
				trie[u].suff = trans(trie[v].suff, i);
				if(v == 0) trie[u].suff = 0;
				Q.push(u);
			}
		}
	}
};

llint n;
string s, t[5005];
AhoCorasick aho;

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> s;
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> t[i];
	
	for(int i = 0; i < s.size(); i++) s[i] += 'a' - 'A';
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < t[i].size(); j++){
			t[i][j] += 'a' - 'A';
		}
	}
	
	for(int i = 1; i <= n; i++) aho.addString(t[i]);
	aho.makeSuffixLink();
	
	/*for(int i = 0; i < aho.trie.size(); i++){
		for(int j = 0; j < 26; j++){
			if(aho.trie[i].next[j] != -1) cout << (char)(j+'a') << aho.trie[i].next[j] << " ";
		}
		cout<< endl;
	}*/
	/*for(int i = 0; i < aho.trie.size(); i++){
		cout << i << " " << aho.trie[i].suff << " " << aho.trie[i].sum << endl;
	}*/
	
	llint ans = 0, v = 0;
	for(int i = 0; i < s.size(); i++){
		v = aho.trans(v, s[i]-'a');
		ans += aho.trie[v].sum;
	}
	cout << ans << endl;
	
	return 0;
}
0