uva10815--没过

//#define LOCAL #include <algorithm> #include <stdio.h> #include <string.h> #include <ctype.h> #define MAXN 50000 + 10 #define MAX 50000 + 10 char word[MAX]; const int sonsum = 26; char base = 'a'; int count; struct Trie { int num;//to remeber how many word can reach here, that is to say,perfix bool terminal;// If terminal == true, the current point has no following point struct Trie * son[sonsum];// the following point }; Trie *NewTrie(); void FindAll(Trie *, char *, int); void Insert(Trie *pnt, char *s, int len); void Delete(Trie *pnt); Trie* Find(Trie *pnt, char *s, int len); char* my_strlwr(char *str); int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #endif int i, j; // 数据的初始化 Trie* root = new Trie; root->num = 0; root->terminal = false; for(i = 0; i < sonsum; i++) root->son[i] = NULL; while(scanf("\n%s", word) != EOF) { // 将单词插入字典树 Insert(root, word, strlen(word)); } // 深搜遍历字典树,输出结果 memset(word, 0, sizeof(word)); FindAll(root, word, 0); return 0; } Trie *NewTrie()// Create a new node { Trie *temp = new Trie; temp->num = 1; temp->terminal = false; for(int i = 0; i < sonsum; i++) temp->son[i] = NULL; return temp; } void Insert(Trie *pnt, char *s, int len)// insert a new word to Trie tree { Trie *temp = pnt; s = my_strlwr(s); for(int i = 0; i < len; i++) { if(isalpha(s[i])) { if(temp->son[s[i] - base] == NULL) temp->son[s[i] - base] = NewTrie(); else temp->son[s[i] - base]->num++; temp = temp->son[s[i] - base]; } } temp->terminal = true; } void Delete(Trie *pnt)// delete the whole tree { if(pnt != NULL) { for(int i = 0; i < sonsum; i++) if(pnt->son[i] != NULL) Delete(pnt->son[i]); delete pnt; pnt = NULL; } } Trie* Find(Trie *pnt, char *s, int len)// trie to find the current word { Trie *temp = pnt; for(int i = 0; i < len; i++) { if(temp->son[s[i] - base] != NULL) temp = temp->son[s[i] - base]; else return NULL; return temp; } } // 树的遍历 void FindAll(Trie *trie, char* word, int len) { int i; if(trie->terminal && len != 0) { word[len] = '\0'; printf("%s\n", word); } for(i = 0; i < sonsum; i++) { if(trie->son[i] != NULL) { word[len++] = i + 'a'; FindAll(trie->son[i], word, len); len--; } } } char * my_strlwr(char *str) { char *p = str; while (*p != '/0') { if(*p >= 'A' && *p <= 'Z') *p = (*p) + 0x20; p++; if(*p == '\0') return str; } return str; }
这个题目比较简单,但是就是想用字典树做,结果。。两天了,悲剧啊。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值