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. tween.js 插件

    1.是什么? jQueryTween是一款轻量级的jQuery补间动画工具库插件.使用jQueryTween可以制作出各种平滑的动画过渡效果.该插件基于tween.js,旨在简化各种补间动画操作,提供 ...

  2. AWS ELB Sticky Session有问题?别忘了AWSELB cookie

    我们的产品中有两个Module,分别部署在独立的Linux机器上,Module 1需要向Module 2发起Http请求来获得服务.由于Module 2有多台,因此我们会在Module 2前部署一台负 ...

  3. chrome浏览器设置12px以下字体大小

    内容很简单 在 body 上添加一个 css 属性即可. .body { -webkit-text-size-adjust: none; } 结束,晚安!

  4. c++为什么定义了析构函数的类的operator new[]传入的参数会多4字节?

    问题:   在网上看人写了这么一段代码: class A { public: A() { std::cout<<"call A constructor"<< ...

  5. SAP Fiori里的List是如何做到懒加载Lazy load的

    今天一同事问我这个问题:S/4HANA Fiori应用里的列表,一旦Scroll到底部就会自动向后台发起新的请求把更多的数据读取到前台显示. 以Product Master这个应用为例,我点击搜索之后 ...

  6. IOS 线程的总结(及cell的图片下载)

    零.线程的注意点(掌握) 1.不要同时开太多的线程(1~3条线程即可,不要超过5条)2.线程概念1> 主线程 : UI线程,显示.刷新UI界面,处理UI控件的事件2> 子线程 : 后台线程 ...

  7. 广搜,智能拼图(ZOJ1079)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=79 解题报告: 思路简单,写法太难. #include <std ...

  8. Poj(2488),按照字典序深搜

    题目链接:http://poj.org/problem?id=2488 思路:按照一定的字典序深搜,当时我的想法是把所有的可行的路径都找出来,然后字典序排序. 后来,凡哥说可以在搜索路径的时候就按照字 ...

  9. DOM节点(二):操作节点

    appendChild() 用于向childNodes列表的末尾添加一个节点. var returnedNode = someNode.appendChild(newNode); 如果传入的节点已经是 ...

  10. 【转】VMware虚拟机系统无法上网怎么办?

    有很多用户通过安装VMware软件来创建虚拟机系统,其中就有部分用户在创建好虚拟机系统后遇到无法上网的问题,下面PC6苹果网小编就给大家带来VMware虚拟机系统下无法上网的解决办法: 1.在虚拟机右 ...