查找字典中具有某个公共前缀的所有单词
2015-12-26 13:58
411 查看
## Trie.h ##
#include<set> #include<string> #define BRANCH 26 using namespace std; struct 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; 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); } } } //释放next数组 void 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>(); } //插入一个单词; void insert(const char * word) { if (word == NULL) { 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 word Node* search(const char * word) { if (word == NULL) { return NULL; } Node* ptr = root; while (ptr&&*word) { ptr = ptr->next[*word - 'a']; ++word; } return ptr; } //find the common prefix word set set<string>* findCommonPrefix_set(const char * prefix) { if (prefix == NULL) { return NULL; } Node* ptr = search(prefix); //find the loication of prefix if (ptr == NULL) { return NULL; } DFS(ptr, string(prefix)); return this->preSet; } //clear the preSet void clearPreSet() { this->preSet->clear(); } //free the memory void 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"); } 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"); } comPreTrie.clearPreSet(); printf("--------------------------------\n"); 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 memory comPreTrie.freeMemory(); }
## 运行结果 ##
相关文章推荐
- SAX解析XML
- spring事务
- Netlink通信机制
- LeetCode(97) Interleaving String
- 无法连接 postgres 服务器数据库
- LeetCode(97) Interleaving String
- SIFT教程:a tutorial on SIFT
- 自定义dialog并设置margin
- 查询表结构信息
- Leetcode: Find the Duplicate Number
- 构建根文件系统启动(1)
- Android 开发中怎么使用自定义字体?
- C++类内存分布
- layoutSubviews
- 关于阅读软件工程和计算机科学技术区别的文章来谈谈自己看法
- po
- .其它命令(help):
- equals,hashCode 方法 的重新理解
- ping www.baidu.com和ping 8.8.8.8出现ping不通
- Linux下Tomcat的安装配置