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. agc027D - Modulo Matrix(构造 黑白染色)

    题意 题目链接 构造一个\(n * n\)的矩阵,要求任意相邻的两个数\(a,b\),使得\(max(a,b) \% min(a,b) \not = 0\) Sol 我的思路: 假设\(mod = 1 ...

  2. MySQL累加值时,考虑到值有为NULL的情况.

    一个字段,表示报名人数,默认为null,经考虑,以以下sql执行加1: ) where id='xxx'

  3. 微软RPC技术学习小结

    RPC,即Remote Procedure Call,远程过程调用,是进程间通信(IPC, Inter Process Communication)技术的一种.由于这项技术在自己所在项目(Window ...

  4. android studio gradle统一管理版本

    创建config.gradle ext { android = [ compileSdkVersion : 26, buildToolsVersion : "26.0.2", mi ...

  5. open ssh 常用的东西

    清除已经存在的但是不同设备的连接信息 ssh-keygen -f "/users/he/.ssh/known_hosts" -R 192.168.1.118 无密码登录openss ...

  6. 笨办法学Python(一)

    习题 1: 第一个程序 你应该在练习 0 中花了不少的时间,学会了如何安装文本编辑器.运行文本编辑器.以及如何运行命令行终端,而且你已经花时间熟悉了这些工具.请不要跳过前一个练习的内容直接进行下面的内 ...

  7. Android 中间白色渐变到看不见的线的Drawable

    用gradient <gradient android:startColor="#00ffffff" android:centerColor="#ffffff&qu ...

  8. 二维码生成的WEB api方法

    /// <summary> /// 获取二维码 /// </summary> /// <param name="size">编码测量度,值越大生 ...

  9. selenium 打开浏览器报错java.lang.NoSuchMethodError: org.openqa.selenium.chrome.ChromeOptions.addArguments([Ljava/lang/String;)

    java.lang.NoSuchMethodError: org.openqa.selenium.chrome.ChromeOptions.addArguments([Ljava/lang/Strin ...

  10. ABI 管理

    https://developer.android.google.cn/ndk/guides/abis.html 不同 Android 手机使用不同的 CPU,因此支持不同的指令集.CPU 与指令集的 ...