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
 
 
 
【题意】
  给你很多个单词,然后给你一篇文章,问给出的单词在文章中出现的次数。
 
【分析】
  根据输入的串建AC自动机,不断走fail累加即可。
 
注意理解题意,同一个串出现一次只算一次,给几个比较坑的数据:
4

5
she
he
say
shr
her
yasherhs 5
she
he
say
shr
her
yasherhs 3
she
she
she
shesheshe 6
she
he
he
say
shr
her
yasherhs 答案:
3
3
3
4

  

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 800100
#define Maxl 60 char s[Maxl];
char ss[]; struct node
{
int son[],cnt,fail;
}t[Maxn];int tot; void upd(int x)
{
t[x].cnt=;
memset(t[x].son,,sizeof(t[x].son));
} void read_trie()
{
int n;
scanf("%d",&n);
tot=;upd();
for(int i=;i<=n;i++)
{
int now;
scanf("%s",s+);
int len=strlen(s+);
now=;
for(int j=;j<=len;j++)
{
int ind=s[j]-'a'+;
if(!t[now].son[ind])
t[now].son[ind]=++tot,upd(tot);
now=t[now].son[ind];
if(j==len) t[now].cnt++;
}
}
} queue<int > q;
void build_AC()
{
int i,j,x,y;
while(!q.empty()) q.pop();
q.push();
while(!q.empty())
{
x=q.front();
y=t[x].fail;
for(j=;j<=;j++)
{
if(t[x].son[j])
{
q.push(t[x].son[j]);
t[t[x].son[j]].fail=x?t[y].son[j]:x;
}
else t[x].son[j]=t[y].son[j];
}
q.pop();
}
} int gmark(int x,int y)
{
if(!x) return y;
if(t[x].cnt) y+=t[x].cnt,t[x].cnt=;
return gmark(t[x].fail,y);
} void ffind()
{
scanf("%s",ss+);
int l=strlen(ss+);
int now,ans=;
for(int i=;i<=l;i++)
{
int x=ss[i]-'a'+;
now=t[now].son[x];
ans+=gmark(now,);
}
printf("%d\n",ans);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
read_trie();
build_AC();
ffind();
}
return ;
}

[HDU2222]

2016-06-14 17:02:09

 

最新文章

  1. sql 索引创建
  2. Java Servlet与Applet、CGI、JSP的比较
  3. jquery easy ui 学习 (7) TreeGrid Actions
  4. JavaScript之事件处理详解
  5. VS插件-JSEnhancements
  6. MySQL中的一些内置函数
  7. Spring源码情操陶冶-AbstractApplicationContext
  8. Spark源码剖析(一):如何将spark源码导入到IDEA中
  9. .net framework 4.5 +steeltoe+ springcloud 实现服务注册功能
  10. CSS Sprite雪碧图
  11. win快捷键
  12. Kernel数据结构移植(list和rbtree)
  13. TensorFlow 学习资料
  14. js获取网页面的高度和宽度
  15. 前端 -----jQuery的位置信息
  16. 【洛谷】【动态规划/01背包】P1734 最大约数和
  17. webpack 解决跨域问题
  18. 用 JS 设置图片的最大宽度
  19. WDA基础七:TABStrip
  20. Hard commits, soft commits and transaction logs

热门文章

  1. Python 基础学习
  2. 【转】学习Flex ActionScript 3.0 强烈推荐电子书
  3. Getting started with new I/O (NIO)--reference
  4. Android 自定义View修炼-Android开发之自定义View开发及实例详解
  5. Android WebView的loadData方法注意事项
  6. Linux 确定系统glibc版本
  7. BI任务列表
  8. MICROSOFT REPORT VIEWER 2012之无法加载相关的dll
  9. A题笔记(1)
  10. shell脚本学习之Bash shell 里各种括号的用法