hdu 1247 (字典树入门)
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12149 Accepted Submission(s): 4338
You are to find all the hat’s words in a dictionary.
input consists of a number of lowercase words, one per line, in
alphabetical order. There will be no more than 50,000 words.
Only one case.
ahat
hat
hatword
hziee
word
hatword
思路:先插入每个单词进入字典树,然后分别对每个单词进行查找,find找到前缀后再利用find2找到后缀,当后缀对应某个单词时,则证明此单词由两个字典中单词组成
注意输入到文件尾
package 字典树;
import java.util.Scanner;
class Trie{
private Node root;
public Trie() {
root = new Node();
}
public void insert(String s){
Node t =root;
for(int i=0;i<s.length();i++){
if(t.nodes[s.charAt(i)-'a']==null){
Node node = new Node();
t.nodes[s.charAt(i)-'a'] = node;
}
t = t.nodes[s.charAt(i)-'a'];
}
t.isword = true;
}
public boolean find(String str){
Node t = root;
for(int i=0;i<str.length();i++){
if(t.nodes[str.charAt(i)-'a']!=null){
if(t.isword){
String str1 = str.substring(i, str.length());
//System.out.println(str1);
if(find2(str1)){
return true;
}
}
t = t.nodes[str.charAt(i)-'a'];
}
else return false;
}
return false;
}
private boolean find2(String str) {
Node t = root;
for(int i=0;i<str.length();i++){
if(t.nodes[str.charAt(i)-'a']==null){
return false;
}
t = t.nodes[str.charAt(i)-'a'];
}
if(t.isword) return true;
return false;
}
public
class Node {
Node [] nodes;
boolean isword; //标记末尾
public Node() {
isword = false;
nodes = new Node[26];
}
}
}
public class hdu_1247 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
Trie t = new Trie();
String [] str = new String [50005];
int k=0;
while(sc.hasNext())
{
str[k] = new String();
str[k] = sc.next();
t.insert(str[k++]);
}
for(int i=0;i<k;i++){
if(t.find(str[i])){
System.out.println(str[i]);
}
}
}
}
hdu 1247 (字典树入门)的更多相关文章
- Hat’s Words HDU - 1247 字典树
题意:给出数个单词 输出单词 如果该单词 是由字典中的单词组成的 思路:字典树 先把全部建树 然后对于每一个单词进行分割,分别拿两半到字典树里面去找 小心RE! #include<bits/s ...
- HDU 5687 字典树入门
Problem C Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- HDU 1251 统计难题(字典树入门模板题 很重要)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDU 4825 Xor Sum(01字典树入门题)
http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...
- HDU 5687 字典树插入查找删除
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...
- HDU 5384 字典树、AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...
- hdu 2112(字典树+最短路)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2072(字典树模板,set,map均可做)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...
随机推荐
- 我们自己写的solr查询的代码作为search项目中的dao
我们自己写的solr查询的代码作为search项目中的dao,但是启动时会报错: 其实就是说 searchServiceImpl 中我们 Autowired 的 SearchDao 类 spring ...
- 第三方库升级Nginx
通过PPA方式,来升级Nginx 1. 添加PPA sudo add-apt-repository ppa:nginx/stable sudo apt-get updatesudo apt-get u ...
- jsoup select 选择器
转载自:http://blog.csdn.net/zhejingyuan/article/details/11801027 方法 利用方法:Element.select(String selector ...
- Java并发多线程 - 并发工具类JUC
安全共享对象策略 1.线程限制 : 一个被线程限制的对象,由线程独占,并且只能被占有它的线程修改 2.共享只读 : 一个共享只读的对象,在没有额外同步的情况下,可以被多个线程并发访问, 但是任何线程都 ...
- 51Nod 1347 旋转字符串 | 规律
Input示例 aa ab Output示例 YES NO 规律:abcabc 只需判断原始字符串 #include <bits/stdc++.h> using namespace std ...
- CAS单点登录原理
转自 https://www.cnblogs.com/lihuidu/p/6495247.html 1.基于Cookie的单点登录的回顾 基于Cookie的单点登录核心原理: 将用户名密 ...
- Linux 文件编码问题及iconv命令
iconv命令是运行于linux/unix平台的文件编码装换工具.当我们在linux/unix系统shell查看文本文件时,常常会发现文件的中文是乱码的,这是由于文本文件的编码与当前操作系统设置的编码 ...
- bzoj 2079: [Poi2010]Guilds——结论题
Description Zy皇帝面临一个严峻的问题,两个互相抵触的贸易团体,YYD工会和FSR工会,他们在同一时间请求在王国各个城市开办自己的办事处.这里有n个城市,其中有一些以双向马路相连,这两个工 ...
- Spring mvc详解(山东数漫江湖)
Spring mvc框架 Spring web MVC 框架提供了模型-视图-控制的体系结构和可以用来开发灵活.松散耦合的 web 应用程序的组件.MVC 模式导致了应用程序的不同方面(输入逻辑.业务 ...
- spring boot 注解说明
Starters 可以创建自己的Starter,但名字格式不能是 spring-boot-starter-*,而是 *-spring-boot-starter.类似Maven插件的规则. 自动配置 ...