HDOJ 2222 Keywords Search
来源:互联网 发布:php 时间轴 编辑:程序博客网 时间:2024/05/29 18:26
Keywords Search
Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u
[Submit] [Go Back] [Status]
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.
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.
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
15shehesayshrheryasherhs
Sample Output
3
一直觉得网上没有顺眼的AC自动机,所以参照白书写了个AC自动机的模板。。。。
AC自动机主要就3个部分:1 建树:和普通字典树一样
2 建失败指针:
///根结点的失败指针指向自己
///每个结点的失败指针是从父节点的失败指针指向的结点的子结点中寻找与这个结点相同的结点
///如果找不到,就向上回溯直到根节点
同时 顺手把不存在的边都给补上,顺手建last数组
3 模式匹配:因为失配函数的时候顺手把不存在的边都给补上了,直接顺着失配边走下去就好了
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;const int maxn=550000;struct AC_auto{ int chd[maxn][26],v[maxn],f[maxn],last[maxn],sz,ans; void init() { sz=1;ans=0; memset(v,0,sizeof(v)); memset(f,0,sizeof(f)); memset(chd[0],0,sizeof(chd[0])); } void insert(char* p) { int cur=0; for(;*p;p++) { if(!chd[cur][*p-'a']) { memset(chd[sz],0,sizeof(chd[sz])); chd[cur][*p-'a']=sz++; } cur=chd[cur][*p-'a']; } v[cur]++; } bool query(char* p) { int cur=0; for(;*p;p++) { if(!chd[cur][*p-'a']) break; cur=chd[cur][*p-'a']; } return v[cur]&&(!(*p)); } int getFail() { queue<int> q; f[0]=0; for(int c=0;c<26;c++) { int u=chd[0][c]; if(u) { f[u]=0; q.push(u); last[u]=0; } } while(!q.empty()) { int r=q.front(); q.pop(); for(int c=0;c<26;c++) { int u=chd[r][c]; if(!u){ chd[r][c]=chd[f[r]][c];continue;}///.... q.push(u); int vv=f[r]; while(vv&&!chd[vv][c]) vv=f[vv]; f[u]=chd[vv][c]; last[u]=v[f[u]] ? f[u] : last[f[u]]; } } } void solve(int j) { if(!j) return; if(v[j]) { ans+=v[j]; v[j]=0; } solve(last[j]); } void find(char* T) { int n=strlen(T),j=0; getFail(); for(int i=0;i<n;i++) { //while(j&&!chd[j][*T-'a']) j=f[j]; j=chd[j][T[i]-'a']; if(v[j]) solve(j); else if(last[j]) solve(last[j]); } }}ac;int main(){ int t,n; char dic[100],str[1100000]; scanf("%d",&t); while(t--) { ac.init(); scanf("%d",&n); while(n--) { scanf("%s",dic); ac.insert(dic); } scanf("%s",str); ac.find(str); printf("%d\n",ac.ans); } return 0;}
- HDOJ 2222 Keywords Search
- hdoj 2222 Keywords Search
- HDOJ 2222 Keywords Search
- HDOJ 2222 Keywords Search
- HDOJ 2222 Keywords Search
- ACM HDOJ 2222 (Keywords Search)
- 【AC自动机】 HDOJ 2222 Keywords Search
- hdoj--2222 Keywords Search(AC自动机)
- hdoj-2222Keywords Search(AC自动机)
- HDOJ 题目2222 Keywords Search(AC自动机)
- HDOJ 2222 - Keywords Search 更新AC自动机模板...用类来表示
- hdu 2222 Keywords Search
- hdu 2222 Keywords Search
- HDU-2222 Keywords Search
- 自动机 Keywords Search 2222
- hdu 2222 Keywords Search
- Hdu 2222 Keywords Search
- hdu 2222-Keywords Search
- 笛卡尔积
- MFC中的SetTimer、ON_WM_TIMER 和OnTimer的关系和使用
- 设计模式(策略模式,单例模式)
- 关于ListView上下滑动时背景变黑的问题
- Oracle operate tablespace
- HDOJ 2222 Keywords Search
- Android 获取下载文件的真实名字
- flash builder 调用wcf
- 防遗忘笔记:nmap 扫描服务器打开proxy服务没
- pig中各种sql语句的实现
- Exception Ljava/lang/UnsatisfiedLinkError 的解决方法
- Java 单例模式详解
- 用PHOTOSHOP给图片打马赛克
- 图像处理与计算机视觉:基础,经典以及最近发展