微软2014实习生及秋令营技术类职位在线测试——String reorder

来源:互联网 发布:淘宝客优惠券宣传海报 编辑:程序博客网 时间:2024/05/16 19:29

题目 : String reorder

时间限制: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 “<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.


样例输入
aabbccdd007799aabbccddeeff113355zz1234.89898abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
样例输出
abcdabcd013579abcdefz013579abcdefz<invalid input string>abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa

#include <iostream>#include <string>using namespace std;int main(void) {string str[1024];int num[36],i=0,j=0,m=0,n=0;bool flag;getline(cin,str[n]);while(!str[n].empty()){n++;getline(cin,str[n]);}while(m<n){for(i=0;i<36;i++)num[i]=0;i=0;flag=true;while(i<str[m].length()){if(str[m][i]>='0'&&str[m][i]<='9'){num[str[m][i]-48] += 1;}else if(str[m][i]>='a'&&str[m][i]<='z'){num[str[m][i]-87] += 1;}else{flag=false;cout<<"<invalid input string>"<<endl;break;}i++;}if(flag){while(i--){for(j=0;j<36;j++){if(j>=0&&j<=9){if(num[j]>0){cout<<char(j+48);num[j]--;}}else{if(num[j]>0){cout<<char(j+87);num[j]--;}}}}cout<<endl;}m++;}system("PAUSE");    return 0;}





0 0