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. pm2的的常用命令及用法

    使用pm2启动静态文件服务器的方法如下: pm2 serve path port pm2 serve . 9001 这样就可以把当前文件夹下的静态文件跑起来了,而且端口号是9001, 同样也支持进阶的 ...

  2. Eclipse Infrastructure

    Everything is plug-ins running on or loaded by plug-ins loader called by a small kernal which is an ...

  3. python模块详解 hashlib

    hashlib模块 用于加密相关的操作,在python3中替代了md5和sha模块,主要提供SHA和MD5算法. MD5 import hashlib m = hashlib.md5() #调用md5 ...

  4. Struts2_Global_Results_全局结果集

    struts.xml文件配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts ...

  5. ansible使用2-命令

    并发与shell # bruce用户身份,-m指定模块名称,默认模块名command,all所有目标主机,也可以指定组名或者主机名 ansible all -m ping -u bruce # bru ...

  6. Arcgis for Js之鼠标经过显示对象名的实现

    在浏览地图时,移动鼠标经过某个对象或者POI的时候,能够提示该对象的名称对用户来说是很实用的,本文讲述在Arcgis for Js中,用两种不同的方式来实现该效果. 为了有个直观的概念,先给大家看看实 ...

  7. Java中的字符集

    Java中的字符集 1.字符集概述 字符集是各国家文字与字符编码对照表.字符可以看成是计算机中展示的图案效果,每个字符集都对每一种图案进行编码,有着一对一的对应关系.因此进行字符输出时,都需要指定使用 ...

  8. CentOS6下DHCP服务(二)简单配置案例及故障排查

    1.预分配网络参数如下:linux服务器:eth0 IP为192.168.8.250  做为局域网DHCP服务器局域网网段设置为192.168.8.0/24:内部计算机的router为192.168. ...

  9. C++学习之显式类型转换与运行时类型识别RTTI

    static_cast const_cast reinterpret_cast 运行时类型识别(RTTI) dynamic_cast 哪种情况下dynamic_cast和static_cast使用的情 ...

  10. 使用selenium启动火狐浏览器,解决Unable to create new remote session问题

    今天用火狐浏览器来做自动化,才启动就报错,提示不能创建新的session,不能启动浏览器 问题原因: 火狐driver与火狐浏览器与selenium版本的不兼容 我使用的火狐driver是0.21.0 ...