hdu----(2222)Keywords Search(trie树)
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35683 Accepted Submission(s): 11520
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.
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.
5
she
he
say
shr
her
yasherhs
- /*hdu 2222 字典树*/
- //#define LOCAL
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- using namespace std;
- struct Trie
- {
- struct Trie *next[];
- int tail;
- };
- char str[];
- char ss[];
- void in_Trie(char *s,Trie *root)
- {
- Trie *cur=root,*newcur;
- for(int i=;s[i]!='\0';i++)
- {
- if(cur->next[s[i]-'a']==NULL)
- {
- newcur=new Trie; //(Trie*)malloc(sizeof(sizeof(Trie)));
- for(int j=;j<;j++)
- newcur->next[j]=NULL;
- newcur->tail=;
- cur->next[s[i]-'a']=newcur;
- }
- cur=cur->next[s[i]-'a'];
- }
- cur->tail++;
- }
- int query(char *s,Trie *root)
- {
- int i=,cnt=;
- Trie *cur;
- for(int j=;s[j]!='\0';j++)
- {
- cur=root;
- for(i=j;s[i]!='\0';i++){
- if(cur->next[s[i]-'a']!=NULL){
- cur=cur->next[s[i]-'a'];
- cnt+=cur->tail;
- cur->tail=; //只需求出第一次出现的
- }
- else break;
- }
- }
- return cnt;
- }
- void dele(Trie *root)
- {
- for(int i= ; i< ; i++ )
- if(root->next[i]!=NULL)
- dele(root->next[i]);
- // free(root);
- delete root;
- }
- int main()
- {
- #ifdef LOCAL
- freopen("test.in","r",stdin);
- #endif
- int t,i,n;
- Trie *root;
- scanf("%d",&t);
- while(t--)
- {
- root = new Trie ;
- for(int j=;j<;j++)
- root->next[j]=NULL;
- root->tail=;
- scanf("%d",&n);
- for(i=;i<n;i++){
- scanf("%s",str);
- in_Trie(str,root);
- }
- scanf("%s",ss);
- printf("%d\n",query(ss,root));
- dele(root);
- }
- return ;
- }
hdu----(2222)Keywords Search(trie树)的更多相关文章
- HDU 2222 Keywords Search(查询关键字)
HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 Keywords Search (AC自动机)
题意:给你一些模式串,再给你一串匹配串,问你在匹配串中出现了多少种模式串,模式串可以相同 AC自动机:trie树上进行KMP.首先模式串建立trie树,再求得失配指针(类似next数组),其作用就是在 ...
- HDU 2222 Keywords Search(瞎搞)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 Keywords Search(AC自动机)
/* 啥也不说了,直接套模板... */ 1 #include<iostream> #include<map> #include<string> #include& ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- HDU 2222 Keywords Search(AC自己主动机模板题)
题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数. 思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以. #include<cstdio> #incl ...
- hdu 2222 Keywords Search 模板题
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
随机推荐
- Cheatsheet: 2013 10.09 ~ 10.23
Other 10 Basic Linux Networking and Monitoring Commands You Should Know A simple, portable yet effic ...
- jquery之event与originalEvent的关系、event事件对象用法浅析
在jquery中,最终传入事件处理程序的 event 其实已经被 jQuery 做过标准化处理, 其原有的事件对象则被保存于 event 对象的 originalEvent 属性之中, 每个 even ...
- [HDOJ5935]Car(精度,数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5935 题意:有个老司机在开车, 开车过程中车的速度是不减的. 交警记录了这个老司机在nn个时间点的位置 ...
- Object-C: 枚举
摘自:http://coffeeandsandwich.com/?p=8 在 iOS 6 和 Mac OS X 10.8 以前,定义枚举类型的方式如下: typedef enum the_enum_n ...
- GitHub上不错的Android开源项目(三)
收集相关系列资料,自己用作参考,练习和实践.小伙伴们,总有一天,你也能写出 Niubility 的 Android App :-) GitHub上不错的Android开源项目(一):http://ww ...
- 转 C编译: 使用gdb调试
C编译: 使用gdb调试 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! gdb是the GNU Debugger的简称.它是 ...
- JCO事务管理
/* * 标准对账单过账 * @account 标准对账单号 * @year 年度 */ public List<String> doAccountStatmentPost(String ...
- [转载] DevOps年中盘点:国外最受欢迎的10篇技术文章
本文根据高效运维系列微信群的群友投稿整理而成.“高效运维”公众号作为本系列群的官方唯一公众号,原创并独家首发. 欢迎关注“高效运维”公众号,以免费参加「运维讲坛」每月一次的线下交流活动:并抢先赏阅干货 ...
- poj3855Blast the Enemy!(多边形重心)
链接 #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> ...
- Spring对Hibernate事务管理
谈Spring事务管理之前我们想一下在我们不用Spring的时候,在Hibernate中我们是怎么进行数据操作的.在Hibernate中 我们每次进行一个操作的的时候我们都是要先开启事务,然后进行数据 ...