hdu2222 KeyWords Search AC自动机入门题

时间:2023-03-08 23:10:16
hdu2222 KeyWords Search AC自动机入门题
/**
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222
题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L(L <= 106)目标串,求目标串出现了多少个模式串。
思路:ac自动机入门题。。直接插入查询。
唯一需要特殊考虑的是存在多个相同的字符串;相同的字符串会在字典书上覆盖原先的。
解决方法1:用map<string,int>标记同一种字符串。之后利用标记来统计。
解决方法2:用num[i]标记字典树上某个节点为结尾的字符串出现次数。之后统计的时候,如果是第一次统计它,那么加上它,然后置为-1表示
下次不需要再统计它了。 AC自动机好文章:http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html
*/ ///解法1:
#include<bits/stdc++.h>
using namespace std;
#define P pair<int,int>
#define ms(x,y) memset(x,y,sizeof x)
#define LL long long
const int maxn = ;
const int mod = 1e9+;
const int maxnode = *+;
const int sigma_size = ;
int cnt[];
map<string,int> mp;
struct AhoCorasickAutomata
{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
int f[maxnode];
int last[maxnode];
void clear(){sz = ; memset(ch[],,sizeof ch[]); }
int idx(char c){return c-'a'; } void insert(char *s,int x)
{
int u = , n = strlen(s);
for(int i = ; i < n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], , sizeof ch[sz]);
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = x;
} void find(char *T){
int n = strlen(T);
int j = ;
for(int i = ; i < n; i++){
int c = idx(T[i]);
//while(j&&!ch[j][c]) j = f[j];
j = ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
} void print(int j)
{
if(j){
cnt[val[j]] = ;
print(last[j]);
}
} void getFail(){
queue<int> q;
f[] = ;
for(int c = ; c < sigma_size; c++){
int u = ch[][c];
if(u){f[u] = ; q.push(u); last[u] = ;}
} while(!q.empty()){
int r = q.front(); q.pop();
for(int c = ; c < sigma_size; c++){
int u = ch[r][c];
if(!u){
ch[r][c] = ch[f[r]][c]; continue;
}//if(!u) continue;
q.push(u);
int v = f[r];
while(v&&!ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} } ac ;
char s[];
char t[][];
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
scanf("%d",&n);
ac.clear();
mp.clear();
for(int i = ; i <= n; i++){
scanf("%s",t[i]);
ac.insert(t[i],i);
mp[string(t[i])] = i;///因为两个完全相同的字符串会覆盖原先的,所以用map标记属于同一个。这样可以都加到。
}
scanf("%s",s);
ac.getFail();
ms(cnt,);
ac.find(s);
int ans = ;
for(int i = ; i <= n; i++) ans += cnt[mp[string(t[i])]];
printf("%d\n",ans);
}
return ;
} /*
1
5
she
he
say
shr
her
yasherhs
*/ ///解法2:
#include<bits/stdc++.h>
using namespace std;
#define P pair<int,int>
#define ms(x,y) memset(x,y,sizeof x)
#define LL long long
const int maxn = ;
const int mod = 1e9+;
const int maxnode = *+;
const int sigma_size = ;
int cnt[];
map<string,int> mp;
int num[maxnode];///统计在自动机上到达i节点的这个字符串的相同字符串的个数。
struct AhoCorasickAutomata
{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
int f[maxnode];
int last[maxnode];
void clear(){sz = ; memset(ch[],,sizeof ch[]); }
int idx(char c){return c-'a'; } void insert(char *s,int x)
{
int u = , n = strlen(s);
for(int i = ; i < n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], , sizeof ch[sz]);
num[sz] = ;
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = x;
num[u]++;
} void find(char *T){
int n = strlen(T);
int j = ;
for(int i = ; i < n; i++){
int c = idx(T[i]);
//while(j&&!ch[j][c]) j = f[j];
j = ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
} void print(int j)
{
if(j){
if(num[j]!=-){
cnt[val[j]] = num[j];
num[j] = -;
}
print(last[j]);
}
} void getFail(){
queue<int> q;
f[] = ;
for(int c = ; c < sigma_size; c++){
int u = ch[][c];
if(u){f[u] = ; q.push(u); last[u] = ;}
} while(!q.empty()){
int r = q.front(); q.pop();
for(int c = ; c < sigma_size; c++){
int u = ch[r][c];
if(!u){
ch[r][c] = ch[f[r]][c]; continue;
}//if(!u) continue;
q.push(u);
int v = f[r];
while(v&&!ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} } ac ;
char s[];
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
scanf("%d",&n);
ac.clear();
mp.clear();
for(int i = ; i <= n; i++){
scanf("%s",s);
ac.insert(s,i);
}
scanf("%s",s);
ac.getFail();
ms(cnt,);
ac.find(s);
int ans = ;
for(int i = ; i <= n; i++) ans += cnt[i];
printf("%d\n",ans);
}
return ;
} /*
2
5
she
he
say
shr
her
yasherhs
2
ab
ab
aba
*/