## 2013年9月25日 星期三

### [ Google CT2004 ] RoundA : Problem A. Read Phone Number

Problem:
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.
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
3
15012233444 3-4-4
15012233444 3-3-5
12223 2-3

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

One Solution:

decuple one one

o: triple one double one two
x: quintuple one two

1. package cj2014;
2.
3. import java.util.ArrayList;
4. import java.util.HashMap;
5. import java.util.List;
6. import java.util.Map;
7.
9. import flib.util.io.QSWriter;
10.
11. public class PA {
12.     public static List Lz = new ArrayList();
13.     public static Map DigMap = new HashMap();
14.
15.     static {
16.         // 2 successive numbers use double.
17.         // 3 successive numbers use triple.
18.         // 4 successive numbers use quadruple.
19.         // 5 successive numbers use quintuple.
20.         // 6 successive numbers use sextuple.
21.         // 7 successive numbers use septuple.
22.         // 8 successive numbers use octuple.
23.         // 9 successive numbers use nonuple.
24.         // 10 successive numbers use decuple.
34.         DigMap.put('0'"zero");
35.         DigMap.put('1'"one");
36.         DigMap.put('2'"two");
37.         DigMap.put('3'"three");
38.         DigMap.put('4'"four");
39.         DigMap.put('5'"five");
40.         DigMap.put('6'"six");
41.         DigMap.put('7'"seven");
42.         DigMap.put('8'"eight");
43.         DigMap.put('9'"nine");
44.     }
45.
46.     /**
48.      * @param args
49.      */
50.     public static void main(String[] args) throws Exception{
51.         String fn="A-large-practice.in";
53.         QSWriter qsw = new QSWriter(String.format("data/2014/%s.out", fn)); // 寫出 output
54.         qsr.open();
55.
56.         Integer pc = Integer.valueOf(qsr.line()); // T
57.         for(int i=1; i<=pc; i++)
58.         {
59.             int sc=0;
60.             char pvc='-';
61.             StringBuffer lineBuf = new StringBuffer();
62.             String qs[] = qsr.line().split(" "); // N
63.             String fs[] = qs[1].split("-"); // F
64.             char[] ns = qs[0].toCharArray();
65.             int ci=0;
66.             for(int j=0; j
67.             {
68.                 int r = Integer.valueOf(fs[j]);
69.                 for(int k=0; k
70.                 {
71.                     if(pvc=='-')
72.                     {
73.                         pvc = ns[ci];
74.                         sc++;
75.                     }
76.                     else if(pvc == ns[ci]) sc++;
77.                     else
78.                     {
79.                         String line="";
80.                         if(sc==1) line = String.format("%s ", DigMap.get(pvc));
81.                         else if(sc<=10) line = String.format("%s %s ", Lz.get(sc-2), DigMap.get(pvc));
82.                         else for(int q=0; q<(sc); q++) line = line + DigMap.get(pvc) + " ";
83.                         lineBuf.append(line);
84.                         pvc = ns[ci];
85.                         sc=1;
86.                     }
87.                     ci++;
88.                 }
89.
90.                 if(pvc!='-')
91.                 {
92.                     if(sc==1) lineBuf.append(String.format("%s ", DigMap.get(pvc)));
93.                     else if(sc<=10) lineBuf.append(String.format("%s %s ", Lz.get(sc-2), DigMap.get(pvc)));
94.                     else for(int q=0; q"%s ", DigMap.get(pvc)));
95.                 }
96.                 pvc='-';
97.                 sc=0;
98.             }
99.             qsw.line(String.format("Case #%d: %s", i, lineBuf.toString().trim()));
100.         }
101.         qsr.close();
102.         qsw.close();
103.
104.     }
105. }

### [ Python 文章收集 ] List Comprehensions and Generator Expressions

Source From  Here   Preface   Do you know the difference between the following syntax?  view plain copy to clipboard print ? [x  for ...