【マイクロソフト2014実習生及び秋令営技術職オンラインテスト】テーマ1:String reorder
3856 ワード
時間制限:
10000ms
単一点時限:
1000ms
メモリの制限:
256MB
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 “” when the input contains any invalid characters (i.e., outside the '0'-'9' and 'a'-'z' range).
Input consists of multiple cases, one case per line. Each case is one string consisting of ASCII characters.
For each case, print exactly one line with the reordered string based on the criteria above.
サンプル入力
サンプル出力
考え方:簡単なハッシュ
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 “
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
考え方:簡単なハッシュ
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Scanner;
import java.lang.String;
import java.lang.Math;
import java.util.HashSet;
/*
class TreeNode
{
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; left = null; right = null;}
}
class ListNode
{
int val;
ListNode next;
ListNode(int x){val = x; next = null;}
}
*/
public class Solution {
//static long mode = 1000000007;
/*
public static void permutation(char[] str, HashSet<String> hashset, int start, int end) {
if (start == end) {
hashset.add(new String(str));
//sum++;
}
else {
for (int i = start; i <= end; i++) {
char tmp = str[start];
str[start] = str[i];
str[i] = tmp;
permutation(str, hashset, start+1, end);
tmp = str[start];
str[start] = str[i];
str[i] = tmp;
}
}
}
*/
public static void main(String[] args)
{
//int T ;
Scanner jin = new Scanner(System.in);
//T = jin.nextInt();
while (jin.hasNext()) {
String str = jin.next();
int[] hash_c = new int[75];
for (int i = 0; i < hash_c.length; i++) {
hash_c[i] = 0;
}
int len = str.length();
boolean Isvalid = true;
for (int i = 0; i < len; i++) {
if ((str.charAt(i) >= '0' && str.charAt(i) <= '9') || (str.charAt(i) >= 'a' && str.charAt(i) <= 'z')) {
hash_c[str.charAt(i)-'0']++;
}
else {
Isvalid = false;
break;
}
}
if (!Isvalid) {
System.out.println("<invalid input string>");
continue;
}
StringBuilder strbuild = new StringBuilder();
while (strbuild.length() < len) {
for (int i = 0; i < hash_c.length; i++) {
if (hash_c[i] > 0) {
char ch = (char)(i+'0');
strbuild.append(ch);
hash_c[i]--;
}
}
}
System.out.println(strbuild.toString());
hash_c = null;
}
}
/*
public static double distance(double x, int[] x_array, int[] y_array) {
double dist = 0;
for (int i = 0; i < x_array.length; i++) {
dist += Math.sqrt((x-x_array[i])*(x-x_array[i]) + y_array[i]*y_array[i]);
}
return dist;
}
*/
}