博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu2222-Keywords Search 【AC自动机】
阅读量:7101 次
发布时间:2019-06-28

本文共 4339 字,大约阅读时间需要 14 分钟。

Keywords Search

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

Total Submission(s): 49940    Accepted Submission(s): 16020

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自动机的入门版。求多个模式串在母串出现的总个数。
思路:参考
三步走,第一步建立字段数,把模式串整合为一棵树;第二步最重要,为各节点初始化fail指针,bfs搜索,功能类似于kmp的next数组,root的fail指向null,第二层指向root,第三层以后,判断其父亲的fail是否有同样的儿子,有就fail指过去那个儿子,没有就找其父亲的fail的fail,循环至root,没办法,就指向root(见下图);第三步就是搜索,与第二步类似,沿着fail指针找就对了,具体看代码。
代码:
1 #include 
2 #include
3 using namespace std; 4 5 #define MAXLEN 1000005 6 #define N 26 7 #define QUELEN 500005 8 #define SLEN 55 9 10 struct Node 11 { 12 Node *word[N]; 13 Node *fail; 14 int cnt; 15 Node (): cnt (0), fail(NULL) 16 { 17 memset (word, NULL, sizeof (word)); 18 } 19 }*root; 20 21 int n; 22 char str[MAXLEN]; 23 24 void Insert (char*); 25 void Init (); 26 void BuildACAutomation (); 27 int ACSearch (char*); 28 29 int main () 30 { 31 //freopen("D:\\input.in","r",stdin); 32 int T; 33 scanf ("%d", &T); 34 while (T--) 35 { 36 Init (); 37 BuildACAutomation (); 38 scanf ("%s", str); 39 printf ("%d\n", ACSearch (str)); 40 } 41 return 0; 42 } 43 void Insert (char *s) 44 { 45 Node *p = root; 46 int i = 0; 47 while (s[i]) 48 { 49 int tmp = s[i] - 97; 50 if (!p->word[tmp]) 51 { 52 p->word[tmp] = new Node; 53 } 54 p = p->word[tmp]; 55 i++; 56 } 57 p->cnt++; 58 } 59 void Init () 60 { 61 char s[SLEN]; 62 root = new Node; 63 scanf ("%d", &n); 64 for (int i = 0; i < n; i++) 65 { 66 scanf ("%s", s); 67 Insert (s); 68 } 69 } 70 void BuildACAutomation () 71 { 72 Node *que[QUELEN]; 73 int front = 0, rear = 0; 74 for (int i = 0; i < N; i++) 75 { 76 if (root->word[i])//处理第二层,使其fail指针都指向root,而root的fail默认指向null 77 { 78 que[rear++] = root->word[i]; 79 root->word[i]->fail = root; 80 } 81 } 82 while (front < rear)//处理其他层 83 { 84 Node *p = que[front++], *cur; 85 for (int i = 0; i < N; i++) 86 { 87 if (p->word[i]) 88 { 89 que[rear++] = p->word[i]; 90 cur = p->fail; 91 while (cur) 92 { 93 if (cur->word[i]) 94 { 95 p->word[i]->fail = cur->word[i]; 96 break; 97 } 98 cur = cur->fail; 99 }100 if (!cur)101 {102 p->word[i]->fail = root;103 }104 }105 }106 }107 }108 int ACSearch (char *s)109 {110 int i = 0, ans = 0;111 Node *p = root, *cur;112 while (s[i])113 {114 int tmp = s[i] - 97;115 while ((!p->word[tmp]) && p != root)116 {117 p = p->fail;118 }119 p = p->word[tmp];120 if (!p)121 {122 p = root;123 }124 cur = p;125 while (cur && (~cur->cnt))126 {127 ans += cur->cnt;128 cur->cnt = -1;129 cur = cur->fail;130 }131 i++;132 }133 return ans;134 }

 

转载地址:http://tmkhl.baihongyu.com/

你可能感兴趣的文章
【java】java中替换中括号[ ]操作
查看>>
重看计算机基础1:数据线、地址线,按字、按字节寻址。
查看>>
oracle 11g亿级复杂SQL优化一例(数量级性能提升)
查看>>
Qt Md5应用示例
查看>>
tensorflow 笔记11:tf.nn.dropout() 的使用
查看>>
路由事件
查看>>
WPF实现选项卡效果(1)——使用AvalonDock
查看>>
字符 16进制 字节 关系
查看>>
C# 给现有PDF文档添加页眉、页脚
查看>>
『算法学习』FPN:feature pyramid networks for object detection
查看>>
K-近邻算法(KNN)
查看>>
java服务端微信小程序支付
查看>>
flip 翻转效果 css3实现
查看>>
Cocos Creater 监听程序到后台和重新到前台
查看>>
Windows 10 应用创建模糊背景窗口的三种方法
查看>>
Python类与标准库
查看>>
学生表、课程表、 成绩表 、教师表sql练习
查看>>
vue inheritAttrs、$attrs和$listeners使用
查看>>
诗歌的分类
查看>>
nexus maven私服搭建
查看>>