【微软2014实习生及秋令营技术类职位在线测试】题目1 : String reorder

来源:互联网 发布:淘宝宝贝链接怎么缩短 编辑:程序博客网 时间:2024/05/15 02:55
时间限制:10000ms
单点时限:1000ms
内存限制:256MB

Description

For this question, your program is required to process an input string containing only ASCII characters between ‘0’ and ‘9’, or between ‘a’ and ‘z’ (including ‘0’, ‘9’, ‘a’, ‘z’).

Your program should reorder and split all input string characters into multiple segments, and output all segments as one concatenated string. The following requirements should also be met,
1. Characters in each segment should be in strictly increasing order. For ordering, ‘9’ is larger than ‘0’, ‘a’ is larger than ‘9’, and ‘z’ is larger than ‘a’ (basically following ASCII character order).
2. Characters in the second segment must be the same as or a subset of the first segment; and every following segment must be the same as or a subset of its previous segment.

Your program should output string “<invalid input string>” when the input contains any invalid characters (i.e., outside the '0'-'9' and 'a'-'z' range).


Input


Input consists of multiple cases, one case per line. Each case is one string consisting of ASCII characters.

Output


For each case, print exactly one line with the reordered string based on the criteria above.


样例输入
aabbccdd007799aabbccddeeff113355zz1234.89898abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
样例输出
abcdabcd013579abcdefz013579abcdefz<invalid input string>abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa

思路:简单的哈希

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. import java.text.DecimalFormat;  
  2. import java.util.Arrays;  
  3. import java.util.Scanner;  
  4. import java.lang.String;  
  5. import java.lang.Math;  
  6. import java.util.HashSet;  
  7. /* 
  8. class TreeNode 
  9. { 
  10.     int val; 
  11.     TreeNode left; 
  12.     TreeNode right; 
  13.     TreeNode(int x) { val = x; left = null; right = null;} 
  14. } 
  15. class ListNode 
  16. { 
  17.     int val; 
  18.     ListNode next; 
  19.     ListNode(int x){val = x; next = null;} 
  20. } 
  21. */  
  22. public class Solution {  
  23.     //static long mode = 1000000007;  
  24.     /* 
  25.     public static void permutation(char[] str, HashSet<String> hashset, int start, int end) { 
  26.         if (start == end) { 
  27.             hashset.add(new String(str)); 
  28.             //sum++; 
  29.         } 
  30.         else { 
  31.             for (int i = start; i <= end; i++) { 
  32.                 char tmp = str[start]; 
  33.                 str[start] = str[i]; 
  34.                 str[i] = tmp; 
  35.                  
  36.                 permutation(str, hashset, start+1, end); 
  37.                   
  38.                 tmp = str[start]; 
  39.                 str[start] = str[i]; 
  40.                 str[i] = tmp; 
  41.             } 
  42.         } 
  43.     } 
  44.     */  
  45.     public static void main(String[] args)   
  46.     {  
  47.         //int T ;  
  48.         Scanner jin = new Scanner(System.in);  
  49.         //T = jin.nextInt();  
  50.         while (jin.hasNext()) {  
  51.             String str = jin.next();  
  52.             int[] hash_c = new int[75];  
  53.             for (int i = 0; i < hash_c.length; i++) {  
  54.                 hash_c[i] = 0;  
  55.             }  
  56.             int len = str.length();  
  57.             boolean Isvalid = true;  
  58.             for (int i = 0; i < len; i++) {  
  59.                 if ((str.charAt(i) >= '0' && str.charAt(i) <= '9') || (str.charAt(i) >= 'a' && str.charAt(i) <= 'z')) {  
  60.                     hash_c[str.charAt(i)-'0']++;  
  61.                 }  
  62.                 else {  
  63.                     Isvalid = false;  
  64.                     break;  
  65.                 }  
  66.             }  
  67.             if (!Isvalid) {  
  68.                 System.out.println("<invalid input string>");  
  69.                 continue;  
  70.             }  
  71.             StringBuilder strbuild = new StringBuilder();  
  72.               
  73.             while (strbuild.length() < len) {  
  74.                 for (int i = 0; i < hash_c.length; i++) {  
  75.                     if (hash_c[i] > 0) {  
  76.                         char ch = (char)(i+'0');  
  77.                         strbuild.append(ch);  
  78.                         hash_c[i]--;  
  79.                     }  
  80.                 }  
  81.             }  
  82.             System.out.println(strbuild.toString());  
  83.             hash_c = null;  
  84.         }  
  85.     }  
  86.     /* 
  87.     public static double distance(double x, int[] x_array, int[] y_array) { 
  88.         double dist = 0; 
  89.         for (int i = 0; i < x_array.length; i++) { 
  90.             dist += Math.sqrt((x-x_array[i])*(x-x_array[i]) + y_array[i]*y_array[i]); 
  91.         } 
  92.         return dist; 
  93.     } 
  94.     */  
  95. }  
0 0