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

Problem Description
A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
You are to find all the hat’s words in a dictionary.
 
Input
Standard
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.
 
Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
 
Sample Input
a
ahat
hat
hatword
hziee
word
 
Sample Output
ahat
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 (字典树入门)的更多相关文章

  1. Hat’s Words HDU - 1247 字典树

    题意:给出数个单词 输出单词 如果该单词 是由字典中的单词组成的 思路:字典树 先把全部建树  然后对于每一个单词进行分割,分别拿两半到字典树里面去找 小心RE! #include<bits/s ...

  2. HDU 5687 字典树入门

    Problem C Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  3. HDU 1251 统计难题(字典树入门模板题 很重要)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  4. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  5. HDU 4825 Xor Sum(01字典树入门题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...

  6. HDU 5687 字典树插入查找删除

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...

  7. HDU 5384 字典树、AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...

  8. hdu 2112(字典树+最短路)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. hdu 2072(字典树模板,set,map均可做)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...

随机推荐

  1. PLAN OF HEOI(unfinished)

    Au:整体二分/计算几何/多项式/fwtAg:可持久化重量平衡树/线段树分治/线段树合并/最短路树/最短路DAGCu:三分Up:博弈论/置换群/杜教筛/矩阵树定理/BSGS/动态树分治/网络流(线性规 ...

  2. 剑桥offer(21~30)

    21.题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba.   还不 ...

  3. 学习tcpIp必备的抓包工具wireshark

    wireshark是一个优秀的抓包工具 ip.src=192.168.10.123  发送http的一端 ip.dst=192.168.10.126 接收http的一端 如下图所示:

  4. homebrew常见用法

    1. 安装 Homebrew是mac下安装软件的好帮手, 是使用 ruby 写的,采用 github 来存放信息库,很方便吧. Ruby 已经内置,最好装上 Xcode,因为可能需要一些编译包.然后在 ...

  5. Jmeter-12-如何使用Plugin Manager

    1. 搜索 Jmeter plugin 并找到plugin manager 下载jar文件 2. 放到jmeter/lib/ext下面, 重启jmeter 3. 找到选项-> Plugin ma ...

  6. Spring Boot 配置定时任务

    package com.zooper.demo; import java.text.SimpleDateFormat; import java.util.Date; import org.slf4j. ...

  7. Eclipse中 将java Gradle项目转换为web项目

    1.找到项目工作空间目录,打开.project文件,并修改文件, 修改如下:      找到:<natures> </natures>代码段,在代码段中加入如下内容并保存:   ...

  8. MYSQL查找总结

      a.条件判断where select * from 表 where id > 1 and name != 'alex' and num = 12; select * from 表 where ...

  9. luaj luaoc 回调函数传递的一些小总结

    问题场景:我们的游戏在支付时,由于第三方支付比较费时,可能在支付的过程中,我们lua写的cocos2dx项目会断网,我们的游戏有自动重连的机制.我就想,如果断线好了以后,支付完成了,那在断网之前传入的 ...

  10. Javascript prototype 及 继承机制的设计思想

    我一直很难理解Javascript语言的继承机制. 它没有"子类"和"父类"的概念,也没有"类"(class)和"实例" ...