美文网首页
2019-03-31Tire字典树和简单匹配模式(. == 所有

2019-03-31Tire字典树和简单匹配模式(. == 所有

作者: Aluha_f289 | 来源:发表于2019-03-31 22:50 被阅读0次
Snipaste_2019-03-31_22-29-16.png
package trie;

import java.util.TreeMap;

public class WordDictionary {

    private class Node{

        public boolean isWord;
        public TreeMap<Character, Node> next;

        public Node(boolean isWord){
            this.isWord = isWord;
            next = new TreeMap<>();
        }

        public Node(){
            this(false);
        }
    }

    private Node root;

    /** Initialize your data structure here. */
    public WordDictionary() {
        root = new Node();
    }

    /** Adds a word into the data structure. */
    public void addWord(String word) {

        Node cur = root;
        for(int i = 0 ; i < word.length() ; i ++){
            char c = word.charAt(i);
            if(cur.next.get(c) == null)
                cur.next.put(c, new Node());
            cur = cur.next.get(c);
        }
        cur.isWord = true;
    }

    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    public boolean search(String word) {
        return match(root, word, 0);
    }

    private boolean match(Node node, String word, int index){

        //递归到底的情况
        if(index == word.length())
            return node.isWord;

        char c = word.charAt(index);

        //不包含'.'的情况
        if(c != '.'){
            if(node.next.get(c) == null)
                return false;
            return match(node.next.get(c), word, index + 1);
        }
        else{//包含'.'的情况下遍历所有key
            for(char nextChar: node.next.keySet())
                if(match(node.next.get(nextChar), word, index + 1))
                    return true;
            return false;
        }
    }


public static void main(String args[]){
        WordDictionary wordDictionary = new WordDictionary();

        wordDictionary.addWord("bad");
        wordDictionary.addWord("dad");
        wordDictionary.addWord("mad");
        boolean flag =  wordDictionary.search("pad");
//        boolean flag =  wordDictionary.search("b..");
        System.out.println(flag);
    }
}

相关文章

  • 2019-03-31Tire字典树和简单匹配模式(. == 所有

  • 字符串---AC自动机

    求目标串中出现了几个模式串 思路 一、构建字典树 二、构建fail指针 三、串匹配 例题 HDU2222

  • iOSweb和native交互方式

    H5和native方法交互经验: 方法一:采用字典匹配模式 //MARK: - WKScriptMessageHa...

  • python re库的使用(正则表达式笔记)

    末尾有福利(手动滑稽~) 1.正则表达式字典 (1)字典 模式描述^匹配字符串的开头$匹配字符串的末尾.匹配任意字...

  • 数据结构之字典树Trie

    字典树Trie 字典树也叫前缀树,是一种在字符串查找,前缀匹配等问题广泛应用的算法,为什么使用字典树呢?我们都知道...

  • CSS选择器

    CSS选择器 选择器基础 CSS选择器是由与文档树中所有元素匹配的模式组成。当模式中的所有条件均满足时,选择器匹配...

  • 字符串匹配算法总结

    字符串匹配算法总结 所有代码集合 在一个主串中匹配模式串 BF算法   最简单的使用strcmp逐个匹配的算法, ...

  • Series和DataFrame简单入门

    (1)、导入库 (2)、Series简单创建与使用 (3)、根据字典创建Series (4)、列表与字典进行匹配 ...

  • Scala中的模式匹配

    简单匹配 模式匹配常用于match语句: 变量使用 模式匹配case中可以使用变量来获取参数值 类型匹配 守卫匹配...

  • Trie 树

    Trie 树,也叫字典树,专门做字符串匹配的数据结构,也可快速的做字符串前缀匹配。 它是一种多叉树,即把前缀相同的...

网友评论

      本文标题:2019-03-31Tire字典树和简单匹配模式(. == 所有

      本文链接:https://www.haomeiwen.com/subject/fdepbqtx.html