HDU 2222 Keywords Search(查询关键字)

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 131072/131072 K (Java/Others)

【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想把这个功能加入他的图片搜索系统。

每张图都有一串很长的描述,当用户输入一些关键字去搜索图片时,系统会匹配描述图片的关键字并显示有最多关键字匹配的图片。

【Input】

【输入】

First line will contain one integer means how many cases will follow by.

Each case will contain one 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.

第一行有一个整数随后测试用例数量。

每个测试用例。

每个测试用例有一个整数N表示关键字数量与随后有N个关键字。(N <= 10000)

每个关键字只包含字符'a'-'z',并且其长度不大于50。

最后一行是此图片的描述,并且其长度不大于1000000。

【Output】

【输出】

Print how many keywords are contained in the description.

输出描述中包含的关键字数量。

【Sample Input - 输入样例】

【Sample Output - 输出样例】

1

5

she

he

say

shr

her

yasherhs

3

【题解】

AC自动机入门题,不过数据不全面,里面没有考虑回头匹配子串情况的数据(个人觉得这是KMP的AC自动机的主要不同之处,Trie图不会,哪天被打脸了再说)

例如:

1

2

abc

b

abc

输出1的代码居然也能A……而且貌似这个问题被提出来很久了,一直没有改善。

注意匹配的是关键字数量,关键字可以相同,不过可以看成匹配一个关键字就去掉一个关键字。

【代码 C++】

 #include <cstdio>
#include <cstring>
#include <queue>
struct Node{
int next[], fail, isWord;
}tr[];
int it;
char text[]; void build(){
it = ;
memset(tr, , sizeof(tr));
int n, i, j, w;
char word[];
scanf("%d", &n), getchar();
while (n--){
gets(word);
for (i = j = ; word[i]; ++i){
w = word[i] - 'a';
if (!tr[j].next[w]) tr[j].next[w] = ++it;
j = tr[j].next[w];
}
++tr[j].isWord;
}
}
void setFail(){
std::queue<int> q, last;
int now, pre, next, i, j;
for (i = ; i < ; ++i){
if (tr[].next[i]) q.push(tr[].next[i]), last.push();
} while (!q.empty()){
now = q.front(); q.pop();
pre = last.front(); last.pop();
for (i = ; i < ; ++i){
next = tr[now].next[i];
if (next){
q.push(next);
if (tr[pre].next[i]) j = pre;
else j = ;
if (tr[j].next[i]){
tr[next].fail = tr[j].next[i];
last.push(tr[j].next[i]);
}
else last.push();
}
}
}
}
int fid(){
gets(text);
int i, iTR, opt, w, temp;
for (i = iTR = opt = ; text[i]; ++i){
w = text[i] - 'a';
if (tr[iTR].next[w]) iTR = tr[iTR].next[w];
else{
if (iTR) --i;
iTR = tr[iTR].fail;
}
for (temp = iTR; ~tr[temp].isWord; temp = tr[temp].fail){
opt += tr[temp].isWord;
tr[temp].isWord = -;
}
}
return opt;
}
int main(){
int t;
scanf("%d", &t);
while (t--){
build();
setFail();
printf("%d\n", fid());
}
return ;
}

自己面向图片编程的代码……

 #include <cstdio>
