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最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...
随机推荐
- 多线程中Local Store Slot(本地存储槽)[转]
1. 使用ThreadStatic特性 ThreadStatic特性是最简单的TLS使用,且只支持静态字段,只需要在字段上标记这个特性就可以了: [ThreadStatic] static str ...
- php ul li 分类
<?phpfunction do_tree($arr,$pid){ echo "<ul>"; foreach ($arr as $key => $value ...
- 【图论-最短路】【P3393】逃离僵尸岛
传送门 Description 小a住的国家被僵尸侵略了!小a打算逃离到该国唯一的国际空港逃出这个国家. 该国有N个城市,城市之间有道路相连.一共有M条双向道路.保证没有自环和重边. K个城市已经被僵 ...
- 爬虫实例——爬取煎蛋网OOXX频道(反反爬虫——伪装成浏览器)
煎蛋网在反爬虫方面做了不少工作,无法通过正常的方式爬取,比如用下面这段代码爬取无法得到我们想要的源代码. import requests url = 'http://jandan.net/ooxx' ...
- acm1878欧拉回路
欧拉回路解释 对于本题我们只要把每个点的度进行记录,判断是否存在奇数度的点,如果是就可以判断不是欧拉回路,如果不是就在一个点出发,进行dfs搜索, 看能否走到起点,因为对于欧拉回路是一个闭合的回路,无 ...
- HDU 4313树形DP
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- linux内核的配置
以2.6.35.7版本的内核为例 总结:.config决定了Make时的条件编译与连接..config文件由两次配置第一次make XX_defconfig 第二次menuconfig. 1.分析源码 ...
- iOS Button设置
UIButton *kefuBtn = [[UIButton alloc]initWithFrame:CGRectMake(, , , )]; kefuBtn.backgroundColor = SX ...
- Spring Boot 中使用 MyBatis 整合 Druid 多数据源
2017 年 10 月 20 日 Spring Boot 中使用 MyBatis 整合 Druid 多数据源 本文将讲述 spring boot + mybatis + druid 多数据源配置方 ...
- HTML -- get与post提交方式的区别 -- (转)
在写代码过程中,get与post是两种不同的提交方式.下面,列举出两种方式的不同. 方法/步骤 get是从服务器上获取数据,post是向服务器传送数据. get是把参数数据队列加到提交表单的A ...