// Google Code Jam Qualification Round Africa 2010 C T9 Spelling
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Google Code Jam
public class GoogleCode1 {
private static MyScanner in;
private static PrintStream out;
static String alpha = " abcdefghijklmnopqrstuvwxyz";
static String digit = "022233344455566677778889999";
static String times = "112312312312312312341231234";
private static void solve() throws IOException
{
int C = in.nextInt();
in.nextLine();
for (int i = 0; i < C; i++) {
out.print("Case #" + (i + 1) + ": ");
solveCase();
}
}
private static void solveCase() throws IOException {
// TODO Auto-generated method stub
String s = in.nextLine();
int idx;
char prevdig = '*';
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == ' ')
idx = 0;
else
idx = 1 + c - 'a';
char t = times.charAt(idx);
int repeat = Integer.valueOf(String.valueOf(t));
char dig = digit.charAt(idx);
if (dig == prevdig)
out.print(' ');
for (int j = 0; j < repeat; j++) {
out.print(dig);
}
prevdig = dig;
}
out.println();
}
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean usingFileForIO = true;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("E:\\zin.txt");
out = new PrintStream("E:\\zout.txt");
}
else {
in = new MyScanner();
out = System.out;
}
solve();
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
public String nextLine() throws IOException {
return inp.nextLine();
}
}
}
I loved how you solved it; very elegant.
ReplyDeletethanks!
Delete