您的位置:首页 > 其它

微软2014实习生及秋令营技术类职位在线测试-题目1 : String reorder

2014-04-12 21:18 513 查看
时间限制:10000ms
单点时限:1000ms
内存限制:256MB


Description

Forthisquestion,yourprogramisrequiredtoprocessaninputstringcontainingonlyASCIIcharactersbetween‘0’and‘9’,orbetween‘a’and‘z’(including‘0’,‘9’,‘a’,‘z’).
Yourprogramshouldreorderandsplitallinputstringcharactersintomultiplesegments,andoutputallsegmentsasoneconcatenatedstring.Thefollowingrequirementsshouldalsobemet,

1.Charactersineachsegmentshouldbeinstrictlyincreasingorder.Forordering,‘9’islargerthan‘0’,‘a’islargerthan‘9’,and‘z’islargerthan‘a’(basicallyfollowingASCIIcharacterorder).

2.Charactersinthesecondsegmentmustbethesameasorasubsetofthefirstsegment;andeveryfollowingsegmentmustbethesameasorasubsetofitsprevioussegment.
Yourprogramshouldoutputstring“<invalidinputstring>”whentheinputcontainsanyinvalidcharacters(i.e.,outsidethe'0'-'9'and'a'-'z'range).


Input

Inputconsistsofmultiplecases,onecaseperline.EachcaseisonestringconsistingofASCIIcharacters.


Output

Foreachcase,printexactlyonelinewiththereorderedstringbasedonthecriteriaabove.



样例输入
aabbccdd
007799aabbccddeeff113355zz
1234.89898
abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee


样例输出
abcdabcd
013579abcdefz013579abcdefz
<invalidinputstring>
abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa




packageStringReorder;

importjava.io.BufferedInputStream;
importjava.util.Scanner;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;

publicclassMain{

publicstaticvoidmain(String[]args){
booleanflag=false;
Scannercin=newScanner(newBufferedInputStream(System.in));
while(cin.hasNextLine()){
Stringstr=cin.nextLine();
Patternpattern=Pattern.compile("[0-9a-z]+");
Matchermatcher=pattern.matcher(str);
booleanb=matcher.matches();
if(!b){
System.out.println("<invalidinputstring>");
}else{
flag=true;
//System.out.println(str);
//HashMap<String,Integer>hm=new<String,Integer>HashMap(36);
int[]count=newint[45];
for(inti=0;i<str.length();i++){
charc=str.charAt(i);
//System.out.println((int)c);
if(c>=48&&c<=57){
count[c-48]++;
}elseif(c>=97&&c<=122){
count[10+c-97]++;
}
}

while(flag){

for(inti=0;i<count.length;i++){
if(count[i]>0){
if(i>=0&&i<=9)
System.out.print((char)(48+i));
elseif(i>=10&&i<=45)
System.out.print((char)(97+i-10));

count[i]--;
}
}

flag=false;

for(inti=0;i<count.length;i++)
if(count[i]>0){
flag=true;
break;
}
}
System.out.println();
}
}
cin.close();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