记录一个菜逼的成长。。
题目链接
题目大意:
给你n个字符串,最后给你一个字符串,问最后的字符串里包含前面n个里面的几种字符串
AC自动机入门。简单的说就是树上的KMP。
kuangbin的模板
失败指针的构造:
构造失败指针的过程概括起来就一句话:设这个节点上的字母为C,沿着他父亲的失败指针走,直到走到一个节点,他的儿子中也有字母为C的节点。然后把当前节点的失败指针指向那个字母也为C的儿子。如果一直走到了root都没找到,那就把失败指针指向root。具体操作起来只需要:先把root加入队列,这以后我们每处理一个点,就把它的所有儿子加入队列,队列为空。
不过在这个模板里,next里不存在的都指向了root,可以省略了很多过程。
#include <bits/stdc++.h>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
typedef long long LL;
const int INF = 0x3f3f3f3f;
struct Trie
{
int next[500010][26],fail[500010],cnt[500010];
int root,L;
int newnode()
{
for(int i = 0;i < 26;i++)
next[L][i] = -1;
cnt[L++] = 0;
return L-1;
}
void init()
{
L = 0;
root = newnode();
}
void Insert(char buf[])
{
int len = strlen(buf);
int now = root;
for(int i = 0;i < len;i++)
{
if(next[now][buf[i]-'a'] == -1)
next[now][buf[i]-'a'] = newnode();
now = next[now][buf[i]-'a'];
}
cnt[now]++;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = 0;i < 26;i++)
if(next[root][i] == -1)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
while( !Q.empty() )
{
int now = Q.front();
Q.pop();
for(int i = 0;i < 26;i++)
if(next[now][i] == -1)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
int query(char buf[])
{
int len = strlen(buf);
int now = root;
int res = 0;
for(int i = 0;i < len;i++)
{
now = next[now][buf[i]-'a'];
int temp = now;
//这一步是因为一个单词的后缀可能是其他单词的前缀
while( temp != root )
{
res += cnt[temp];
cnt[temp] = 0;//防止重复计算(比如AAAA里包含3个AA,但这题只计算一个),但是要计算个数而不是种数时,这行不要。
temp = fail[temp];
}
}
return res;
}
};
const int maxn = 1000000 + 10;
char str[maxn];
Trie ac;
int main()
{
int T;scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
ac.init();
for( int i = 0; i < n; i++ ){
scanf("%s",str);
ac.Insert(str);
}
ac.build();
scanf("%s",str);
int ans = ac.query(str);
printf("%d\n",ans);
}
return 0;
}

本文介绍了一种使用AC自动机解决字符串匹配问题的方法。通过构建失败指针,实现了高效的字符串匹配,特别适用于寻找多个模式串在一个目标串中的出现情况。文章提供了完整的C++代码实现,并附带了解题思路。
1118

被折叠的 条评论
为什么被折叠?



