Trie树解决字典中查找单词问题
2017-08-04 12:50
344 查看
package com.kai.util; import java.util.HashSet; /** * Created by Administrator on 2017/8/4. */ public class TrieTree { Node root=new Node(); private class Node{ private Node[] child=new Node[26]; private int count; private HashSet<String> set=new HashSet<String>(); } public void addTrieNode(String s){ Node current=root; for(int index=0;index<s.length();index++){ char c=s.charAt(index); if(current.child[c-'a']==null){ Node node =new Node(); current.child[c-'a']=node; } current=current.child[c-'a']; if(index==s.length()-1){ current.count ++; } // current.set.add(s); } } public int findTrie(String s){ Node current=root; for(int index=0;index<s.length();index++){ char c=s.charAt(index); if(current.child[c-'a']==null){ return 0; } current=current.child[c-'a']; } return current.count; } public static void main(String[] args) { TrieTree tree =new TrieTree(); tree.addTrieNode("hello"); tree.addTrieNode("hello"); tree.addTrieNode("hell"); tree.addTrieNode("world"); tree.addTrieNode("hiiih"); System.out.println(tree.findTrie("hello")); } }
相关文章推荐
- Trie树,又称单词查找树、字典
- Trie树的建立—查找字典中以特定字符串开头的单词数量(java实现)
- 困扰我多日的英文网站单词无法换行问题终于解决了。
- B树、B-树、B+树、B*树、红黑树rbtree 二叉排序树、trie树Double Array 字典查找树
- 解决Python获取字典dict中不存在的值时出错问题
- 根据原生JS封装函数,根据传递的选择器查找对应的元素以及解决getElementsByClassName() 兼容问题
- Trie树(单词查找树)
- 解决增强MyEclipse的代码提示功能实例化时自动补全不必要的单词问题
- 【转】Notepad++ 中文查找(中文搜索)问题解决方法
- 多域环境下people picker查找不到用户问题的解决(转载jianyi)
- DP32 单词按照字典分割问题 Word Break Problem @geeksforgeeks
- 查找字典中具有某个公共前缀的所有单词
- DWZ】常见问题解决 对话框 查找带回 分页问题 选择分页大小无效问题
- 2014最后一天,好烦!这个问题从来没遇到过!网上查找了很多办法都没解决!并且no wifi 了!
- 字典树_Trie树_单词查找树
- poj1035 串-字典修改查找问题
- 解决Python字典写入文件出行首行有空格的问题
- 在 win7 中使用 vs2003 时,“在文件中查找”导致无响应的问题解决
- 【转】Notepad++ 中文查找(中文搜索)问题解决方法
- B树、B-树、B+树、B*树、红黑树、 二叉排序树、trie树Double Array 字典查找树简介