Keywords Search

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

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 
 

题意:给出一个字典和一个模式串,问模式串中出现几个字典中的单词

代码:

 ////#include "bits/stdc++.h"
#include "cstdio"
#include "map"
#include "set"
#include "cmath"
#include "queue"
#include "vector"
#include "string"
#include "cstring"
#include "time.h"
#include "iostream"
#include "stdlib.h"
#include "algorithm"
#define db double
#define ll long long
//#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, x, y) for(int i=x;i<=y;i++)
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
const int M = ;
queue<int> q;
vector<string> vec;
bool vis[N];
struct Trie {
int trieN;
int ch[N][M], val[N], fail[N];
void init() {
memset(vis,,sizeof(vis));
trieN = -;
newnode();
}
int newnode() {
memset(ch[++trieN], , sizeof(ch[]));
val[trieN] = fail[trieN] = ;
return trieN;
}
void insert(const string &str, int index) {
int cur = ;
for (int i = ;str[i];i++) {
int d = str[i] - 'a';
if (!ch[cur][d])
ch[cur][d] = newnode();
cur = ch[cur][d];
}
if (val[cur]) vis[index] = ;
else val[cur] = index;
}
void build() {
for (int i = ;i < M;i++) {
if (ch[][i])
q.push(ch[][i]);
}
while (!q.empty()) {
int cur = q.front(); q.pop();
for (int i = ;i < M;i++) {
int &next = ch[cur][i];
if (next) {
fail[next] = ch[fail[cur]][i];
q.push(next);
}
else next = ch[fail[cur]][i];
}
}
}
void query(const string &str) {
int cur = , tmp;
for (int i = ;str[i];i++) {
int d = str[i] - 'a';
tmp = cur = ch[cur][d];
while (tmp && ~val[tmp]) {
if (val[tmp] != -) vis[val[tmp]] = ;
val[tmp] = -;
tmp = fail[tmp];
}
}
}
}ac; int main(){
ios::sync_with_stdio(false);
cin.tie();
int t;
cin >> t;
while(t--){
ac.init(); //初始化
int n;
string str;
cin >> n;
vec.resize(n+);
for(int i = ;i <= n;i++){
cin >> vec[i];
ac.insert(vec[i],i);
}
ac.build();
cin>>str;
ac.query(str); //查询是否在str里出现过
int ans=;
for(int i = ;i <= n;i++){
if (vis[i]) ans++;
}
pi(ans);
}
return ;
}

HDU 2222 AC自动机(模版题)的更多相关文章

  1. hdu 2222(AC自动机模版题)

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

  2. HDU 2222 AC自动机模版题

    所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...

  3. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  4. HDU 2222 AC自动机 裸题

    题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...

  5. hdu 2896 AC自动机模版题

    题意:输出出现模式串的id,还是用end记录id就可以了. 本题有个关键点:“以上字符串中字符都是ASCII码可见字符(不包括回车).”  -----也就说AC自动机的Trie树需要128个单词分支. ...

  6. hdu 3065 AC自动机模版题

    题意:输出每个模式串出现的次数,查询的时候呢使用一个数组进行记录就好. 同上题一样的关键点,其他没什么难度了. #include <cstdio> #include <cstring ...

  7. Keywords Search HDU - 2222 AC自动机板子题

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

  8. HDU 2222 Keywords Search(AC自动机模版题)

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

  9. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

随机推荐

  1. Kindeditor单独调用多图上传

    html代码: <input type="button" id="J_selectImage" value="图片上传" />& ...

  2. sharepoint2007就地升级2010系列(四)升级数据库

    上一篇我们完成了系统的升级,今天我们来看一下SQL2005X64是如何升级到SQL2008X64的. 首先,我们先停掉所有sharepoint的服务 其实网上的文档并没有写到这一步,但是我个人觉得,要 ...

  3. ASP.NET MVC 音乐商店 - 1 创建项目

    我们的项目从在 Visual Studio 中的文件菜单中选择“新建”,选择“项目”开始. 然后,选择 C# 中的 Web 模板组,在右边的项目模板中选择 ASP.NET MVC3 Web 应用程序, ...

  4. Lua相关函数整理

    1.asset(a==b,tipmsg);错误处理 2.pcall,xpcall,debug,保护函数执行,并且查看相关信息 3.collectgarbage()函数相关: collectgarbag ...

  5. Android触摸事件MotionEvent详解

    触摸事件MotionEvent在用户交互中,占着非常重要的地位.首先,来看看MotionEvent中封装的一些常用的事件常量,它定义了触摸事件的不同类型. 1.单点触摸按下动作 public stat ...

  6. 视频SharePoint 2010 大局观 By 陈希章[zt]

    SharePoint 2010 大局观 By 陈希章 http://hi.baidu.com/jinzesudawei/item/9d0adace8cbcaf2c47d5c0fc

  7. 爬虫技术框架——Heritrix

    Heritrix是一个由Java开发的开源Web爬虫系统,用来获取完整的.精确的站点内容的深度复制, 具有强大的可扩展性,运行开发者任意选择或扩展各个组件,实现特定的抓取逻辑. 一.Heritrix介 ...

  8. 解决ubuntu的chkconfig[/sbin/insserv 无法找到路径问题]

    http://www.cnblogs.com/lost-1987/archive/2012/10/17/2727285.html 今天在虚拟机里做DRBD实验,使用chkconfig管理服务的时候,出 ...

  9. *1 Two Sum two pointers(hashmap one scan)

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  10. 使用命令创建jenkins的job,解决jenkinsapi.custom_exceptions.JenkinsAPIException错误

    如果你使用 Python 2.7.12,Jenkins版本为Jenkins ver. 2.22,你使用我上面一种提到的修改的以下代码可以进行Jenkins的job复制 http://www.cnblo ...