杭电2072 单词数(Trie tree或者STL)
来源:互联网 发布:linux 查看网卡状态 编辑:程序博客网 时间:2024/05/18 12:34
->题目请戳这里<-
题目大意:中文题,不解释。
题目分析:字典树水过~
详情请见代码:
#include <iostream>#include<cstdio>#include<map>#include<string>#include<cstring>#include<cstdlib>#include<set>using namespace std;typedef struct node{ struct node *next[26]; int end;}tree;char s[100005];int len;int deep;int cnt;void init(tree *t){ t->end = 0; int i; for(i = 0;i < 26;i ++) t->next[i] = NULL;}void del(tree *t){ int i; for(i = 0;i < 26;i ++) if(t->next[i] != NULL) del(t->next[i]); free(t);}void build(tree *t,int id){ if(t->next[s[id] - 'a'] == NULL) { t->next[s[id] - 'a'] = (tree *)malloc(sizeof(tree)); init(t->next[s[id] - 'a']); } if(s[id + 1] == ' ' || s[id + 1] == '\0') { if(t->next[s[id] - 'a']->end == 0) { t->next[s[id] - 'a']->end = 1; cnt ++; } deep = id + 1; return; } else build(t->next[s[id] - 'a'],id + 1);}int main(){ int i; tree *root = NULL; while(gets(s) && strcmp(s,"#") != 0) { cnt = 0; root = (tree *)malloc(sizeof(tree)); init(root); len = strlen(s); int flag; for(i = 0;i < len;i ++) { if(s[i] == ' ') { while(s[i] == ' ') i ++; i --; } else { deep = 0; build(root,i); // printf("deep:%d\n",deep); i = (deep - 1); } } printf("%d\n",cnt); del(root); } return 0;}//0MS332K
这题数据应该很弱,字典树竟然只花了332K。。。
其实此题还有更简单解法,可以用STL的set水过~
详情请见代码:
#include <iostream>#include<cstdio>#include<map>#include<string>#include<cstring>#include<cstdlib>#include<set>using namespace std;set<string> lcm;string s;char str[100005];char tmp[100005];int main(){ int i,j; while(gets(str),strcmp(str,"#") != 0) { lcm.clear(); i = 0; while(str[i] == ' ') i ++; j = 0; //printf("len:%d\ni:%d\n",strlen(str),i); for(;i < strlen(str);i ++) { if(str[i] == ' ') { tmp[j] = '\0'; s = tmp; lcm.insert(s); j = 0; while(str[i] == ' ') i ++; i --; } else tmp[j ++] = str[i]; } if(j) { s = tmp; lcm.insert(s); } printf("%d\n",lcm.size()); } return 0;}//0MS364K
- 杭电2072 单词数(Trie tree或者STL)
- 【杭电oj】2072 - 单词数(STL - string & map)
- 杭电 单词数 2072
- 杭电2072单词数
- 杭电2072 单词数
- 杭电2072 单词数
- 杭电2072 单词数
- 杭电2072-单词数
- 杭电 2072 单词数
- 杭电acm 2072(单词数)
- 杭电Hd 2072 单词数
- 杭电 HDU 2072 单词数
- 杭电ACM 2072:单词数
- 杭电OJ-2072_单词数
- 杭电oj2072单词数
- hdu 2072 单词数(Trie树)
- HDU 2072 - 单词数 (Trie)
- HDU 2072 单词数【STL】
- C++常用数学公式.txt
- 全面认识Cookie
- Opencv编程心得
- 将真分数分解为埃及分数
- 行为级和RTL级的区别
- 杭电2072 单词数(Trie tree或者STL)
- SHELL相关的特殊字符总结
- STM32 串口中断 BUG
- DBN(PoE and Contrastive Divergence)
- Poj 1035 Spell checker
- 一小时给未来
- C++中智能指针的工作原理和简单实现
- 0514-排序
- Android ListView常用用法