-
aabbccdd 007799aabbccddeeff113355zz 1234.89898 abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
样例输出
-
abcdabcd 013579abcdefz013579abcdefz <invalid input string> abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa
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.
以下是AC代码,不知道是不是测试系统的问题,这个代码前几次提交一直报RE(在本地运行妥妥的),折腾了半个小时,忽然就AC了
思路挺简单,就是统计字母出现的次数,然后按照题中要求依次输出。
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
/**
* Amazure@BUPT
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in =new Scanner(System.in);
String a=null;
while (in.hasNextLine()) {
a=in.nextLine();
boolean bre=false;
char[] chars=a.toCharArray();
int[] counters =new int[36];
for (int i = 0; i < chars.length; i++) {
if (chars[i]>='0'&&chars[i]<='9'||chars[i]>='a'&&chars[i]<='z') {
if (chars[i]>='0'&&chars[i]<='9') {
counters[chars[i]-'0']++;
}
if (chars[i]>='a'&&chars[i]<='z') {
counters[chars[i]-'a'+10]++;
}
}
else {
bre=true;
System.out.println("<invalid input string>");
break;
}
}
String out="";
if (!bre) {
int n=chars.length;
while (n>0) {
for (int j = 0; j < counters.length; j++) {
if (counters[j]!=0) {
if (j<10) {
out+=(char)('0'+j);
}
else {
out+=(char)('a'+j-10);
}
counters[j]--;
n--;
}
}
}
System.out.println(out);
}
}
}
}