zoukankan      html  css  js  c++  java
  • 字典树

    package com.xiaomi.mahout_test;

    public class Trie {
    private Node root;

    public Trie() {
    root = new Node();
    }

    public static void addWord(Node root, String sentence) {
    Node cur = root;
    for (char tmp : sentence.toCharArray()) {
    int index = tmp - 'a';
    if (cur.chlidrens[index] == null) {
    cur.chlidrens[index] = new Node();
    }
    cur = cur.chlidrens[index];
    cur.ch = tmp;
    }
    }
    public static boolean findWord(Node root, String sentence) {
    Node cur = root;
    for (char tmp : sentence.toCharArray()) {
    int index = tmp - 'a';
    if (cur.chlidrens[index] == null) {
    return false;
    }
    cur = cur.chlidrens[index];
    }
    return true;
    }
    public static void main(String[] args) {
    String[] str = {"asdf", "asji", "bjkl", "cdsdf", "jdsfk"};
    Trie trie = new Trie();
    for (String s : str) {
    addWord(trie.root, s);
    }
    if (findWord(trie.root, "jdsfk")) {
    System.out.println("string is found~");
    } else {
    System.out.println("not found~");
    }
    }
    static class Node {
    Node[] chlidrens = new Node[26];
    char ch;
    }
    }

  • 相关阅读:
    三大范式
    html 横线的代码
    CSS下拉 菜单3.27第一次
    JS页面三种打开方式及对话框
    函数整理
    3.22整理作业
    for循环,if 练习
    php测试题
    设计模式
    面向对象的三大特性
  • 原文地址:https://www.cnblogs.com/dlutxm/p/4721145.html
Copyright © 2011-2022 走看看