UVA10008




Sample Input 

3
This is a test.
Count me 1 2 3 4 5.
Wow!!!!  Is this question easy?
Sample Output 

S 7
T 6
I 5
E 4
O 3
A 2
H 2
N 2
U 2
W 2
C 1
M 1
Q 1
Y 1
解法: 儲存記數
import java.util.Collections;
import java.util.Scanner;
import java.util.Vector;

public class UVA10008 {

 static final int NLETTER = 26;

 static Vector<LetterRecord> vl = new Vector<LetterRecord>();

 public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);

  int n = Integer.parseInt(sc.nextLine());

  for (int i = 0; i < NLETTER; i++) {
   LetterRecord aLetter = new LetterRecord((char) ('A' + i));

   vl.add(aLetter);
  }

  for (int i = 0; i < n; i++)

  {
   String aLine = sc.nextLine();

   for (int j = 0; j < aLine.length(); j++) {

    char aChar = aLine.charAt(j);

    int idx;

    if (aChar >= 'A' && aChar <= 'Z')

    {
     idx = aChar - 'A';
     vl.get(idx)._count++;
    } else if (aChar >= 'a' && aChar <= 'z') {
     idx = aChar - 'a';
     vl.get(idx)._count++;
    }

   }

  }

  Collections.sort(vl);

  StringBuilder sb = new StringBuilder();

  for (int i = 0; i < vl.size(); i++) {
   String rStr = vl.get(i).show();
   if (rStr != null)
    sb.append(rStr);
  }

  System.out.print(sb.toString());

  sc.close();
 }

}

class LetterRecord implements Comparable<LetterRecord> {

 public char _letter;
 public int _count;

 LetterRecord(char aLetter) {
  _letter = aLetter;
  _count = 0;
 }

 public String show() {
  if (_count > 0) {
   return String.format("%c %d\n", _letter, _count);
  }

  return null;
 }

 @Override
 public int compareTo(LetterRecord arg0) {
  // TODO Auto-generated method stub
  return (arg0._count - this._count); // in descending order
 }

}