博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AC自动机 Keywords Search
阅读量:6817 次
发布时间:2019-06-26

本文共 2222 字,大约阅读时间需要 7 分钟。

/*

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 47775    Accepted Submission(s): 15220

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.
 

 */

#include<cstdio>

#include<iostream>
#include<cstring>
using namespace std;
int size,a[500001][27],T,shu[500001],dui[500001],fail[500001],sum;
bool f[500001];
char ch[100],s[1000008];
void jia()
{
int l=strlen(ch),now=1;
for(int i=0;i<l;i++)
if(a[now][ch[i]-'a'+1])
now=a[now][ch[i]-'a'+1];
else
{
size++;
now=a[now][ch[i]-'a'+1]=size;
}
shu[now]++;
return;
}
void shi()
{
dui[1]=1;
int h=0,t=1;
for(;h<t;)
{
h++;
int now=dui[h];
for(int i=1;i<=26;i++)
if(a[now][i])
{
int k=fail[now];
for(;!a[k][i];k=fail[k]);
fail[a[now][i]]=a[k][i];
t++;
dui[t]=a[now][i];
}
}
}
void jin()
{
int now=1,l=strlen(s);
for(int i=0;i<l;i++)
{
f[now]=1;
int k=s[i]-'a'+1;
for(;!a[now][k];now=fail[now]);
now=a[now][k];
if(!f[now])
for(int j=now;j;j=fail[j])
{
sum+=shu[j];
shu[j]=0;
}
}
printf("%d\n",sum);
}
int main()
{
for(int i=0;i<=26;i++)
a[0][i]=1;
scanf("%d",&T);
for(;T;T--)
{
size=1;
sum=0;
int n;
scanf("%d",&n);
for(;n;n--)
{
scanf("%s",ch);
jia();
}
shi();
scanf("%s",s);
jin();
for(int i=1;i<=size;i++)
{
f[i]=fail[i]=shu[i]=0;
for(int j=1;j<=26;j++)
a[i][j]=0;
}
}
return 0;
}

转载于:https://www.cnblogs.com/xydddd/p/5147234.html

你可能感兴趣的文章
android-studio 安装gradle
查看>>
nodejs字符与字节之间的转换
查看>>
C++:函数模板与模板函数
查看>>
iOS 内存管理
查看>>
linux查看某个进程CPU消耗较高的具体线程或程序的方法
查看>>
Codrops 实验:使用 Vibrant.js 提取图像颜色
查看>>
WPF中的换行符
查看>>
15天玩转redis —— 第五篇 集合对象类型
查看>>
相机的3A技术
查看>>
SSH配置
查看>>
Break the Chocolate(规律)
查看>>
论文阅读之 Inferring Analogous Attributes CVPR 2014
查看>>
quick-cocos2d-x游戏开发【6】——制作您自己的自定义效果button菜单
查看>>
JSON.parse()和JSON.stringify()
查看>>
android中的返回键与Activity
查看>>
站点分析基础概念网页浏览数
查看>>
蛇形填数之斜着排
查看>>
ASI简单实现网络编程
查看>>
LeetCode - Flatten Binary Tree to Linked List
查看>>
欧几里得(模板)
查看>>