POJ 2136垂直直方图

时间:2013-10-27 19:05:17

标签: java

这是一个poj问题 - http://poj.org/problem?id=2136 我意识到这是一个简单的,但不知何故,在线评判不接受我的解决方案,虽然它似乎对我的样本输入工作。它说“错误的解决方案”。我确保没有额外的空格,也没有问题中提到的额外线条。有人可以指出我哪里出错了吗?

import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeMap;
public class poj2136 {

public static void main(String[] args) 
{
    // TODO Auto-generated method stub

    Scanner sc = new Scanner(System.in);
    int counter = 0;
    TreeMap<Character,Integer> hm_frequency = new TreeMap<Character,Integer>();
    while(counter<4)
    {
        String line = sc.nextLine();
        String words[] = line.split(" ");
        for(String word: words)
        {
            //System.out.println(word);
            for(int i=0; i<word.length();i++)
            {
                char key = word.charAt(i);
                if((int)key>64 && (int)key<91)
                {
                    if(hm_frequency.containsKey(key))
                    {
                        hm_frequency.put(key,hm_frequency.get(key)+1);
                    }
                    else
                    {
                        hm_frequency.put(key,1);
                    }
                }
            }

        }
        counter++;
    }

    while(true)
    {           
        int max_value = maxvalue(hm_frequency);
        char ch = maximumchar(hm_frequency, max_value);
        if(max_value == 0)
        {
            break;
        }
        Iterator iter = hm_frequency.keySet().iterator();
        int stop_flag = 0;
        while(iter.hasNext() && stop_flag==0)
        {
            char key = (Character) iter.next();
            if (key== ch)
            {
                stop_flag = 1;
            }
            if(hm_frequency.get(key)== max_value)
            {
                hm_frequency.put(key, max_value-1);
                if(stop_flag == 0)
                System.out.print("* ");
                else
                System.out.print("*");  
            }
            else
            {
                if(stop_flag == 0)
                System.out.print("  ");
                else
                System.out.print(" ");
            }
        }

        System.out.println();
    }       
    Iterator iter = hm_frequency.keySet().iterator();
    while(iter.hasNext())
    {
        char key = (Character) iter.next();
        if(iter.hasNext())
        System.out.print(key+" ");
        else
        System.out.print(key);
    }
}

public static char maximumchar(TreeMap<Character,Integer> hm_frequency, int max_value)
{
    Iterator iter = hm_frequency.keySet().iterator();
    char ch = '.';
    while(iter.hasNext())
    {
        char key = (Character) iter.next();
        int value = hm_frequency.get(key);
        if(value==max_value)
        {
            ch = key;
        }
    }
    return ch;
}

public static int maxvalue(TreeMap<Character,Integer> hm_frequency)
{
    Iterator iter = hm_frequency.keySet().iterator();
    int max = 0;
    while(iter.hasNext())
    {
        char key = (Character) iter.next();
        int value = hm_frequency.get(key);
        if(value>max)
        {
            max = value;
        }
    }
    return max;
}

}

1 个答案:

答案 0 :(得分:2)

您需要输出完整的字母,并且只打印文本中的字母:

  

大写字母用空格分隔