微軟2014實習生及秋令營技術類職位在線測試——String reorder

題目 : 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[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;
}





發佈了63 篇原創文章 · 獲贊 18 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章