字典树
来源:互联网 发布:网络用语狗粮图片 编辑:程序博客网 时间:2024/05/13 01:11
http://poj.org/problem?id=2418
值得注意的是:
C语言对于字符串的处理
首先,回车键按下以后,键盘缓冲区中存放的是字符\n,如果我们用getchar,那么我们读入的是字符“\n”;如果我们使用gets进行读取,那么“\n”将被作为表示的结束的控制字符读取。然后,gets会自动把这个\n转换成'\0'存储在字符串中。
另外,如果从终端读取一个字符串,gets会把最后的换行符\n换成\0,而fgets从stdin读入的时候,不会对\n进行处理。
字典树解法:#include<stdio.h>#include<string.h>#include<stdlib.h>#define N 130//ASCII total numberschar s[40];int sum=0;struct node { char str[100]; bool isword; int prefix; node *next[N]; node() { memset(str,0,sizeof(str)); //str=NULL; isword=false; prefix=0; memset(next,NULL,sizeof(next)); }}*root;void ins(char *a) { node *p=root; for(int i=0; a[i]; i++) { int x=a[i]; if(p->next[x]==NULL) p->next[x]=new node; p=p->next[x]; } p->prefix++; p->isword=true; strcpy(p->str,a);}void cnt(node *root) { node *p=root; if(p->isword==true) printf("%s %.4lf\n",p->str,(p->prefix)*100.0/sum); for(int i=0; i<N; i++) { if(p->next[i]!=NULL) { //p=p->next[i]; cnt(p->next[i]); } }}int main() { root=new node; sum=0; while(gets(s)) { if(strcmp(s,"") == 0){ printf("+++%s+++\n",s); break; } sum++; ins(s); memset(s,0,sizeof(s)); //printf("+++%d+++\n",sum); } cnt(root);}
0 0
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树。。
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- ABAP
- 学习日志
- Linux下修改用户bashrc添加自定义路径来加载动态库
- C/C++产生随机数
- 异常类(1)
- 字典树
- 第九章 APO管道
- C++中指定小数位数输出
- jsp中静态包含和动态包含的区别与联系
- 【九度OJ合集】P1000-P1049
- Objective-C中对象的初始化及内存分配
- MJRefresh类------>下拉刷新,上拉加载
- JavaScript
- 基于BS架构的聊天工具!暂时起名MyQQ!struts2.1.8+hibernate3.2+Spring3.0+ExtJs4.2+MySql+WebSocket