zoukankan      html  css  js  c++  java
  • 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


    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]);
                }
            }
        }
    }
  • 相关阅读:
    获取exe可执行程序文件中的图标
    在客户端显示服务器时钟
    解决系统管理员不允许使用保存的凭据登录远程计算机
    c#反射实现实体类生成以及数据获取与赋值
    c# 利用反射动态给实体类对象赋值
    将一个DataTable转换成一个List<T>的泛型集合
    解决table中无内容边框显示不出来的问题
    javascript 格式化日期显示
    一些常用的
    Web.config配置
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5303032.html
Copyright © 2011-2022 走看看