Problem
The Constitution of a certain country states that the leader is the person with the name containing the greatest number of different alphabet letters. (The country uses the uppercase English alphabet from A through Z.) For example, the name GOOGLE
has four different alphabet letters: E, G, L, and O. The name APAC CODE JAM
has eight different letters. If the country only consists of these 2 persons, APAC CODE JAM
would be the leader.
If there is a tie, the person whose name comes earliest in alphabetical order is the leader.
Given a list of names of the citizens of the country, can you determine who the leader is?
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line with an interger N, the number of people in the country. Then N lines follow. The i-th line represents the name of the i-th person. Each name contains at most 20 characters and contains at least one alphabet letter.
Output
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y is the name of the leader.
Limits
1 ≤ T ≤ 100.
1 ≤ N ≤ 100.
Small dataset
Each name consists of at most 20 characters and only consists of the uppercase English letters A
through Z
.
Large dataset
Each name consists of at most 20 characters and only consists of the uppercase English letters A
through Z
and ' '(space).
All names start and end with alphabet letters.
Sample
Input |
Output |
2 3 ADAM BOB JOHNSON 2 A AB C DEF |
Case #1: JOHNSON Case #2: A AB C |
import java.io.*;
public class CountryLeader {
public static void main(String[] args) {
File outfile = new File("D://Code//Java//workspace//kickstart2017//src//kickstart2017//outputforsmall.txt"); //创建输出文件对象
try {
FileWriter out =new FileWriter(outfile); //创建FileWriter对象
BufferedWriter bufw = new BufferedWriter(out); //创建BufferedWriter类对象
FileReader fr = new FileReader("D://Code//Java//workspace//kickstart2017//src//kickstart2017//A-small-practice.in");
BufferedReader bufr = new BufferedReader(fr);
String cases = null;
//读取第一行信息得到case的值,为字符变量
cases = bufr.readLine();
int numofcases = Integer.parseInt(cases); //字符串转变成int常量
for(int j = 1;j < numofcases+1; j++){ //对每一个案列分别进行处理
String N = null;
N = bufr.readLine();
int numofnames = Integer.parseInt(N);
int numofcharacter[] = new int[numofnames]; //数组存放每个名字的字母个数
String nameofarrays[] = new String[numofnames];//将所有名字放入到一个字符串数组中去
for(int k = 0;k < numofnames; k++){ //对每个名字即每行进行处理
int ch[] = new int[26]; //数组存放26个字母的出现次数
String names = bufr.readLine(); //读取一行,得到名字中包含所有的字母
nameofarrays[k] = names;
for(int m = 0;m < names.length();m++){
char c = names.charAt(m); //依次取出每个字母
int index = c-'A'; //
ch[index] = ch[index] + 1;// 对应字母出现则存储字母的数组加1
}
int numofalp = 0; //求出每个数组中不为0的元素的个数即为不同字母的个数
for(int n = 0;n < 26; n++){
if(ch[n]>= 1){
numofalp++;
}
}
numofcharacter[k] = numofalp; //将每个名字包含的字母数存储到数组中
}
int maxzhi = 0;
int maxzhiindex = 0;
for(int p = 0;p < numofnames;p++ ){ //求取每个案列中的最大值和其对应的名字
maxzhi = numofcharacter[0];
if(numofcharacter[p] > maxzhi){
maxzhi = numofcharacter[p];
maxzhiindex = p;
}
}
System.out.println("Case #"+ j +":" +" " + nameofarrays[maxzhiindex]);
bufw.write("Case #"+ j +":" +" " + nameofarrays[maxzhiindex]);
bufw.newLine();
}
bufr.close();
fr.close(); //将FileReader流关闭
bufw.close();
out.close(); // 将输出流关闭
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}