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.
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 
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: 
基本上這像是送分的, 只要會 coding 應該都解得出來. 沒有太多的技巧與數學在裡面. 唯一要注意的是 "連續10個以上" 還是個別念, 所以 11111111111 會是 11 個 one 而不是: 
decuple one one

另外再處理 range 的時候, 不要把前面的連續帶到後面來, 如 "111 112" 應該是: 
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.   
  8. import flib.util.io.QSReader;  
  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.  
  25.         Lz.add("double");  
  26.         Lz.add("triple");  
  27.         Lz.add("quadruple");  
  28.         Lz.add("quintuple");  
  29.         Lz.add("sextuple");  
  30.         Lz.add("septuple");  
  31.         Lz.add("octuple");  
  32.         Lz.add("nonuple");  
  33.         Lz.add("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.     /** 
  47.      * BD: https://code.google.com/codejam/contest/2924486/dashboard#s=p0 
  48.      * @param args 
  49.      */  
  50.     public static void main(String[] args) throws Exception{  
  51.         String fn="A-large-practice.in";  
  52.         QSReader qsr = new QSReader(String.format("data/2014/%s", fn)); // 讀入 input  
  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. }  
上面的 QSReader 與 QSWriter 是用到 flib library

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...