Lintcode---单词的添加与查找

设计一个包含下面两个操作的数据结构:addWord(word)search(word)

addWord(word)会在数据结构中添加一个单词。而search(word)则支持普通的单词查询或是只包含.a-z的简易正则表达式的查询。

一个 . 可以代表一个任何的字母。

注意事项

你可以假设所有的单词都只包含小写字母 a-z。

您在真实的面试中是否遇到过这个题?

Yes

样例

addWord("bad")
addWord("dad")
addWord("mad")
search("pad")  // return false
search("bad")  // return true
search(".ad")  // return true
search("b..")  // return true

思路:先定义字典树节点类,用以实现字典树;                   添加单词过程和之前的过程一模一样,在查找的时候,过程也类似,但要对‘.’字符进行特殊处理,这是问题的关键;                  因为字符为‘.‘的时候,一个 . 可以代表一个任何的字母,在这种情况下,使用递归比较好实现。

/*
思路:先定义字典树节点类,用以实现字典树;

        添加单词过程和之前的过程一模一样,在查找的时候,过程也类似,但要对‘.’字符进行特殊处理,这是问题的关键;

        因为字符为‘.‘的时候,一个 . 可以代表一个任何的字母,在这种情况下,使用递归比较好实现。

*/

//节点类的定义,注意构造函数对所有数据成员都进行初始化;

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

class WordDictionary {
public:

    WordDictionary() {
        root = new TrieNode();
    }

    // Adds a word into the data structure.
    //添加单词过程和插入过程一模一样
    void addWord(string word) {
        // Write your code here

        if(root==NULL||word.size()==0){
            return;
        }

        int len=word.size();
        TrieNode* t=root;
        int i=0;

        while(i<len){
            if(t->child[word[i]-‘a‘]==NULL){
                TrieNode* temp=new TrieNode();
                t->child[word[i]-‘a‘]=temp;
                t=t->child[word[i]-‘a‘];
            }
            else{
                t=t->child[word[i]-‘a‘];
            }
            i++;
        }
        t->count=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) {
        // Write your code here
        search(word, root, 0);
    }

    bool search(string &word, TrieNode *p, int i){
        if (i == word.size()){
            return p->count;
        }

        //当遇到字符为‘.‘的时候,一个 . 可以代表一个任何的字母;
        //这里用递归的方式判断输入字符串是否存在;

        if (word[i] == ‘.‘) {
            for (auto a : p->child) {
                if (a && search(word, a, i + 1)){
                    return true;
                }
            }
            return false;
        }
        else {
            return p->child[word[i] - ‘a‘] && search(word, p->child[word[i] - ‘a‘], i + 1);
        }
    }

private:
    TrieNode *root;
};

// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
 
时间: 2025-01-05 05:26:11

Lintcode---单词的添加与查找的相关文章

算法-单词的添加和查找

今天遇到了一道字典树的题,这是我第一次使用字典树来解决问题,所以我觉得还是有必要记录下来. 题意: 设计一个包含下面两个操作的数据结构:addWord(word), search(word) addWord(word)会在数据结构中添加一个单词.而search(word)则支持普 通的单词查询或是只包含.和a-z的简易正则表达式的查询. 一个 . 可以代表一个任何的字母. 样例: addWord("bad") addWord("dad") addWord("

关于eclipse添加自动查找文件以及svn的插件

1.添加自动查找当前文件位置的插件(如下图) 在百度搜索下载 OpenExplorer_1.5.0.v201108051313.jar,下载之后放入eclipse下面的plugin文件夹下面既可以 2.svn插件 同样的在网上下载好svn插件文件,进行解压,  然后就eclipse文件夹下dropins里面关于svn的插件全部删除  然后将下载后的插件包解压放入文件,重启eclipse即可, 当然可能会遇到错误,由于文件夹多一级的原因,此处参考 如下 http://blog.csdn.net/t

二叉树,添加,查找

public class Tree { TreeNode last = null; TreeNode root = null; public Tree(int value){ root = createNode(value); } //结构 static class TreeNode{ int data; TreeNode left; TreeNode right; } //查找结点 public boolean searchTreeNode(int key,TreeNode tree){ if

python实现将字符串中以大写字母开头的单词前面添加“_”下划线

在工作中写测试用例代码生成的时候,函数命令考虑采用参数文件的名称来命名,但是发现文件命名是驼峰的写写法,所以想按照字符串中的大写字母做分割,每个单词前面添加下划线,主要考虑采用正则的模式来匹配,替换然后咋对字符串拼接下. case_name = "testAdvanceRepayRequest" re.sub("[A-Z]", lambda x: "_" + x.group(0).lower(), case_name) 原文地址:https://

[LintCode] Add and Search Word 添加和查找单词

Design a data structure that supports the following two operations: addWord(word) and 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.Notic

[LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

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

[LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计

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

二叉树的添加与查找

添加: 添加的时候可以用数组,循环添加,我是一步一步测试方便,才一个一个添加. 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <script> 7 var root=null; 8 function bt_add(node,n){ 9

lintcode:单词切分

单词切分 给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词. 样例 s = "lintcode" dict = ["lint","code"] 返回 true 因为"lintcode"可以被空格切分成"lint code" 解题 DFS import java.util.Iterator; import java.util.Scanner; import java.u