Google Code Jam Notes - Read Phone Number - Java

来源:互联网 发布:ppt数据展示模板 编辑:程序博客网 时间:2024/06/06 03:30
Problem:
Retrieved from: http://code.google.com/codejam/contest/2924486/dashboard#s=p0

Do you know how to read the phone numbers in English? Now let me tell you.

For example, In China, the phone numbers are 11 digits, like: 15012233444. Someone divides the numbers into 3-4-4 format, i.e. 150 1223 3444. While someone divides the numbers into 3-3-5 format, i.e. 150 122 33444. Different formats lead to different ways to read these numbers:

150 1223 3444 reads one five zero one double two three three triple four.

150 122 33444 reads one five zero one double two double three triple four.

Here comes the problem:

Given a list of phone numbers and the dividing formats, output the right ways to read these numbers.

Rules:

Single numbers just read them separately.

2 successive numbers use double.

3 successive numbers use triple.

4 successive numbers use quadruple.

5 successive numbers use quintuple.

6 successive numbers use sextuple.

7 successive numbers use septuple.

8 successive numbers use octuple.

9 successive numbers use nonuple.

10 successive numbers use decuple.

More than 10 successive numbers read them all separately.

Input

The first line of the input gives the number of test cases, TT lines|test cases follow. Each line contains a phone number N and the dividing format F, one or more positive integers separated by dashes (-), without leading zeros and whose sum always equals the number of digits in the phone number.

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the reading sentence in English whose words are separated by a space.

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ length of N ≤ 10.

Large dataset

1 ≤ length of N ≤ 100.

Sample


Input 
  
315012233444 3-4-415012233444 3-3-512223 2-3


Output 
 
Case #1: one five zero one double two three three triple fourCase #2: one five zero one double two double three triple fourCase #3: one two double two three

Analysis:
Take your time and simulate each step.

Time complexity O(n).

My solution: (Your opinion is highly appreciated)

package codeJam.google.com;import java.io.BufferedReader;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;/** * @author Zhenyi 2013 Dec 22, 2013 11:01:40 AM */public class ReadPhoneNumber {public static void main(String[] args) throws IOException {BufferedReader in = new BufferedReader(new FileReader("C:/Users/Zhenyi/Downloads/A-small-practice.in"));FileWriter out = new FileWriter("C:/Users/Zhenyi/Downloads/A-small-practice.out");// BufferedReader in = new BufferedReader(new FileReader(// "C:/Users/Zhenyi/Downloads/A-large-practice.in"));// FileWriter out = new FileWriter(// "C:/Users/Zhenyi/Downloads/A-large-practice.out");Integer T = new Integer(in.readLine());for (int cases = 1; cases <= T; cases++) {String[] st = in.readLine().split("\\s");String[] st1 = st[1].split("-");String[] st2 = new String[st1.length];int pos = 0;for (int i = 0; i < st1.length; i++) {Integer len = new Integer(st1[i]);st2[i] = st[0].substring(pos, pos + len);pos = pos + len;}String result = "";for (int i = 0; i < st2.length; i++) {int loc = 0;while (loc < st2[i].length()) {if (loc == st2[i].length() - 1) {result = result + check(st2[i].charAt(loc), 1);loc++;} else {if (st2[i].charAt(loc) != st2[i].charAt(loc + 1)) {result = result + check(st2[i].charAt(loc), 1);loc++;} else {int num = 2;loc++;while (loc + 1 < st2[i].length()&& st2[i].charAt(loc) == st2[i].charAt(loc + 1)) {num++;loc++;}result = result + check(st2[i].charAt(loc), num);loc++;}}}}out.write("Case #" + cases + ":" + result + "\n");}in.close();out.flush();out.close();}private static String check(char c, int i) {String s = "";if (i > 1 && i < 11) {switch (i) {case 2:s = " double";break;case 3:s = " triple";break;case 4:s = " quadruple";break;case 5:s = " quintuple";break;case 6:s = " sextuple";break;case 7:s = " septuple";break;case 8:s = " octuple";break;case 9:s = " nonuple";break;case 10:s = " decuple";break;}s = s + numTS(c);} else {for (int j = 0; j < i; j++) {s = s + numTS(c);}}return s;}private static String numTS(char c) {String s = "";switch (c) {case '0':s = " zero";break;case '1':s = " one";break;case '2':s = " two";break;case '3':s = " three";break;case '4':s = " four";break;case '5':s = " five";break;case '6':s = " six";break;case '7':s = " seven";break;case '8':s = " eight";break;case '9':s = " nine";break;}return s;}}


0 0
原创粉丝点击