简单的trie实现
class Node{
private:string data;//注意這里的data存儲的是節點的對應的value,而不是keymap<string, Node> mp;
public:void insert(const string &str, string data) {Node *curr = this;stringstream ss;ss <<str;string word;while(ss >> word)curr = &(curr->mp[word]);curr->data = data;}string search(const string &str) {Node *curr = this;stringstream ss;ss <<str;string word;map<string,Node>::iterator it;while(ss >> word) {it = curr->mp.find(word);if(it==curr->mp.end())return "";curr = &(it->second);}return curr->data;}
};
總結
- 上一篇: 求无序数组的第二小的元素
- 下一篇: 查看网络端口使用情况