您的位置:首页 > 其它

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

2014-04-13 08:57 369 查看


题目 : 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.



样例输入
aabbccdd
007799aabbccddeeff113355zz
1234.89898
abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee


样例输出
abcdabcd
013579abcdefz013579abcdefz
<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
);
while(!str
.empty())
{
n++;
getline(cin,str
);
}
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  微软 技术 在线测试
相关文章推荐