Trie树的简单实现
import java.util.ArrayList;
import java.util.TreeMap; import util.FileOperation; public class Trie {
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;
private int size;
public Trie() {
root=new Node();
size=0;
} public int getSize() {
return size;
}
public void add(String word) { Node cur=root;
for(char c:word.toCharArray()) {
if(cur.next.get(c)==null)
cur.next.put(c, new Node());
cur=cur.next.get(c);
}
if(!cur.isWord) {
cur.isWord=true;
size++;
}
} public boolean find(String word) {
Node cur=root;
for(char c:word.toCharArray()) {
if(cur.next.get(c)==null)
return false;
cur=cur.next.get(c);
}
return cur.isWord;
} public boolean isPrefix(String word) {
Node cur=root;
for(char c:word.toCharArray()) {
if(cur.next.get(c)==null)
return false;
cur=cur.next.get(c);
}
return true;
} public boolean searchRegex(String word) {
return match(root,word,0);
}
private boolean match(Node node,String word,int index) {
if(index==word.length()) return node.isWord;
else {
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 {
for(char w:node.next.keySet()) {
if(match(node.next.get(w), word, index+1)) {
return true;
}
}
return false;
}
}
}
public static void main(String[] args) { System.out.println("Pride and Prejudice"); ArrayList<String> words = new ArrayList<>();
if(FileOperation.readFile("pride-and-prejudice.txt", words)){ long startTime = System.nanoTime();
long endTime = System.nanoTime();
// ---
startTime = System.nanoTime();
Trie trie = new Trie();
for(String word: words)
trie.add(word);
for(String word: words)
trie.find(word);
endTime = System.nanoTime(); double time = (endTime - startTime) / 1000000000.0; System.out.println("Total different words: " + trie.getSize());
System.out.println("Trie: " + time + " s");
} }
}
Trie树的简单实现的更多相关文章
- POJ 3630 Phone List(trie树的简单应用)
题目链接:http://poj.org/problem?id=3630 题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES 思路:简单的trie树应用,插 ...
- hdu 1251 统计难题(trie 树的简单应用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给你多个字符串,求以某个字符串为前缀的字符串数量. 思路:简单的trie数应用,在trie ...
- 数据结构《16》----自动补齐实现《一》----Trie 树
1. 简述 Trie 树是一种高效的字符串查找的数据结构.可用于搜索引擎中词频统计,自动补齐等. 在一个Trie 树中插入.查找某个单词的时间复杂度是 O(len), len是单词的长度. 如果采用平 ...
- trie树(前缀树)
问题描述: Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...
- Trie树(字典树) 最热门的前N个搜索关键词
方法介绍 1.1.什么是Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...
- POJ 2001 Shortest Prefixes 【Trie树】
<题目链接> 题目大意: 找出能唯一标示一个字符串的最短前缀,如果找不出,就输出该字符串. 解题分析: Trie树的简单应用,对于每个单词的插入,都在相应字符对应的节点 num 值+1 , ...
- 从Trie树(字典树)谈到后缀树
转:http://blog.csdn.net/v_july_v/article/details/6897097 引言 常关注本blog的读者朋友想必看过此篇文章:从B树.B+树.B*树谈到R 树,这次 ...
- Trie树 - 字典树
1.1.什么是Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是最大限 ...
- hdu 1251 trie树
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Problem De ...
随机推荐
- ubuntu16.04问题 · 最初的梦想
ubuntu 包管理器命令 1 $ sudo synaptic 安装主题 1 $ sudo apt-get install unity-tweak-tool 下载主题 https://www.sysg ...
- Flink命令行提交job (源码分析)
这篇文章主要介绍从命令行到任务在Driver端运行的过程 通过flink run 命令提交jar包运行程序 以yarn 模式提交任务命令类似于: flink run -m yarn-cluster X ...
- webpack插件
插件 plugins:[ new ExtractTextPlugin.extrct({ }) //创建html new HtmlWebpackPlugin({ title:"first pa ...
- 正式学习MVC 03
1.View -> Controller的数据通信 1) 通过url查询字符串 public ActionResult Index(string user) { return Content(u ...
- Head First设计模式——生成器和责任链模式
生成器 生成器模式:封装一个产品的构造过程,并允许按步骤构造. 现又一个度假计划指定,需要指定度假每一天的活动.旅馆.用餐.门票等等事情,但是每个客人的度假计划可能不太一样.例如天数.活动类型.用餐等 ...
- WEB渗透 - 万能密码
asp万能密码 'or'='or' aspx万能密码 1: "or "a"="a 2: ')or('a'='a 3:or 1=1-- 4:'or 1=1-- 5 ...
- 关于PHP命名空间的讨论
什么是命名空间? 根据php.net官方翻译文档描述,命名空间是这样定义的: 什么是命名空间?从广义上来说,命名空间是一种封装事物的方法. 在PHP中,命名空间用来解决在编写类库或应用程序时创建可重用 ...
- ES6中的Promise使用总结
One.什么是Promise? Promise是异步编程的解决方案,而它本身也就是一个构造函数,比传统的异步解决[回调函数]和[事件]更合理,更强大. Two.Promise有何作用? 作用:解决回调 ...
- linux ftp服务器设置,只允许用户访问指定的文件夹,禁止访问其他文件夹
在Linux中添加ftp用户,并设置相应的权限,操作步骤如下: 1.环境:ftp为vsftp.被限制用户名为test.被限制路径为/home/test 2.建用户:在root用户下: useradd ...
- c strncpy 容易出错的地方
使用strncpy的是注意两点,目的是数组和目的是指针 .目的是数组: ] = "abcde"; // ] = "; strncpy(dest,src,N); dest[ ...