Keywords Search

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

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自动机
AC代码:
 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Node_Tree
{
int cnt;
struct Node_Tree *child[];
struct Node_Tree *fail;
}Node;
Node *root;
char keywd[];
char decpt[];
Node *q[];
int tail = , head = ;
void insert()
{
if(keywd == NULL)
return ;
int i;
char *p = keywd;
Node *t = root;
while(*p != '\0')
{
if(t->child[*p - 'a'] == NULL)
{
Node *temp = (Node *)malloc(sizeof(Node));
memset(temp, , sizeof(Node));
for(i = ; i < ; i ++)
{
temp->child[i] = NULL;
}
temp->cnt = ;
temp->fail = NULL;
t->child[*p - 'a'] = temp;
}
t = t->child[*p - 'a'];
p ++;
}
t->cnt ++;
} void getfail()
{
int i;
q[tail++] = root;
while(tail != head) //BFS;
{
Node *p = q[head++];
Node *temp = NULL;
for(i = ; i < ; i ++)
{
if(p->child[i] != NULL)
{
if(p == root)
{
p->child[i]->fail = root;
}
else
{
temp = p->fail;
while(temp != NULL)
{
if(temp->child[i] != NULL)
{
p->child[i]->fail = temp->child[i];
break ;
}
temp = temp->fail;
}
if(temp == NULL)
p->child[i]->fail = root;
}
q[tail++] = p->child[i];
}
}
}
} int search()
{
int i, ret = ;
char *p = decpt;
Node *t = root;
while(*p != '\0')
{
while(t->child[*p - 'a'] == NULL && t != root)
t = t->fail;
t = t->child[*p - 'a'];
if(t == NULL)
t = root;
Node *temp = t;
while(temp != root && temp->cnt != -)
{
ret += temp->cnt;
temp->cnt = -;
temp = temp->fail;
}
p ++;
}
return ret;
} int main(int argc, char const *argv[])
{
int c, i, t;
scanf("%d", &c);
Node TREEROOT;
root = &TREEROOT;
while(c --)
{
for(i = ; i < ; i ++)
{
root->child[i] = NULL;
root->cnt = ;
root->fail = NULL;
}
tail = head = ;
memset(decpt, , sizeof(decpt));
memset(keywd, , sizeof(keywd));
scanf("%d", &t);
while(t --)
{
scanf("%s", keywd);
insert();
memset(keywd, , sizeof(keywd));
}
getfail();
scanf("%s", decpt);
printf("%d\n", search());
}
return ;
}

统计难题 HDOJ--2222的更多相关文章

  1. 统计难题 HDOJ --1251

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  2. HDOJ ——统计难题

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  3. hdoj 1251 统计难题 【字典树】

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  4. HDU 1251 统计难题(字典树)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  5. hduoj 1251 统计难题

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  6. HDU 1251统计难题

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  7. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  8. hdu1251 统计难题

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1251 题目: 统计难题 Time Limit: 4000/2000 MS (Java/Othe ...

  9. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  10. ACM:统计难题 解题报告-字典树(Trie树)

    统计难题 Time Limit:2000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status ...

随机推荐

  1. JS Attribute属性操作

    Attribute是属性的意思,文章仅对部分兼容IE和FF的Attribute相关的介绍. attributes:获取一个属性作为对象 getAttribute:获取某一个属性的值setAttribu ...

  2. [002] The Perks of Being a Wallflower - 读后记

    The Perks of Being a Wallflower 今天(2015年10月30日 18:26:17)读完"The Perks of Being a Wallflower" ...

  3. 在ARM Linux 使用 Valgrind

    Linux valgrind 移植到ARM-Linux  一.Cross-Compile/交叉编译 (1)下载及解压Valgrind-3.11 (2)修改confirure 将armv7*)修改为ar ...

  4. vi简单快键命令

    进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n filename :打开文件,并将光标置于第n行首 vi + filename :打开文件,并将光标置于最后 ...

  5. svn-主副分支使用

    主改bug 副加功能, :主合并到副(在副中切换主分支),副调试成功,合并回主(在主切换回副分支) 奇葩的实现了需求 主改bug 副加功能, :主合并到副(在副中切换主分支),副调试成功,合并回主(在 ...

  6. javascript笔记之正则表达式

    1.在js正则表达式特殊的需要转义的字符有: ^ $ . * + ?  = ! : | \ / ( ) [ ] { } 但实际应用中,还要根据实际情况来判断,以上字符可能不需要转义,也可能不止以上字符 ...

  7. Uniqueidentifier数据类型

    一.Uniqueidentifier数据类型 可存储16字节的二进制值 Uniqueidentifier用来存储一个全局唯一标识符,即GUID.GUID是唯一的二进制数:世界上的任何两台计算机都不会生 ...

  8. php 接收二进制流转换成图片

    php 接收二进制流转换成图片,图片类imageUpload.php如下: <?php /** * 图片类 * @author http://blog.csdn.net/haiqiao_2010 ...

  9. nginx 反向代理 odoo, 速度提升

    1: sudo apt-get  install nginx 2:  add server: 在/etc/nginx/sites-enabled 中增加一个可用的server. 文件如下图所示 3:重 ...

  10. C语言-07其它相关

    预处理指令 /* 不带参数的宏定义 1.所有的预处理指令都是以#开头 2.预处理指令分3种 1> 宏定义 2> 条件编译 3> 文件包含 3.预处理指令在代码翻译成0和1之前执行 4 ...