微软在线测试题String reorder
问题描述:
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
思路:用数组的位置表示每个字符出现的次数,再打印!
public class test5 {
public static void main(String[] args) {
String aString="abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee";
char b[]=aString.toCharArray();
aa(b);
}
public static void aa(char str[]) {
int []count=new int[35];
for (int i = 0; i < str.length; i++) {
if (str[i]<='9' && str[i]>='0') {
count[str[i]-'0']++;
}
else if (str[i]<='z'&& str[i]>='a') {
count[str[i]-'a'+10]++;
}
else {
System.out.println("非法");
}
}
while (!count.equals(0)) {
for (int j = 0; j < count.length; j++) {
if (count[j]>0) {
if (j<10) {
int y=j+'0';
char x = (char)y;
System.out.print(x+" ");
count[j]--;
}
else {
int y=j+'a'-10;
char x = (char)y;
System.out.print(x+" ");
count[j]--;
}
}
}
}
}
}

浙公网安备 33010602011771号