查找字典中某个公共前缀的所有单词
来源:互联网 发布:社交网络电影剧情介绍 编辑:程序博客网 时间:2024/06/06 17:57
//Trie.h/* * trie树统计公共前缀的单词集合、个数 */#include <set>#include <string>using namespace std;#define BRANCH 26struct Node{Node * next[BRANCH];int prefix; // 此前缀的单词个数,为验证结果而加bool isStr;Node():prefix(0), isStr(false){memset(next, 0, sizeof(next));}};class Trie{private:Node * root;set<string> * preSet; /* private: * DFS() and freeNextArr() are private functions */* DFS find all the words from Node "ptr" * 实际就是寻找“以当前前缀本身为单词”的单词 */void DFS(Node * ptr, string preStr){for(int i = 0; i < BRANCH; ++i){if(ptr->next[i] != NULL){char chr[] = {'a' + i, '\0'};DFS(ptr->next[i],preStr+string(chr));}if(ptr->isStr) // 存在当前前缀的单词{this->preSet->insert(preStr);}}}// free the next arrayvoid freeNextArr(Node * ptr){for(int i = 0; i < BRANCH; ++i){if(ptr->next[i] != NULL){freeNextArr(ptr->next[i]);}}delete ptr;}public:Trie(){root = new Node();preSet = new set<string> ();}// insert a wordvoid insert(const char * word){if(NULL == word){return ;}Node * ptr = root;while(*word){if(ptr->next[*word-'a'] == NULL){ptr->next[*word-'a'] = new Node();}ptr->prefix ++;ptr = ptr->next[*word-'a'];++word;}ptr->isStr = true;}// find the location of the wordNode * search(const char * word){if(NULL == word){return NULL;}Node * ptr = root;while(ptr && *word){ptr = ptr->next[*word-'a'];++word;}return ptr;}// find the common prefix words setset<string> * findCommonPrefix_Set(const char * prefix){if(NULL == prefix){return NULL;}Node * ptr = search(prefix); // find location of prefixif(ptr == NULL) // prefix not exist{return NULL;}DFS(ptr, string(prefix)); // DFS find commonPre wordsreturn this->preSet;}// clear the preSetvoid clearPreSet(){this->preSet->clear();}// free the memoryvoid freeMemory(){this->freeNextArr(root);delete this->preSet;}};
测试程序:
#include <iostream>#include "Trie.h"void main(){Trie comPreTrie;comPreTrie.insert("abc");comPreTrie.insert("abcd");comPreTrie.insert("abcde");comPreTrie.insert("abcdef");comPreTrie.insert("abcdefg");comPreTrie.insert("cde");/* test the common prefix "abcd" * 注,测试时默认输入合法字符,即 a-z * 本文程序不对此做出检查 */Node * ptr = comPreTrie.search("abcd");if(ptr){printf("num of words prefix by \"abcd\" is %d\n",ptr->isStr ? ptr->prefix + 1 : ptr->prefix);}else{printf("ptr for \"abcd\" is null\n");}set<string> * comPreSet= comPreTrie.findCommonPrefix_Set("abcd");if(comPreSet){for(set<string>::iterator it = comPreSet->begin();it != comPreSet->end(); ++it){printf("%s\n", (*it).c_str());}}else{printf("comPreSet for \"abcd\" is null\n");}comPreTrie.clearPreSet();printf("---------------\n");/* test the common prefix "word" */ptr = comPreTrie.search("word");if(ptr){printf("num of words prefix by \"word\" is %d\n",ptr->isStr ? ptr->prefix + 1 : ptr->prefix);}else{printf("ptr for \"word\" is null\n");}comPreSet = comPreTrie.findCommonPrefix_Set("word");if(comPreSet){for(set<string>::iterator it = comPreSet->begin();it != comPreSet->end(); ++it){printf("%s\n", (*it).c_str());}}else{printf("comPreSet for \"word\" is null\n");}comPreTrie.clearPreSet();// free memorycomPreTrie.freeMemory();}
0 0
- 查找字典中某个公共前缀的所有单词
- 查找字典中具有某个公共前缀的所有单词
- 在一个目录及其所有子目录下所有的文件中查找某个单词
- 百度面试题--给定一个单词,从字典查找该单词的所有兄弟单词
- 算法#26--查找字符串数组中最长的公共前缀
- 查找字符串数组中最长的公共前缀字符串。
- 利用字典树查找唯一标记某单词的最小前缀
- 字典树(Trie树、单词查找树、前缀树)
- 字典树详解----串查找、排序、公共前缀之杀手锏
- 储存每一个单词W以及W的所有前缀,特定方的方向执行一次扫描的时候,如果被查找的单词作为前缀不在散列表中,那么在这个方向上可以及早终止
- 从书中查找某个单词出现的频率
- 查找字符串数组的最大公共前缀
- 查找字典中单词变位词 方法
- 在字典中查找兄弟单词
- Vi编辑器中查找某个单词或重复次数较多的单词
- 查找数据库中包含某个字段的所有表
- AC解 - 统计带某个前缀的单词数量
- 用户输入一个单词,要求在一个字典中找出该单词的所有兄弟单词,并输出
- LeetCode -- SpiralOrder
- 个人对runtime的理解
- HDU 1064.Financial Management【超级无敌大水题】【9月1】
- GIT刷新忽略文件.gitignore
- 训犬日志--狗狗定点排便
- 查找字典中某个公共前缀的所有单词
- java反射机制
- 最小二叉堆
- Android 网络编程——Socket,HTTP,SOAP辨析
- 自己对于MVC开发模式的理解-2
- Dancing Link讲解
- spark源码剖析之----Shuffle
- 远程重启服务器的有效办法?
- 实现pdf图片格式转换成txt文档的方法