您的位置:首页 > 其它

leetCode_Add and Search Word - Data structure design

2016-08-05 09:03 351 查看
题意:设计一个数据结构,支持addWord和searchWord两种方法,其中searchWord支持通配符(‘.’)

解法:我自己想出来有三种解法,前两种超时(所以就不贴代码了),后一种虽然时间比较长,但是过了,我再搜索有没有更好的方法,然后补充上去。

       若是没有通配符,那么非常简单,建立一个trie树就行了。难就难在有通配符的。

       解法一:最直接的想法,有几个通配符就递归几层。我一开始看题目中的tag里有递归,我还以为就这样递归就行了。哎,我实在是太天真了。果然超时。(这儿写错了,因为我的代码写错了,一开始的代码根本就没搜trie树,必然会超时)。改进过得代码如下,是正确的。要注意有几处当函数的值为true才返回,false不返回上一层的写法如下:

class TrieNode {
public:
int tag;
TrieNode *next[26];
// Initialize your data structure here.
TrieNode() {
int i;
for(i=0;i<26;i++) next[i]=NULL;
tag=0;
}
};

class WordDictionary {
public:
WordDictionary()
{
root=new TrieNode();
}

// Adds a word into the data structure.
void addWord(string word) {
int i;
TrieNode *temp=root;
for(i=0;i<word.length();i++)
{
if(temp->next[word[i]-'a']==NULL) temp->next[word[i]-'a']=new TrieNode();
temp=temp->next[word[i]-'a'];
}
temp->tag=1;
}

// 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) {
return searchAWord(word,0,root);
}

bool searchAWord(string word,int cindex,TrieNode * temp)
{
if(temp==NULL) return false;
if(cindex==word.length()) return temp->tag==1;
if(word[cindex]=='.')
{
for(int i=0;i<26;i++)
{
if(temp->next[i]!=NULL&&searchAWord(word,cindex+1,temp->next[i])) return true;
}
}
else
{
if(temp->next[word[cindex]-'a']!=NULL&&searchAWord(word,cindex+1,temp->next[word[cindex]-'a']))
return true;
}
return false;
}
private:
TrieNode * root;
};

       解法二:略改进的想法。我们观察字典里的所有词,看看每一个位置上都有哪些字母出现。这样遍历每一个通配符的时候就不用26个字母都遍历了,感觉是一种取巧的方法。但是显然,当每个单词的每一位字母差别都比较大的时候,这种方法的时间复杂度和第一种是一样的。也超时。(本质上和第一种一样)

       解法三:这种方法过了。我们维护两个队列:一个队列是trie树,一个队列(index)是目前比较到第几个字母了。首先先把整颗trie树放进去,同时另一个队列放入0。然后取出trie数,看看与index是否能匹配,如果能匹配,就把下一层index那个节点为树根的trie树全放进去。如果遇到通配符,则把下一层的所有节点都拆开成trie树,放进去。当index和word .length()相等时:如word[length-1]为通配符,只要这个根节点的结束标志位1就可以;如果不是,则还要保证这个单词对应的字母和最后一个单词的字母相等。(这好像是我写过的最长的话了,呵呵)。上代码:

class TrieNode
{
public:
int tag;
TrieNode *next[26];
int letter;
TrieNode(int c)
{
int i;
for(i=0; i<26; i++) next[i]=NULL;
tag=0;
this->letter=c;
}
};

class WordDictionary
{
public:
WordDictionary()
{
root=new TrieNode(-1);
}

// Adds a word into the data structure.
void addWord(string word)
{
int i;
TrieNode *temp=root;

for(i=0; i<word.length(); i++)
{
if(temp->next[word[i]-'a']==NULL)
{
TrieNode *ww=new TrieNode(word[i]-'a');
temp->next[word[i]-'a']=ww;
}
temp=temp->next[word[i]-'a'];
if(i==word.length()-1) temp->tag=1;
}
}

// 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)
{
queue<TrieNode *> tries;
queue<int> indexes;
int i,index,j;
TrieNode * temp;
if(root!=NULL)
{
tries.push(root);
indexes.push(0);
}
while(!tries.empty())
{
int i;
temp=tries.front();
index=indexes.front();
tries.pop();
indexes.pop();
if(index==word.length())
{
if(word[index-1]=='.'&&temp->tag==1) return true;
else if(word[index-1]!='.'&&word[index-1]-'a'==temp->letter&&temp->tag==1)
return true;
}
else if(index<word.length())
{
if(word[index]!='.')
{
if(temp->next[word[index]-'a']!=NULL)
{
tries.push(temp->next[word[index]-'a']);
indexes.push(index+1);
}
}
else
{
for(i=0; i<26; i++)
{
if(temp->next[i]!=NULL)
{
tries.push(temp->next[i]);
indexes.push(index+1);
}
}
}
}

}
return false;
}

private:
TrieNode * root;
};
我不知道有没有更好的方法,还想再搜索搜索看看。如果有的话,会及时补充的。加油~!再过一周就要科目三了,希望能顺利,虽然掉头还不会。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  trie树 DFS