hdu-2072-单词数(字典树)
来源:互联网 发布:ubuntu快捷键 编辑:程序博客网 时间:2024/06/05 10:27
题目:单词数(传送门)
字典树水题:初学的可以看看模板
模板:http://blog.csdn.net/forever_kirito/article/details/76620722
参考题:http://blog.csdn.net/forever_kirito/article/details/76610719
这题字符串的处理要注意一下:
code:
#include<cstdio>#include<cstring>#include<string>using namespace std;struct node{ int cnt; node *next[26]; node(){ cnt=0; for(int i=0;i<26;i++){ next[i] = NULL; } }};node *root;int ans = 0;void Insert(char *str){ //printf("%s\n",str); node *p=root; int len = strlen(str); for(int i=0;i<len;i++){ int id = str[i] - 'a'; if(p->next[id]!=NULL){ p = p->next[id]; } else{ p->next[id]=new node; p=p->next[id]; } } if(p->cnt!=1){ p->cnt=1;ans++; }}void del(node *p){ for(int i=0;i<26;i++) if(p->next[i]!=NULL) del(p->next[i]); delete(p);}int main(){ char ss[1000]; while(gets(ss)&&strcmp(ss,"#")!=0){ root=new node; char str[100]; int len=strlen(ss); ss[len]='*';///让最后一个单词插入到字典树 int t=0; bool first=false;///在输入多个空格时不读入空格 for(int i=0;i<=len;i++){ if(isalpha(ss[i])){ str[t++]=ss[i]; first=true; } else if(first){ str[t]='\0'; t=0; Insert(str); first=false; } } printf("%d\n",ans); ans=0; del(root); } return 0;}
阅读全文
2 0
- hdu-2072-单词数(字典树)
- hdu 2072 单词数(字典树)
- hdu 2072 单词数 字典树
- HDU 2072 单词数 字典树
- hdu 2072 单词数 字典树
- hdu 2072-单词数(字典树)
- hdu 题目2072 单词数 (字典树)
- hdu 2072 单词数(字典树入门题)
- hdu2072 单词数(字典树)
- hdu 2072 单词数(Trie树)
- hdu 2846 【字典树】单词子串的匹配数
- 单词数(hdu2072字典树)
- 单词数 (HDU_2072) 字典树
- HDU 2072 单词数
- hdu 2072 单词数
- hdu 2072 单词数
- HDU 2072 单词数
- HDU 2072 单词数
- Fibonacci 数列O(logn)解法
- 进程管理
- 匿名函数
- 视频编码(H264概述)
- Nginx负载均衡(架构之路)
- hdu-2072-单词数(字典树)
- 使用spark streaming报错ERROR DFSClient: Failed to close inode xxxx
- JavaScript用穷举写百钱买百鸡问题
- 抽象数据类型(ADT)
- 4. Median of Two Sorted Arrays
- [python3.6 flask web学习]web服务器基本结构
- recyclere 点击事件
- Bootstrap 写的webview 在安卓里图片失真,变形
- java数据库初探