首页 > 其他 > 详细

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

时间:2014-04-13 06:50:32      阅读:256      评论:0      收藏:0      [点我收藏+]

题目1 : 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 s;
	int a[36]; //用来存储 ‘0’到‘9’和 ‘a’到 ‘z’在字符串中出现的次数。例如:0 出现3次 a[0] = 3;  ‘a‘出现1次 a[10] = 1;
	int i = 0; //数组a的下标

	//给数组a初始化
	for(int j = 0; j<36; j++)
	{
		a[j] = 0;
	}

	//输入字符串s,并处理
	while(cin >> s)
	{
		int lenght = s.size();//字符串长度

		//存储 ‘0’到‘9’和 ‘a’到 ‘z’在字符串中出现的次数
		for(int ix = 0; ix != s.size(); ++ix)
		{
			if((s[ix] >= ‘0‘ && s[ix] <= ‘9‘) || (s[ix] >= ‘a‘ && s[ix] <= ‘z‘) )
			{
				if(s[ix] >= ‘0‘ && s[ix] <= ‘9‘)
				{
					i = (int)s[ix] - 48;//‘0‘的ASCII码为48,转化为数组a的下标 48-48 = 0
				}
				else
				{
					i = (int)s[ix] - 87;//‘a‘的ASCII码为97,转化为数组a的下标 97-87 = 10
				}
		
				++a[i] ;//对应的a[i]即对应字符在字符串中出现的次数+1
			}
			else{
				cout << "<invalid input string>" << endl; 
				break;
			}
		}

		//输出从小到大的字符串
		int count = 0;//记录已经输出的字符个数
		while(count  < lenght)
		{
			for(int k = 0 ; k <36; ++k)
			{			
				if( k >= 0 && k<10 && a[k]>0)
				{
					cout << k;
					--a[k];
					count++;
				}
	
				if(k >= 10 && k<36 && a[k]>0)
				{
					cout << (char)(k+87);
					--a[k];
					count++;
				}	
			}
		}

		cout << endl;
	}
	return 0;
}


微软2014实习生及秋令营技术类职位在线测试 题目1 : String reorder,布布扣,bubuko.com

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

原文:http://blog.csdn.net/muzilanlan/article/details/23555805

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!