(字典树)(H1251)
来源:互联网 发布:图片大小批处理软件 编辑:程序博客网 时间:2024/06/05 21:46
处理链表,指针的时候很容易出错啊,,,。
#include<iostream>#include<stdlib.h>#include<math.h>#include<stdio.h>#include<algorithm>#include<queue>#include<string.h>#include<stack>#include<math.h>#include<string>#include<stdlib.h>#include<list>#include<vector>using namespace std;struct my{ int num; my *next[26];}*root;char c[13];void insert(char *str){ int i,j,k; my *newp,*p; p=root; for (i=0;*str;str++) { if (p->next[*str-'a']!=NULL) { p=p->next[*str-'a']; p->num++; } else { newp=new my; for (i=0;i<26;i++) { newp->next[i]=NULL; } p->next[*str-'a']=newp; p=p->next[*str-'a']; p->num=1; } }}int find(char str[]){ int i,j,k; my *p; p=root; for (;*str;str++) { if (p->next[*str-'a']==NULL) return 0; p=p->next[*str-'a']; } return p->num;}int main(){ int i,j,k; root=new my; for (i=0;i<26;i++) { root->next[i]=NULL; } root->num=0; while (gets(c)) { if (strcmp(c,"")==0) break; insert(c); } while (gets(c)) { cout<<find(c)<<endl; }}
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 8919 Accepted Submission(s): 3580
Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
注意:本题只有一组测试数据,处理到文件结束.
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
Sample Input
banana band bee absolute acm ba b band abc
Sample Output
2 3 1 0
Author
- (字典树)(H1251)
- Codevs 4189 字典(字典树Trie)
- 字典树(边改边抄)
- 字典树(Trie)
- 字典树(TrieTree)
- Trie(字典)树
- 字典树(非)
- hdu1251(字典树)
- POJ2503_Babelfish(字典树)
- hdu1671( 字典树)
- 字典树(Tries)
- uvalive3942(字典树)
- 字典树(模板)
- HDU_2846_Repository(字典树)
- 字典树(java)
- 字典树(Trie)
- 字典树(trie)
- 字典树(Trie)
- 第十周任务三之点类及多重派生
- 抄抄
- Android 程式开发:(九)使用活动栏 —— 9.3自定义ActionBar项目和程序图标
- POJ-2833(第k小的数变形)
- 防止UI界面被输入法遮挡(画面随输入法自适应)
- (字典树)(H1251)
- 让图片居中显示(PHP)
- rpm安装gcc
- applet
- c++预编译介绍
- 去掉 iframe 边框以及滚动条以及背景透明
- vi编辑器的文字说明
- Android培训班(103)内核入口汇编3
- 十大著名黑客-----查德-斯德尔曼