Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25356    Accepted Submission(s): 8280

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
text:100万长度,关键字1万,多个测试案例,用Trie匹配必定超时。Trie也可以多模式匹配,不多比AC自动机耗时太多。
Trie 超时代码:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int INF=0x5fffffff;
const int MS=;
const double EXP=1e-; struct node
{
int have;//根据情况灵活变化
node * next[];
}nodes[MS*]; //注意这个大小 尽量大一点 node *root;
int cnt,ans; char text[MS*]; node * add_node(int c)
{
node *p=&nodes[c];
for(int i=;i<;i++)
p->next[i]=NULL;
p->have=;
return p;
} void insert(char *str)
{
node *p=root;
int len=strlen(str);
for(int i=;i<len;i++)
{
int id=str[i]-'a';
if(p->next[id]==NULL)
{
p->next[id]=add_node(cnt++);
}
p=p->next[id];
}
p->have++;
}
void search(char *str)
{
node *p=root;
int len=strlen(str);
for(int i=;i<len;i++)
{
int id=str[i]-'a';
p=p->next[id];
if(p==NULL)
return ;
if(p->have)
{
ans+=p->have;
p->have=;
}
}
} int main()
{
int n,i,T;
scanf("%d",&T);
while(T--)
{
cnt=;
ans=;
root=add_node(cnt++);
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%s",text);
insert(text);
}
scanf("%s",text);
int len=strlen(text);
for(i=;i<len;i++)
{
search(text+i);
}
printf("%d\n",ans);
}
return ;
}

这题是AC自动机最经典的入门题。学会了kmp,Trie,就可以学习ac自动机了。

time : 280 ms

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int INF=0x5fffffff;
const int MS=;
const double EXP=1e-;
// AC自动机 KMP TRIE
struct node
{
bool isbad;
node *pre;
node * next[];
int n;
}nodes[MS*]; //注意这个大小 个数*每个的长度就不会访问非法内存 node *root;
int cnt,ans; char text[MS*]; node * add_node(int c)
{
node *p=&nodes[c];
for(int i=;i<;i++)
p->next[i]=NULL;
p->isbad=false;
p->pre=NULL;
p->n=;
return p;
} void insert(char *str)
{
node *p=root;
int len=strlen(str);
for(int i=;i<len;i++)
{
int id=str[i]-'a';
if(p->next[id]==NULL)
{
p->next[id]=add_node(cnt++);
}
p=p->next[id];
}
p->isbad=true;
p->n++; //终止节点
} void build()
{ // 在trie树上加前缀指针
for(int i=;i<;i++)
nodes->next[i]=root;
nodes->pre=NULL;
root->pre=nodes;
deque<node *> dq;
dq.push_back(root);
while(!dq.empty())
{
node *proot=dq.front();
dq.pop_front();
for(int i=;i<;i++)
{
node *p=proot->next[i];
if(p!=NULL)
{
node *pre=proot->pre;
while(pre)
{
if(pre->next[i]!=NULL) //NULL==0
{
p->pre=pre->next[i];
if(p->pre->isbad)
p->isbad=true;
break;
}
else
pre=pre->pre;
}
dq.push_back(p);
}
}
}
} void search(char *str)
{ //返回值为true,说明包含模式串
node *p=root;
int len=strlen(str);
for(int i=;i<len;i++)
{
int id=str[i]-'a';
while(p!=root&&p->next[id]==NULL)
{
p=p->pre;
}
p=p->next[id];
if(p==NULL)
{
p=root;
}
node *tp=p;
while(tp!=root&&tp->n!=)
{
ans+=tp->n;
tp->n=;
tp=tp->pre;
} /*
while(1) 是否包含模式串
{
if(p->next[id])
{
p=p->next[id];
if(p->isbad)
return true;
break;
}
else
p=p->pre;
}
*/
} //return false;
} int main()
{
int n,T;
scanf("%d",&T);
while(T--)
{
cnt=;
ans=;
add_node(cnt++);
root=add_node(cnt++);
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%s",text);
insert(text);
}
build();
scanf("%s",text);
search(text);
printf("%d\n",ans);
}
return ;
}
/*
节点p的前缀指针定义为:指向树中出现
过的S的最长的后缀(不能等于S)。
如果p节点匹配失败,该节点对应c,就沿着它的父节点的前缀指针走,直到某节点,其儿子节点
对应的字母也为c,把p节点的前缀指针指向该儿子节点。如果走到了root都没有找到,就指向root
*/
 
 

Keywords Search的更多相关文章

  1. 【HDU2222】Keywords Search AC自动机

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

  2. hdu2222 Keywords Search ac自动机

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

  3. HDU 2222 Keywords Search(AC自动机模版题)

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

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

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

  5. C++之路进阶——hdu2222(Keywords Search)

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

  6. HDU 2222 Keywords Search(查询关键字)

    HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...

  7. hdu----(2222)Keywords Search(ac自动机)

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

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

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

  9. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

随机推荐

  1. iOS开发中的Get请求和POST请求

    //Get请求一般为不涉及到用户的账号密码的网络请求,其中Get请求是等请求内容回来之后,才可以进行下一步的操作 - (void)requestWithGet{ //Get请求: //1.设置请求路径 ...

  2. Linux top和负载的解释

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法. top - 01:06:48 up  1:22,   ...

  3. emWin5.24 VS2008模拟LCD12864 stm32 RTX移植 【worldsing笔记】

      emWin for 12864 并口移植 源代码下载:RTX_emWin5.24_Keil_VS2008-20141122.zip   硬件环境: CPU: stm32f103ve LCD:st7 ...

  4. mongodb索引操作

    创建索引 db.table.ensureIndex({name:1}) 创建联合索引 db.table.ensureIndex({"table.name":1,"tabl ...

  5. Linux下生成动态链接库是否必须使用 -fPIC 的问题[转]

    在 Linux 下制作动态链接库,“标准” 的做法是编译成位置无关代码(Position Independent Code,PIC),然后链接成一个动态链接库.经常遇到的一个问题是 -fPIC 是不是 ...

  6. 【转】google推出的SwipeRefreshLayout下拉刷新用法

    SwipeRefreshLayout是Google在support v4 19.1版本的library更新的一个下拉刷新组件,实现刷新效果更方便. 使用如下: 1.先下载android-support ...

  7. Spring Bean生命周期

    1.Bean的建立:BeanFactory容器寻找Bean的定义信息,读取Bean定义文件,并将其实例化,生成各个Bean实例.2.属性注入:使用依赖注入,Spring按照Bean定义信息配置Bean ...

  8. MVC6与Asp.net5

    http://www.cnblogs.com/n-pei/p/4263148.html https://blogs.msdn.microsoft.com/scottgu/2015/04/30/asp- ...

  9. PHP安装环境,服务器不支持curl_exec的解决办法

    转自:http://jingyan.baidu.com/article/00a07f38909c6b82d028dc83.html windows下开启方法: 拷贝PHP目录中的libeay32.dl ...

  10. PL/pgSQL学习笔记之三

    http://www.postgresql.org/docs/9.1/static/plpgsql-overview.html 39.1.2. Supported Argument and Resul ...