hdu 2072 单词数 字典树
2015-05-08 23:41
435 查看
简单字典树,我看错了题目,郁闷了好一会,沙茶了一把。(次次沙茶QAQ)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2072
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2072
#include <stdio.h> #include <stdlib.h> #include <string.h> #define maxn 30 struct Trie{ Trie * next[maxn]; int v; }; Trie * root; int con; void Init_Trie(){ root = (Trie *)malloc(sizeof(Trie)); for(int i=0;i<maxn;i++){ root -> next[i] = NULL; } } void Add_Trie(char * str){ Trie * p = root , * q; for(int i=0;str[i]!='\0';i++){ int id = str[i] - 'a'; if(p->next[id]==NULL){ q = (Trie *)malloc(sizeof(Trie)); q -> v = 0; for(int j=0;j<maxn;j++){ q -> next[j] = NULL; } p -> next[id] = q; p = p -> next[id]; } else { p = p -> next[id]; } } if(!(p->v)){ con++; p -> v = 1; } } void Del_Trie(Trie * p){ for(int i=0;i<maxn;i++){ if(p->next[i])Del_Trie(p->next[i]); } free(p); } void Input(){ char a[10005]; while(gets(a),a[0]!='#'){ con = 0; Init_Trie(); char b[105]; int i = 0; while(a[i]==' ')i++; for(;i<strlen(a);){ sscanf(a+i,"%s",b); i+=strlen(b); while(a[i]==' ')i++; Add_Trie(b); } printf("%d\n",con); Del_Trie(root); } } void File(){ freopen("a.in","r",stdin); freopen("a.out","w",stdout); } int main(void){ // File(); Input(); return 0; }
相关文章推荐
- hdu 2072 单词数 字典树
- (HDU)2072 - 单词数【字典树】or【Set容器】
- 单词数 (HDU_2072) 字典树
- hdu2072-单词数 字典树、STL
- hdu2072单词数 字典树做法
- HDU 2072 单词数 字典树
- hdu 题目2072 单词数 (字典树)
- hdu 2072 单词数(字典树)
- hdu 2072-单词数(字典树)
- hdu 2072 单词数(字典树入门题)
- hdu-2072-单词数(字典树)
- HDU-1274展开字符串/HDU-2072单词数
- HDU 2072 单词数
- hdu 2072 单词数(STL set写法)
- Hdu 2072 - 单词数
- hdu2072 单词数
- HDU 2072 单词数
- HDU 2072 单词数 map的应用
- 单词数 HDU 2072
- HDU 2072 单词数 【map】