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;
    }
    }

  • 相关阅读:
    WCF和SOA的简介
    .NET 反射应用
    ASP.NET---如何使用web api创建web服务
    获得N位数字字母随机组合
    git的初步使用
    js贪吃蛇
    python多线程
    2013-12-13
    2012-12-12
    2013-12-5
  • 原文地址:https://www.cnblogs.com/dlutxm/p/4721145.html
Copyright © 2011-2022 走看看