这是一个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;
}
}
最佳答案
您需要输出完整的字母,并且只打印文本中的字母:
大写字母由空格分隔