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

时间限制: 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.


样例输入
aabbccdd
007799aabbccddeeff113355zz
1234.89898
abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
样例输出
abcdabcd
013579abcdefz013579abcdefz
<invalid input string>
abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa

思路:简单的哈希

[java]  view plain copy 在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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值