微软2014实习生在线测试之String reorder .

// 问题描述:
// 
// Time Limit: 10000ms
// Case Time Limit: 1000ms
// Memory Limit: 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.
// 
// Sample In
// aabbccdd
// 007799aabbccddeeff113355zz
// 1234.89898
// abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
// 
// Sample Out
// abcdabcd
// 013579abcdefz013579abcdefz
// <invalid input string>
// abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa
// 
//思想:使后面的串或者等于前面的串或者为前面串的子串,也就是说,使前面的串最长并且是有序的。
//用map存放每个合法字符出现的次数.map[0:9]对应0-9,map[10-35]对应a-z,可见,遍历map而输出对应字符时,是从小到大输出的。
//map[i]>0,表示可以用来构建字符串,即可以输出,同时次数-1.
//为了防止死循环,设置bool 变量hasElement, 当有元素输出时,置hasElement为true。
//以00aa33bb为例,map[0] ==2, map[3]==2, map[10]==2,map[11]==2.
//为使前面字符尽量长,故将map中可以使用的(map[i]>0)都输出一遍,同时--map[i].
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str;
	cout << "please input string:";
	
	while (cin >> str)
	{
		int size_str = str.size();
		int map[36] = {0}; //存放字符出现的次数,map[0:9]对应0-9,map[10-35]对应a-z
		//统计每个合法字符出现的次数
		for (int i=0; i<size_str; ++i)
		{
			if ('0'<=str[i] && str[i] <= '9' )
			{
				++map[str[i]-'0'];
			}
			else if ('a'<=str[i] && str[i] <= 'z')
			{
				++map[str[i]-'a'+10];
			}
			else
			{
				cout << "<invalid input string> "<< endl;
				break;
			}
		}//end for


		//由于map[0-35]中分别存放0-9,a-z等字符出现的次数,从map[0]开始遍历map,将可以使用的子符取出,
		//来构建前面的字符串(此时已经保证了从小到大排序)
		//并且使前面的字符串取得最长,每次使字符串尽量长。
		//
		bool hasElement = true;//用来标记有没有字符输出了,有则为真,否则为假

		while (hasElement)
		{
			hasElement = false;
			for (int i=0; i< 36; ++i)
			{//为使前面的字符尽量长,就输出map中所有可以使用的字符
				
				if (map[i]>0)
				{
					if (0<=i && i<= 9)
						cout << (char)(i+'0');
					else
						cout << (char)(i+'a'-10);
					hasElement = true; //有元素输出时
					--map[i];
				}
				//如果没有元素输出了,hasElement == false
			}//end for
		}//end while
		cout << endl;
		cout << "please input string:";
	}//end while

	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值