首页 > 专题系列 > Java解POJ > POJ 2418 Hardwood Species [解题报告] Java
2013
11-11

POJ 2418 Hardwood Species [解题报告] Java

Hardwood Species

问题描述 :

Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter.

America’s temperate climates produce forests with hundreds of hardwood species — trees that share certain biological characteristics. Although oak, maple and cherry all are types of hardwood trees, for example, they are different species. Together, all the hardwood species represent 40 percent of the trees in the United States.

On the other hand, softwoods, or conifers, from the Latin word meaning “cone-bearing,” have needles. Widely available US softwoods include cedar, fir, hemlock, pine, redwood, spruce and cypress. In a home, the softwoods are used primarily as structural lumber such as 2x4s and 2x6s, with some limited decorative applications.

Using satellite imaging technology, the Department of Natural Resources has compiled an inventory of every tree standing on a particular day. You are to compute the total fraction of the tree population represented by each species.

输入:

Input to your program consists of a list of the species of every tree observed by the satellite; one tree per line. No species name exceeds 30 characters. There are no more than 10,000 species and no more than 1,000,000 trees.

输出:

Print the name of each species represented in the population, in alphabetical order, followed by the percentage of the population it represents, to 4 decimal places.

样例输入:

Red Alder
Ash
Aspen
Basswood
Ash
Beech
Yellow Birch
Ash
Cherry
Cottonwood
Ash
Cypress
Red Elm
Gum
Hackberry
White Oak
Hickory
Pecan
Hard Maple
White Oak
Soft Maple
Red Oak
Red Oak
White Oak
Poplan
Sassafras
Sycamore
Black Walnut
Willow

样例输出:

Ash 13.7931
Aspen 3.4483
Basswood 3.4483
Beech 3.4483
Black Walnut 3.4483
Cherry 3.4483
Cottonwood 3.4483
Cypress 3.4483
Gum 3.4483
Hackberry 3.4483
Hard Maple 3.4483
Hickory 3.4483
Pecan 3.4483
Poplan 3.4483
Red Alder 3.4483
Red Elm 3.4483
Red Oak 6.8966
Sassafras 3.4483
Soft Maple 3.4483
Sycamore 3.4483
White Oak 10.3448
Willow 3.4483
Yellow Birch 3.4483

温馨提示:

This problem has huge input, use scanf instead of cin to avoid time limit exceeded.

解题代码:

瑙f�涓��
import java.io.*; 
import java.text.DecimalFormat; 

class Trie{ 
    Trie next[] = new Trie[128];
    int cnt;
    public Trie(){
        cnt = 0;
    }
} 
 
public class Main{ 
    Trie root = new Trie();
    String res;
    int all = 0;
    DecimalFormat a = new DecimalFormat("0.0000");
    void solve() throws IOException{
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader cin = new BufferedReader(new FileReader(new File("in")));
 
        String input;
        while((input = cin.readLine()) != null){
            insert(input.toCharArray());
        }
        res = "";
        dfs(root);
    }
    void insert(char[] str){
        int len = str.length;
        int k = 0, t;
        Trie p = root;
        while(k!=len){
            t = str[k++];
            if(p.next[t] == null)p.next[t] = new Trie();
            p = p.next[t];
        }
        p.cnt++;
        all++;
    }
     
    void dfs(Trie p){
        if(p.cnt!=0)System.out.println(res + " " + a.format(p.cnt*100.0/all));
        for(int i=0;i< 128;i++){
            if(p.next[i] != null){
                res+=(char)i;
                dfs(p.next[i]);
                res = res.substring(0, res.length()-1);
            }
        }
    }
    public static void main(String[] args) throws IOException{
        Main test = new Main();
         test.solve();
    }
}

瑙f�浜��				
import java.io.BufferedReader;   
import java.io.IOException;   
import java.io.InputStreamReader;   
  
public class Main {   
       
    public static class TreeNode{   
        private String species;   
        private TreeNode left;   
        private TreeNode right;   
        private int count;   
           
    }   
       
    public static class Tree{   
        private int total;   
        private TreeNode root=new TreeNode();   
  
        public void insert(String newSpecies,TreeNode root){   
            if(root.count==0){   
                   
                root.species=newSpecies;   
                root.count++;   
                total++;   
                return;   
                   
            }   
            else{   
                if(root.species.compareTo(newSpecies)>0){   
                       
                    if(root.left==null){   
                           
                        root.left=new TreeNode();   
                           
                    }   
                    insert(newSpecies,root.left);   
                       
                }else{   
                       
                    if(root.species.compareTo(newSpecies)< 0){   
                           
                        if(root.right==null){   
                               
                            root.right=new TreeNode();   
                               
                        }   
                        insert(newSpecies,root.right);     
                           
                    }else{   
                           
                        root.count++;   
                        total++;   
                           
                    }   
                       
                }   
                   
            }   
               
        }   
           
        public void travelTree(TreeNode root){   
               
            if(root==null){   
                   
                return;   
                   
            }   
            travelTree(root.left);   
            System.out.print(root.species+" ");   
            double p=(double)root.count*100/total;   
            System.out.printf("%.4f", p);   
            System.out.println();   
            travelTree(root.right);   
               
        }   
           
    }   
       
    public static void main(String[] args){   
           
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));   
        Tree t=new Tree();   
        String s;   
           
        try {   
            while((s=reader.readLine())!=null){   
  
                t.insert(s,t.root);   
  
            }   
  
        } catch (IOException e) {   
            // TODO Auto-generated catch block   
            e.printStackTrace();   
        }   
        t.travelTree(t.root);   
           
    }   
  
}

  1. 博主您好,这是一个内容十分优秀的博客,而且界面也非常漂亮。但是为什么博客的响应速度这么慢,虽然博客的主机在国外,但是我开启VPN还是经常响应很久,再者打开某些页面经常会出现数据库连接出错的提示

  2. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。

  3. 站长,你好!
    你创办的的网站非常好,为我们学习算法练习编程提供了一个很好的平台,我想给你提个小建议,就是要能把每道题目的难度标出来就好了,这样我们学习起来会有一个循序渐进的过程!

  4. 第二块代码if(it != mp.end())应改为if(it != mp.end() && (i+1)!=(it->second +1));因为第二种解法如果数组有重复元素 就不正确

  5. for(int i=1; i<=m; i++){
    for(int j=1; j<=n; j++){
    dp = dp [j-1] + 1;
    if(s1.charAt(i-1) == s3.charAt(i+j-1))
    dp = dp[i-1] + 1;
    if(s2.charAt(j-1) == s3.charAt(i+j-1))
    dp = Math.max(dp [j - 1] + 1, dp );
    }
    }
    这里的代码似乎有点问题? dp(i)(j) = dp(i)(j-1) + 1;这个例子System.out.println(ils.isInterleave("aa","dbbca", "aadbbcb"));返回的应该是false