[Ybt Advanced 2-5-1] Word Search

Word query

Topic link: ybt efficient advanced 2-5-1

General idea

There are a bunch of words, and then there is another sentence.
Ask how many words you have appeared as a substring in this sentence.

Multiple sets of data.

Ideas

AC automata template questions.

I have written it before, and there is nothing to say, just go to the template.

——>If you don’t, you can watch this<——

Code

#include<queue> 
#include<cstdio>
#include<cstring>

using namespace std; 

struct Trie {
    
    
	int son[30], num, fail;
	bool use;
}tree[500001];
int T, tot, ans, n;
char c[51], s[1000001];
queue <int> q;

void csh() {
    
    
	tot = 0;
	ans = 0;
	memset(tree, 0, sizeof(tree));
}

void insert_word() {
    
    
	int size = strlen(c);
	int now = 0;
	
	for (int i = 0; i < size; i++) {
    
    
		if (!tree[now].son[c[i] - 'a']) tree[now].son[c[i] - 'a'] = ++tot;
		now = tree[now].son[c[i] - 'a'];
	}
	
	tree[now].num++;
}

void build_fail() {
    
    
	for (int i = 0; i < 26; i++)
		if (tree[0].son[i]) {
    
    
			q.push(tree[0].son[i]);
			tree[tree[0].son[i]].fail = 0;
		}
	
	while (!q.empty()) {
    
    
		int now = q.front();
		q.pop();
		
		for (int i = 0; i < 26; i++)
			if (tree[now].son[i]) {
    
    
				q.push(tree[now].son[i]);
				tree[tree[now].son[i]].fail = tree[tree[now].fail].son[i];
			}
			else tree[now].son[i] = tree[tree[now].fail].son[i];
	}
}

void get_AC() {
    
    
	int now = 0;
	int size = strlen(s);
	
	for (int i = 0; i < size; i++) {
    
    
		now = tree[now].son[s[i] - 'a'];
		
		int noww = now;
		while (noww && !tree[noww].use) {
    
    
			ans += tree[noww].num;
			tree[noww].use = 1;
			noww = tree[noww].fail;
		}
	}
} 

int main() {
    
    
	scanf("%d", &T);
	while (T--) {
    
    
		csh();
		
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
    
    
			scanf("%s", &c);
			insert_word(); 
		}
		scanf("%s", &s);
		
		build_fail();
		
		get_AC();
		
		printf("%d\n", ans);
	}
	
	return 0;
} 

Guess you like

Origin blog.csdn.net/weixin_43346722/article/details/114295501