HDU 2222 Keywords Search AC自动机

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35631    Accepted Submission(s): 11483


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
 

Author
Wiskey
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   2896  3065  2243  2825  3341 

今天刚刚学了一下AC自动机,看了好久还是有点不太明白,那个fail指针的构造没看懂,照着别人的模板敲了一遍,先留着,以后慢慢领悟。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 10000000
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

struct node
{
    int cnt;//标记该单词是否为最后一个节点
    node *fail;//失败指针
    node *next[26];//Tire每个节点的子节点,大小根据题意而定
}*q[MAXN];
char keyword[51];//输入的单词,模式串
char str[1000010];//主串
int head,tail;
node *root;

void Insert(char *word,node *root)
{
    int index,len;
    node *p=root,*newnode;
    len=strlen(word);
    for (int i=0;i<len;i++)
    {
        index=word[i]-'a';
        if (!p->next[index])  //检查该节点是否存在,否则加入trie树
        {
            newnode=(struct node*)malloc(sizeof(struct node));//申请空间,添加新的节点
            for (int j=0;j<26;j++)
                newnode->next[j]=NULL;
            newnode->cnt=0;
            newnode->fail=NULL;
            p->next[index]=newnode;
        }
        p=p->next[index];
    }
    p->cnt++;//单词结尾节点自增标记
}

void build_ac_automation(node *root)
{
    head=0;
    tail=1;
    q[head]=root;
    node *temp,*p;
    while (head<tail)  //bfs构造Trie树的失败指针
    {
        //注意,匹配失败时,由fail指针回溯到正确的位置
        temp=q[head];
        head++;
        for (int i=0;i<26;i++)
        {
            if (temp->next[i])
            {
                if (temp==root)//root下的第一层节点的fail指针都指向root
                    temp->next[i]->fail=root;
                else
                {
                    p=temp->fail;
                    while (p)
                    {
                        if (p->next[i])
                        {
                            temp->next[i]->fail=p->next[i];
                            break;
                        }
                        p=p->fail;
                    }
                    if (!p)
                        temp->next[i]->fail=root;
                }
                q[tail++]=temp->next[i];
            }
        }
    }
}

int query(node *root)
{
    int i,num=0,index,len=strlen(str);
    node *p=root;
    for (i=0;i<len;i++)
    {
        index=str[i]-'a';
        //由失败指针回溯寻找,判断str[i]是否存在于Trie树中
        while(!p->next[index]&&p!=root)
            p=p->fail;
        p=p->next[index];//找到后p指向该节点
        if (!p)
            p=root;
        node *temp=p;
        while (temp!=root)
        {
            if (temp->cnt>=0)//判断该节点是否被访问过
            {
                num+=temp->cnt;
                temp->cnt=-1;
            }
            else
                break;
            temp=temp->fail;
        }
    }
    return num;
}

int main()
{
    int t,n,ans;
    scanf("%d",&t);
    while (t--)
    {
        root=(struct node *)malloc(sizeof(struct node));
        for (int i=0;i<26;i++)
            root->next[i]=NULL;
        root->fail=NULL;
        root->cnt=0;
        scanf("%d",&n);
        for (int i=0;i<n;i++)
        {
            scanf("%s",keyword);
            Insert(keyword,root);
        }
        build_ac_automation(root);
        scanf("%s",str);
        ans=query(root);
        printf("%d\n",ans);
    }
    return 0;
}


(1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词与专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现与管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友正在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参与 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值