这是一个 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;
}
}