hdu----单词数
来源:互联网 发布:godaddy创建数据库 编辑:程序博客网 时间:2024/05/23 07:23
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend#
Sample Output
4
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int MAXN = 100000;struct Trie{ int next[26]; int val;} tree[MAXN];int nxt,sum;char str[MAXN],ss[100];void Insert(char *s){ int rt=0, len = strlen(s); for(int i=0; i < len; i++) { int c=s[i]-'a'; if(!tree[rt].next[c]) tree[rt].next[c]=nxt++; rt = tree[rt].next[c]; } if(!tree[rt].val) sum++; tree[rt].val++;}int main(){ while(gets(str)&&str[0]!='#') { sum=0; nxt = 1; memset(tree,0, sizeof(tree)); int len=strlen(str); for(int i=0;i<len;i++) { while(str[i++]==' '); i--; if(i==len) break; int j=0; while(str[i]!=' '&&i<len) ss[j++]=str[i++]; ss[j]='\0'; Insert(ss); } printf("%d\n",sum); } return 0;}
我的注释理解
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int MAXN = 100000;struct Trie{ int next[26]; int val;} tree[MAXN];int nxt,sum;char str[MAXN],ss[100];void Insert(char *s){ int rt=0, len = strlen(s); for(int i=0; i < len; i++)//这个循环是建立字典树的模板 { int c=s[i]-'a'; if(!tree[rt].next[c]) tree[rt].next[c]=nxt++; rt = tree[rt].next[c]; } if(!tree[rt].val)//tree[rt].val如果为0就说明是个新单词(即之前没出现过),单词数sum就加1 //如果大于0就说明是同一个单词(即出现过),sum不加 sum++; tree[rt].val++;}int main(){ while(gets(str)&&str[0]!='#') { sum=0; nxt = 1; memset(tree,0, sizeof(tree)); int len=strlen(str); for(int i=0;i<len;i++)//将单词拿出来建立字典树 { while(str[i++]==' '); i--; if(i==len) break; int j=0; while(str[i]!=' '&&i<len) ss[j++]=str[i++]; ss[j]='\0'; Insert(ss); } printf("%d\n",sum); } return 0;}
0 0
- HDU 2072 单词数
- hdu 2072 单词数
- hdu 2072 单词数
- HDU 2072 单词数
- HDU 2072 单词数
- Hdu 2072 - 单词数
- Hdu 2072 单词数
- hdu 2072-单词数
- hdu 单词数
- hdu 2072 单词数
- HDU 2072 单词数
- HDU 2072 单词数
- HDU 2072 单词数
- hdu 2072 单词数
- hdu 2072 单词数
- hdu 2072 单词数
- HDU 2072 单词数
- hdu 2082 单词数
- blender球体挖洞
- 基于WSN的智能节水灌溉系统设计方案
- 【项目记录】山东大学场馆管理系统之——需求分析、接口文档
- redis
- 018-Dockerfile指令(1)
- hdu----单词数
- HTML5 canvas之图片批量加载
- 019-Dockerfile指令(2)
- 函数指针和指针函数的区别
- 【强烈推荐】老生常谈-从输入url到页面展示到底发生了什么
- 在Eclipse中直接运行Mapreduce程序
- 020-Dockerfile指令(3)
- Ubuntu16.04环境下eclipse中导入robocode.jar包编译robocode、上传至github仓库操作小结
- 字符串间隔