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. O-C相关-10-动态类型检查

    10-动态类型检查 1.动态绑定 1)OC 中方法的调用不由编译器决定,而由运行时决定 2)OC 中没有方法调用,只有消息接收. 一般称消息为选择器 2.动态类型检查 对象在运行时获得类型的能力称为内 ...

  2. dapper的一个小扩展以支持dataset

    废话不多,直接上方法 public static DataSet ExecuteDataSet(this IDbConnection cnn, IDbDataAdapter adapter, stri ...

  3. C语言链表全操作(增,删,改,查,逆序,递增排序,递减排序,链式队列,链式栈)

    一,数据结构——链表全操作: 链表形式: 其中,每个节点(Node)是一个结构体,这个结构体包含数据域,指针域,数据域用来存放数据,指针域则用来指向下一个节点: 特别说明:对于单链表,每个节点(Nod ...

  4. 学习笔记---C++析构函数心得

    1.动态分配的对象的析构函数 class man{ public: man(){ cout<<"man begin"<<endl; }; ~man(){ c ...

  5. HDU 5351 MZL's Border (规律,大数)

    [HDU 5351 MZL's Border]题意 定义字符串$f_1=b,f_2=a,f_i=f_{i-1}f_{i-2}$. 对$f_n$的长度为$m$的前缀$s$, 求最大的$k$满足$s[1] ...

  6. 九度OJ 1451 不容易系列之一 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1451 题目描述: 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好“一件”事情尚且不易,若 ...

  7. 24种设计模式--观察者模式【Observer Pattern】

     <孙子兵法>有云: “知彼知己,百战不殆:不知彼而知己,一胜一负:不知彼,不知己,每战必殆”,那怎么才能知己知彼呢?知己是很容易的,自己的军队嘛,很容易知道,那怎么知彼呢?安插间谍是很好 ...

  8. 解决Mysql的主从数据库没有同步的两种方法

    今天发现Mysql的主从数据库没有同步 先上Master库: mysql>show processlist;   查看下进程是否Sleep太多.发现很正常.show master status; ...

  9. Netty源码阅读(一) ServerBootstrap启动

    Netty源码阅读(一) ServerBootstrap启动 转自我的Github Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速 ...

  10. jQuery siblings()用法与实例。

    jQuery 的遍历方法siblings() $("给定元素").siblings(".selected") 其作用是筛选给定的同胞同类元素(不包括给定元素本身 ...