/*
Keywords Search

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

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
*/

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; char s[],m[];
int T,n,sz,ans;
int a[][]/*字典树*/ ,q[],point[],danger[]/*单词结束标志*/;
bool mark[]; int ins()
{
int now=,l=strlen(s);
for (int i=;i<l;i++)
{
int t=s[i]-'a'+;
if (a[now][t]) now=a[now][t];
else now=a[now][t]=++sz;
}
danger[now]++;
} int acmach()
{
q[]=;point[]=;
int now,w=,t=;
while (t<w)
{
now=q[t++];
for (int i=;i<=;i++)
{
if (!a[now][i]) continue;
int k=point[now];
while (!a[k][i]) k=point[k];
point[a[now][i]]=a[k][i];
q[w++]=a[now][i];
}
}
} int solve()
{
int k=,l=strlen(m);
for(int i=;i<l;i++)
{
mark[k]=;
int t=m[i]-'a'+;
while(!a[k][t]) k=point[k];
k=a[k][t];
if(!mark[k])
for(int j=k;j;j=point[j])
{
ans+=danger[j];
danger[j]=;
}
}
printf("%d\n",ans);
} int main()
{
scanf("%d",&T);
while(T--)
{
ans=;
sz=;
scanf("%d",&n);
for(int i=;i<=;i++)a[][i]=;
for (int i=;i<n;i++)
{
scanf("%s",s);
ins();
}
scanf("%s",m);
acmach();
solve();
for(int i=;i<=sz;i++)
{
point[i]=danger[i]=mark[i]=;
for(int j=;j<=;j++)
a[i][j]=;
}
}
}

C++之路进阶——hdu2222(Keywords Search)的更多相关文章

  1. HDU2222 Keywords Search 【AC自动机】

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

  2. AC自动机讲解+[HDU2222]:Keywords Search(AC自动机)

    首先,有这样一道题: 给你一个单词W和一个文章T,问W在T中出现了几次(原题见POJ3461). OK,so easy~ HASH or KMP 轻松解决. 那么还有一道例题: 给定n个长度不超过50 ...

  3. hdu2222 Keywords Search ac自动机

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

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

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

  5. hdu----(2222)Keywords Search(trie树)

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

  6. HDU2222 Keywords Search(AC自动机)

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

  7. hdu2222 Keywords Search【AC自动机】

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

  8. ACM学习历程—HDU2222 Keywords Search(字典树)

    Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...

  9. HDU2222 Keywords Search —— AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...

随机推荐

  1. IOS 手势事件的冲突

    关于手操作需要强调几点: UIImageView默认是不支持交互的,也就是userInteractionEnabled=NO ,因此要接收触摸事件(手势识别),必须设置userInteractionE ...

  2. EF不同查询方法生成的SQL比较

    var test1 = db.jj_RolesItem.Join(db.jj_SysItemsClass, m => m.ClassID, g => g.ClassID, (m, g) = ...

  3. [LeetCode]题解(python):049-Groups Anagrams

    题目来源 https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For ...

  4. IDEA UML类图插件

    idea已经集成了该功能,只是默认没打开,仍然打开Settings界面,定位到Plugins,输入UML,参考下图:

  5. Swift闭包

    把Swift中的 block 常见的声明和写法作一个总结.以免后续忘了,好查阅. // //  blockDemo.swift //  swiftDemo // //  Created by appl ...

  6. eclipse的快捷键大全

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...

  7. 集群中用Memcached来实现session共享

    这几天在实现nginx集群的过程中,发现session使用存在问题,登录页面后有时候需要重复登录,和开发部沟通后,决定采用memcached来实现session的共享,这也是各大型网站推荐的方式.开发 ...

  8. Java基础——数组应用之StringBuilder类和StringBuffer类

    接上文:Java基础——数组应用之字符串String类 一.StringBuffer类 StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和 ...

  9. 三层交换机+二层交换机配置VLAN+DHCP

    使用思科模拟软件Cisco Packet Tracer Student,软件功能有限,只能架设简单的网络架构,适合初学者使用.

  10. ios-完成任务状态栏弹出提示view的小框架设计

    设计思路: 创建单例,当设置提示view的属性时,可以随时访问到,并且只有一份. 创建对应的类方法.提供设置提示内容content,提示内容对应的图片image,提示view背景色以及背景图片的设置( ...