#include <cstring>
#include <queue>
struct Node{
int next[], fail, isWord;
}tr[];
int it; void build(){
it = ;
memset(tr, , sizeof(tr));
int n, i, j, w;
char word[];
scanf("%d", &n), getchar();
while (n--){
gets(word);
for (i = j = ; word[i]; ++i){
w = word[i] - 'a';
if (!tr[j].next[w]) tr[j].next[w] = ++it;
j = tr[j].next[w];
}
++tr[j].isWord;
}
}
void setFail(){
std::queue<int> q;
int now, next, i;
for (i = ; i < ; ++i) if (tr[].next[i]) q.push(tr[].next[i]); while (!q.empty()){
now = q.front(); q.pop();
for (i = ; i < ; ++i){
if (next = tr[now].next[i]){
q.push(next);
tr[next].fail = tr[tr[now].fail].next[i];
}
else tr[now].next[i] = tr[tr[now].fail].next[i];
}
}
}
int fid(){
char text[];
gets(text);
int i, iTR, opt, temp;
for (i = iTR = opt = ; text[i]; ++i){
iTR = tr[iTR].next[text[i] - 'a'];
for (temp = iTR; ~tr[temp].isWord; temp = tr[temp].fail){
opt += tr[temp].isWord;
tr[temp].isWord = -;
}
}
return opt;
}
int main(){
int t;
scanf("%d", &t);
while (t--){
build();
setFail();
printf("%d\n", fid());
}
return ;
}

参考kuangbin巨后稍微简化的代码

HDU 2222 Keywords Search(查询关键字)的更多相关文章

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

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

  2. HDU 2222 Keywords Search(瞎搞)

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

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

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

  4. hdu 2222 Keywords Search 模板题

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

  5. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  6. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  7. hdu 2222 Keywords Search

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:裸AC自动机,直接贴代码做模板 #include<stdio.h> #includ ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...

  9. 【刷题】HDU 2222 Keywords Search

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

随机推荐

  1. JNI 回调小记

    javah在eclipse中设置参数:location(javah.exe的位置)working dir(${project_loc}/src) -classpath .;./classes -d $ ...

  2. Oracle DB SQL 性能分析器

    • 确定使用SQL 性能分析器的优点 • 描述SQL 性能分析器工作流阶段 • 使用SQL 性能分析器确定数据库更改所带来的性能改进 SQL 性能分析器:概览 • 11g 的新增功能 • 目标用户:D ...

  3. bootstrap导航条在手机上默认展开二级目录,必须用setTimeout才能实现

    bootstrap导航条在手机上默认展开二级目录,必须用setTimeout才能实现 $(document).ready(function() { $('.dropdown').hover(funct ...

  4. asp显示记录条数

    Sql = "select * from xin126 where ID=" & id Rs.Open Sql,Conn,1,1 %> 共有<strong st ...

  5. exe文件打开方式(恢复EXE文件关联)

    文件关联损坏常常是计算机病毒造成的,目前网络上有很多相关修复工具,相对来说,System Repair Engineer 支持的修复格式是比较齐全的,这个工具可以在http://www.kztechs ...

  6. ServiceStack.Redis之IRedisClient 03_转

    事实上,IRedisClient里面的很多方法,其实就是Redis的命令名.只要对Redis的命令熟悉一点就能够非常快速地理解和掌握这些方法,趁着现在对Redis不是特别了解,我也对着命令来了解一下这 ...

  7. java 堆栈 静态

    所以静态变量和非静态变量的区别就在于静态变量可以用来计数,而非静态变量则不行. 理解了内存,就理解了一切,就理解了各种各样的语言.所有的语言无非都是这样:局部变量分配内存永远在栈里面,new出来的东西 ...

  8. servlet 笔记

    Servlet的作用是接收浏览器传给服务端的请求(request),并将服务端处理完的响应(response)返回给用户的浏览器,浏览器和服务端之间通过http协议进行沟通,其过程是浏览器根据用户的选 ...

  9. QQ空间开放平台开发教程-SDK和API的使用

    <?php /** * OpenAPI V3 SDK 示例代码,适用于大部分OpenAPI.如果是上传文件类OpenAPI,请参考本SDK包中的“Test_UploadFile.php”文件中的 ...

  10. 【转载】使用barman备份PostgreSQL

    什么是barman Barman (备份和恢复管理器) 是 PostgreSQL 数据库服务器中非常方便的备份和恢复工具,允许远程备份多个服务器,允许从一个备份集中一个命令就恢复数据库.同时还可以对多 ...