hdu----(2848)Repository(trie树变形)
Repository
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2538 Accepted Submission(s): 990
Problem Description
When you go shopping, you can search in repository for avalible merchandises by the computers and internet. First you give the search system a name about something, then the system responds with the results. Now you are given a lot merchandise names in repository and some queries, and required to simulate the process.
Input
There is only one case. First there is an integer P (1<=P<=10000)representing the number of the merchanidse names in the repository. The next P lines each contain a string (it's length isn't beyond 20,and all the letters are lowercase).Then there is an integer Q(1<=Q<=100000) representing the number of the queries. The next Q lines each contains a string(the same limitation as foregoing descriptions) as the searching condition.
Output
For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.
Sample Input
20 ad ae af ag ah ai aj ak al ads add ade adf adg adh adi adj adk adl aes 5 b a d ad s
Sample Output
0 20 11 11 2
Source
2009 Multi-University Training Contest 4 - Host by HDU
题意: 给出一些字符,然后寻问一个字符,在给出字符里能找到子串的个数..
代码:
1 //#define LOCAL
2 #include<cstdio>
3 #include<cstring>
4 typedef struct node
5 {
6 struct node *child[26];
7 int cnt; //作为统计
8 int id;
9 }Trie;
10
11 void Insert(char *s,Trie *root,int id)
12 {
13 int pos,i;
14 Trie *cur=root,*curnew;
15 for(;*s!=' ';s++)
16 {
17 pos=*s-'a';
18 if(cur->child[pos]==NULL)
19 {
20 curnew = new Trie;
21 for( i=0; i<26;i++)
22 curnew->child[i]=NULL;
23 curnew->cnt=0;
24 curnew->id=0;
25 cur->child[pos]=curnew;
26 }
27 cur=cur->child[pos];
28 if(cur->id!=id) //避免同一个单词重复计算子串
29 {
30 cur->cnt++;
31 cur->id=id;
32 }
33 }
34 }
35
36 int query(char *s, Trie *root)
37 {
38 int pos;
39 Trie *cur=root;
40 while(*s!=' ')
41 {
42 pos=*s-'a';
43 if(cur->child[pos]==NULL)
44 return 0;
45 cur=cur->child[pos];
46 s++;
47 }
48 return cur->cnt;
49 }
50 void del(Trie *root)
51 {
52 Trie *cur=root;
53 for(int i=0;i<26;i++)
54 {
55 if(cur->child[i]!=NULL)
56 del(cur->child[i]);
57 }
58 delete cur;
59 return ;
60 }
61 char str[22];
62 int main()
63 {
64 #ifdef LOCAL
65 freopen("test.in","r",stdin);
66 #endif
67 int n,m,i;
68 scanf("%d",&n);
69 Trie *root=new Trie;
70 for( i=0;i<26;i++)
71 root->child[i]=NULL;
72 root->cnt=0;
73 while(n--)
74 {
75 scanf("%s",str);
76 for(i=0 ; str[i]!=' ' ;i++)
77 Insert(str+i,root,n+1);
78 }
79 scanf("%d",&m);
80 while(m--)
81 {
82 scanf("%s",str);
83 printf("%dn",query(str,root));
84 }
85 del(root);
86 return 0;
87 }
- PHP实用功能——modern PHP读书笔记(一)
- ModernPHP读书笔记(二) ——PHP开发标准
- iBatis.Net(6):Data Map(深入)
- iBatis.Net(5):Data Map(了解)
- ModernPHP读书笔记(三)——PHP的良好实践
- PHP开发过程的那些坑(一) ——对象拷贝
- PHP开发过程的那些坑(二) ——PHP empty函数
- Thinking in SQL系列之数据挖掘Apriori关联分析再现啤酒尿布神话
- PHP开发过程的那些坑(三) ——PHParray_shift函数
- CSS3弹性盒布局
- iBatis.Net(4):DataMapper API
- PHP开发过程的那些坑(四) ——PDO bindParam函数
- iBatis.Net(3):创建SqlMapper实例
- PHP开发过程的那些坑(五) ——PHP的empty()
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 汇编程序调用c函数为什么需要设置栈?
- Uboot到底如何启动内核
- Flink部署及作业提交(On YARN)
- Flink部署及作业提交(On Flink Standalone)
- MySQL题集
- 《深入浅出SQL》问答录
- Python_字典实现简单预约系统
- java_二维数组简单快递系统
- RabbitMQ六种工作模式
- Java生成随机姓名、性别和年龄
- 商业数据分析从入门到入职(8)Python模块、文件IO和面向对象
- 7.13论坛常见问题以及解决办法
- 移植SDL到JZ2440显示BMP图片
- Vue图片验证码-自定义组件高级版
- Angular页面调试一个有用的小技巧 - normalizeDebugBindingName和normalizeDebugBindingValue