首页 > 其他 > 详细

微软在线测试题String reorder

时间:2014-04-15 10:26:02      阅读:560      评论:0      收藏:0      [点我收藏+]

问题描述:

Time Limit: 10000ms
Case Time Limit: 1000ms
Memory Limit: 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.

Sample In
aabbccdd
007799aabbccddeeff113355zz
1234.89898
abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee

Sample Out
abcdabcd
013579abcdefz013579abcdefz
<invalid input string>
abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa

 

 

思路:用数组的位置表示每个字符出现的次数,再打印!

bubuko.com,布布扣
 1 public class test5 {
 2 
 3     public static void main(String[] args) {
 4         
 5         String aString="abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee";
 6         char b[]=aString.toCharArray();
 7     
 8         aa(b);
 9         
10     }
11     
12     public static void aa(char str[]) {
13         int []count=new int[35];
14         for (int i = 0; i < str.length; i++) {
15             
16             if (str[i]<=‘9‘ && str[i]>=‘0‘) {
17                 count[str[i]-‘0‘]++;
18             }
19             else if (str[i]<=‘z‘&& str[i]>=‘a‘) {
20                 count[str[i]-‘a‘+10]++;
21             }
22             else {
23                 System.out.println("非法");
24             }
25         }
26 
27         
28         while (!count.equals(0)) {
29 
30         for (int j = 0; j < count.length; j++) {
31             
32             
33             if (count[j]>0) {
34                 
35                 if (j<10) {
36                     int y=j+‘0‘;
37                     char x = (char)y;
38                     System.out.print(x+" ");
39                     count[j]--;
40                 }
41                 else {
42                     int y=j+‘a‘-10;
43                     char x = (char)y;
44                     System.out.print(x+" ");
45                     count[j]--;
46                 }
47             
48             }
49         }
50         }
51         
52     }
53     
54     
55 }
View Code

 

微软在线测试题String reorder,布布扣,bubuko.com

微软在线测试题String reorder

原文:http://www.cnblogs.com/daifei/p/3664138.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!