#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
using namespace std;
/*
问题:
Design a data structure that supports the following two operations:
void addWord(word)
bool search(word)
search(word) can search a literal word or a regular expression string containing only letters a-z
or .. A . means it can represent any one letter.
For example:
addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true
Note:
You may assume that all words are consist of lowercase letters a-z.
分析:分析"."或"..",代表任意字符,需要先实现前缀树,则对于"."的处理需要发给所有孩子节点去查询
则存在递归调用多个,只要有一个符合就返回true
前缀树可以不用递归,后缀树需要
输入:
7(指令个数)
addWord bad
addWord dad
addWord mad
search pad
search bad
search .ad
search b..
输出:
false
true
true
true
关键:
1 通配符:添加+搜索 是前缀树的结构,"."代表任意字符,需要先实现前缀树,则对于"."的处理需要发给所有孩子节点去查询
则存在递归调用多个,只要有一个符合就返回true
2
//遇到"."需要递归查找后续所有非空节点
if('.' == word.at(pos))
{
for(int i = 0 ; i < CHILD_SIZE ; i++)
{
childNode = _childs[i];
//如何判断查找结束,当前待查找字符对应节点存在,且完成标记为真
if(pos == len - 1 && (childNode) && childNode->_isFinish)
{
return true;
}
if(childNode)
{
bool result = childNode->search(word, pos+1);
if(result)
{
return true;
}
}
}
return false;
}
*/
const int CHILD_SIZE = 26;
class TrieNode
{
public:
TrieNode()
{
_isFinish = false;
memset(_childs , NULL , sizeof(_childs));
}
bool search(string& word ,int pos) {
if(word.empty() || pos < 0 || pos >= word.length())
{
return false;
}
int len = word.length();
TrieNode* childNode = NULL;
//遇到"."需要递归查找后续所有非空节点
if('.' == word.at(pos))
{
for(int i = 0 ; i < CHILD_SIZE ; i++)
{
childNode = _childs[i];
//如何判断查找结束,当前待查找字符对应节点存在,且完成标记为真
if(pos == len - 1 && (childNode) && childNode->_isFinish)
{
return true;
}
if(childNode)
{
bool result = childNode->search(word, pos+1);
if(result)
{
return true;
}
}
}
return false;
}
else
{
int index = word.at(pos) - 'a';
if(index < 0 || index > 25)
{
return false;
}
childNode = _childs[index];
//如何判断查找结束,当前待查找字符对应节点存在,且完成标记为真
if(pos == len - 1 && childNode && childNode->_isFinish)
{
return true;
}
if(childNode)
{
return childNode->search(word , pos+1);
}
else
{
return false;
}
}
}
public:
bool _isFinish;
TrieNode* _childs[CHILD_SIZE];
};
class WordDictionary {
public:
/** Initialize your data structure here. */
WordDictionary() {
_root = new TrieNode();
}
/** Adds a word into the data structure. */
void addWord(string word) {
if(word.empty())
{
return ;
}
int len = word.length();
TrieNode* curNode = _root;
for(int i = 0 ; i < len ; i++)
{
int index = word.at(i) - 'a';
if(index < 0 || index > 25)
{
return;
}
TrieNode* childNode = curNode->_childs[index];
if(!childNode)
{
childNode = new TrieNode();
curNode->_childs[index] = childNode;//注意赋值
}
curNode = childNode;
}
curNode->_isFinish = true;
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
//由于这里查找中可能包含通配符,因此,这必须是一个递归查找
bool search(string word) {
if(word.empty())
{
return false;
}
bool result = _root->search(word , 0);
return result;
}
~WordDictionary()
{
deleteNode(_root);
}
void deleteNode(TrieNode* root)
{
if(!root)
{
return;
}
for(int i = 0 ; i < CHILD_SIZE ; i++)
{
TrieNode* childNode = root->_childs[i];
if(childNode)
{
deleteNode(childNode);
}
}
if(root)
{
delete root;
root = NULL;
}
}
private:
TrieNode* _root;
};
void print(vector<int>& result)
{
if(result.empty())
{
cout << "no result" << endl;
return;
}
int size = result.size();
for(int i = 0 ; i < size ; i++)
{
cout << result.at(i) << " " ;
}
cout << endl;
}
void process()
{
vector<int> nums;
string value;
int commandNum;
vector<int> result;
string command;
while(cin >> commandNum)
{
WordDictionary obj;
for(int i = 0 ; i < commandNum ; i++)
{
cin >> command >> value;
if("addWord" == command)
{
obj.addWord(value);
}
else if("search" == command)
{
bool result = obj.search(value);
if(result)
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
}
}
cout << endl;
}
}
int main(int argc , char* argv[])
{
process();
getchar();
return 0;
}
leecode 解题总结:211. Add and Search Word - Data structure design
最新推荐文章于 2022-11-01 19:14:02 发布