问题描述:
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
思路:用数组的位置表示每个字符出现的次数,再打印!
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 }
微软在线测试题String reorder,布布扣,bubuko.com
原文:http://www.cnblogs.com/daifei/p/3664138.html