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. 用ContentProvider向系统增加联系人

    发现对系统的联系人进行操作的api很乱,感觉逻辑有点不清楚...... 主要用到这4个类: android.provider.ContactsContract.CommonDataKinds.Emai ...

  2. mysql 主从 Got fatal error 1236 from master when reading data from binary log: 'Could not find first 错误

    本地MySQL环境,是两台MySQL做M-M复制.今天发现错误信息: mysql 5.5.28-log> show slave status\G ************************ ...

  3. Linux下的visudo命令

    新建了一个test账号,su 后使用visudo命令,发现不可用,退出putty,直接用root登录,可以用了,小白了,visudo只能用root执行,简单写下visudo命令吧 visudo后得到的 ...

  4. ECMAScript位操作符

    在ECMAScript中,有少数的几个操作符可以对二进制位进行直接操作,这几个操作符本身直接对二进制进行操作,所有它们的本身是非常效率的,学习这一段有助于以后的优化以及理解. ECMAScript中采 ...

  5. yii2源码学习笔记(二十)

    Widget类是所有部件的基类.yii2\base\Widget.php <?php /** * @link http://www.yiiframework.com/ * @copyright ...

  6. 使用GetLogicalDriveStrings获取驱动器根路径

    使用GetLogicalDriveStrings获取驱动器根路径,并使用自定义的GetDriveInfo函数获取驱动器的属性. VS2012 + win7 x64下调试通过. #include < ...

  7. Web模板引擎本质前奏

    执行字符串表示的函数,并为该函数提供全局变量: #! /usr/bin/env python3 namespace = {'name': 'zingp', 'data': [16, 19, 25]} ...

  8. 读《CSCW的一种建模与实现方法》

    这篇论文为我们描述了作者构建的一种基于交互.活动.协作三层结构的协同工作模型,并提出了一种采用“镜头焦点”和“自由交互”相结合的协作模型实现方法. 计算机支持的协同工作就是利用计算机技术将时间上分离. ...

  9. 安卓手机无法连接VPN的解决办法

    这篇不能算是技术博客吧,但是在网上很难找到解决方案,至少我找了好久也没弄好.. 三种方案,因机而异,我就长话短说了: 一. "/system/xbin" 和 "/syst ...

  10. POJ 2442 Sequence 优先队列

    题目: http://poj.org/problem?id=2442 #include <stdio.h> #include <string.h> #include <q ...