题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222

Keywords Search

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

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自动机的模板题。模板来自kuangbin, 在此特别鸣谢!

代码如下:

 #include<bits/stdc++.h>
#define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 5e5+; struct Trie //封装在一个结构体中
{
int next[MAXN][], fail[MAXN], end[MAXN];
int root, L; int newnode()
{
for(int i = ; i<; i++)
next[L][i] = -;
end[L++] = ;
return L-;
} void init()
{
L = ;
root = newnode();
} void insert(char buf[]) //往Trie树中插入单词
{
int len = strlen(buf);
int now = root;
for(int i = ; i<len; i++)
{
if(next[now][buf[i]-'a'] == -)
next[now][buf[i]-'a'] = newnode();
now = next[now][buf[i]-'a'];
}
end[now]++;
} void build() //构建fail指针
{
queue<int>Q;
fail[root] = root;
for(int i = ; i<; i++)
{
if(next[root][i] == -)
next[root][i] = root;
else
fail[next[root][i]] = root, Q.push(next[root][i]);
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
for(int i = ; i<; i++)
{
if(next[now][i] == -)
next[now][i] = next[fail[now]][i];
else
fail[next[now][i]] = next[fail[now]][i], Q.push(next[now][i]);
}
}
} int query(char buf[]) //将字符串与Trie树进行匹配
{
int len = strlen(buf);
int now = root;
int res = ;
for(int i = ; i<len; i++)
{
now = next[now][buf[i]-'a'];
int tmp = now;
while(tmp != root)
{
res += end[tmp];
end[tmp] = ;
tmp = fail[tmp]; }
}
return res;
}
}; Trie ac;
char buf[MAXN<<];
int main()
{
int T, n;
scanf("%d",&T);
while(T--)
{
ac.init();
scanf("%d", &n);
for(int i = ; i<=n; i++)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
scanf("%s",buf);
printf("%d\n",ac.query(buf));
}
return ;
}

HDU2222 Keywords Search —— AC自动机的更多相关文章

  1. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

  2. HDU2222 Keywords Search [AC自动机模板]

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

  3. hdu2222 KeyWords Search AC自动机入门题

    /** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...

  4. HDU2222 Keywords Search ac自动机第一题

    指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...

  5. hdu2222 Keywords Search (AC自动机板子

    https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...

  6. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  7. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...

  8. Keywords Search(AC自动机模板)

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

  9. Keywords Search AC自动机

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

随机推荐

  1. Codeforces 599E Sandy and Nuts(状压DP)

    题目链接 Sandy and Nuts 题意大概就是给出限制条件求出在该限制条件下树的种数. #include <bits/stdc++.h> using namespace std; # ...

  2. win7 x64 dtrace

    1.下载WINDOW DTRACE 工具 https://github.com/prash-wghats/DTrace-win32 2.系统参数修改 bcdedit/set testsigning o ...

  3. 【spring boot】注解@SpringBootApplication 相当于 @Configuration、@EnableAutoConfiguration 、 @ComponentScan 三个的作用

    注解@SpringBootApplication 相当于 @Configuration.@EnableAutoConfiguration . @ComponentScan 三个的作用 代码示例:Git ...

  4. GridControl 校验输入单元格格式 z

    // 对应的校验行事件,ValidateRow private void gv_FeeItem_ValidateRow(object sender, DevExpress.XtraGrid.Views ...

  5. 7.2 HAVING子句

    7.2 HAVING子句正在更新内容.请稍后

  6. 【每日Scrum】第七天(4.28)Sprint2总结性会议

    本次会议主要是演示了一下本组项目的各项功能,每个人负责那一块儿功能由本人来负责说明和演示,确定alpha版本的发布时间,并且分派了各组员的文档负责情况,上图是会议记录,下面我详细介绍一下我组分派情况: ...

  7. python缺省参数

    def test(a,b=22): result=a+b print("resuLt=%d"%result) test(33,33) #缺省参数的意思就是,函数在有参数的情况下,调 ...

  8. vs2010中添加dll文件

    1.更改设置 1.1   project->properties->configuration properties->C/C++->General->Addtional ...

  9. 服务管理-DHCP、NTP、SSH

    DHCP协议的作用及原理 1.寻找server 当DHCP客户端第一次登陆网络的时候,也就是客户发现本机上没有任何ip资料设定,他会向网路发送一个dhcpdiscover封包.因为客户端还不知道自己属 ...

  10. Ubuntu16.04下屏幕侧边栏的设置

    ubnutu的任务栏都是在左侧: zhang@zhang-virtual-machine:~$ gsettings set com.canonical.Unity.Launcher launcher- ...