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. CSS的历史与工作原理

    1. 浏览器的发展与CSS 网页浏览器主要通过HTTP协议连接网页服务器而取得网页,HTTP容许网页浏览器送交资料到网页服务器并且获取网页.目前最常用的 HTTP 是 HTTP/1.1,这个协议在RF ...

  2. JS传递中文参数出现乱码的解决办法

    一.window.open() 乱码: JS中使用window.open("url?param="+paramvalue)传递参数出现乱码,提交的时候,客户端浏览器URL中显示参数 ...

  3. saltstack:multi-master configuration

    官方手册地址:http://docs.saltstack.com/topics/tutorials/multimaster.html 总结起来,有以下几步: Create a redundant ma ...

  4. arp协议及简单应用

    1:什么是arp协议 参考文章:http://blog.csdn.net/tigerjibo/article/details/7351992 全称是:Address Resolution Protoc ...

  5. JN5169 --------- zigbee代码

    队列: 需要创建3个标准队列(只创建不使用): ------Queue with handle zps_msgMlmeDcfmInd to receive IEEE 802.15.4 MACcomma ...

  6. [技巧篇]19.InputStream与String,Byte之间互转[转载]

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

  7. String作为输出型参数时获取不到值

    有时候在一个方法中,我们需要返回多个字符串,而又不想将这些字段包成一个类.此时就需要使用输出型参数. 但是如果将输出型参数的类型声明为String,那么调用该方法后,是获取不到我们想要的值的. 测试代 ...

  8. @JsonField 修改json字段属性名称

    在前后端分离的开发方式中,经常会遇到后端字段名称和前端字段名称定义不一致的问题,比如,后端定义的Bean中的字段名称为createAt,而前端用的字段名称为createTime.这种情况下可以通过在前 ...

  9. 【uva12232/hdu3461】带权并查集维护异或值

    题意: 对于n个数a[0]~a[n-1],但你不知道它们的值,通过逐步提供给你的信息,你的任务是根据这些信息回答问题: I P V :告诉你a[P] = V I P Q V:告诉你a[P] XOR a ...

  10. .NET中方法的注意事项 明细

    1. 方法中return 会终止整个方法段. 而break只能终止当前循环. 2. 方法就是一对可用代码的复用. a . 对于可重用的代码,在vs中选中,右键  重构  提取方法.即可自动封装成一个方 ...