hdu 2222 ac自动机

本文深入探讨了AC自动机在关键词搜索问题中的应用,通过详细解释代码实现过程,包括字典树构建、失败指针计算及关键词匹配算法,旨在帮助读者掌握AC自动机的核心原理与实践技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

没有学过ac自动机的同学 可移步这位大佬的链接 https://blog.youkuaiyun.com/bestsort/article/details/82947639

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 85932    Accepted Submission(s): 29875


 

Problem Description

In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.

 

 

Input

First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.

 

 

Output

Print how many keywords are contained in the description.

 

 

Sample Input

 

1 5 she he say shr her yasherhs

 

 

Sample Output

 

3

 

 

这里讲一下模板的各个代码的作用,加强一下理解。

这个题目还有要注意的地方就是TLE和MLE

输入字符串与字典树的大小为两倍关系,开大了会超。

不要用string 用char *读入,strlen要写在for循环外 

#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#include <bitset>
#define  LL long long
#define  ULL unsigned long long
#define mod 10007
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
using namespace std;
const int maxn = 10000 * 50 + 10;
int n,m;
int temp[maxn][26];
int colo[maxn];
int fail[maxn];
int cnt;
void buildTire(char str[])//建好字典树
{
    int tot = 0;
    int len = strlen(str);
   for(int i = 0; i < len; i++){
      int c = str[i] - 'a';
      if(!temp[tot][c]){
        temp[tot][c] = ++cnt;
      }
      tot = temp[tot][c];
   }
   colo[tot]++;//记录字符串结尾的位置一共有多少个
}
void getFail()//获得失配指针
{
    queue<int> Q;
    for(int i = 0; i < 26; i++){
      if(temp[0][i]){
        fail[temp[0][i]] = 0;//第一个字符的失配都指向根节点
        Q.push(temp[0][i]);
      }
    }
    while(!Q.empty()){
      int v = Q.front();
      Q.pop();

      for(int i = 0; i < 26; i++){
        if(temp[v][i]){
            fail[temp[v][i]] = temp[fail[v]][i];//当前字符的指针指向父节点失配指针的子节点
            Q.push(temp[v][i]);
        }
        else{
          temp[v][i] = temp[fail[v]][i];//若没有出现该字符,当前字符指向父节点失配指针的子节点
        }
      }
    }
}
int query(char str[])
{
    int ans = 0,now = 0;
    int len = strlen(str);
    for(int i = 0; i < len; i++){
      int c = str[i] - 'a';
      now = temp[now][c];
      for(int j = now; j && colo[j]!=-1; j = fail[j]){
        ans += colo[j];
        colo[j] = -1;
      }
    }
    return ans;
}
void init()
{
   for(int i = 0; i < maxn;i++){
     fail[i] = 0,colo[i] = 0;
   }
   mem(temp,0);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
      cnt = 0;
      init();
      int n;
      scanf("%d",&n);
      while(n--){
        char str[maxn << 1];
        scanf("%s",str);
        buildTire(str);
      }
      fail[0] = 0;
      getFail();
      char ss[maxn << 1];
      scanf("%s",ss);
      printf("%d\n",query(ss));
    }



    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值