微软2014实习生及秋令营之String reorder问题

时间:2014.04.16

地点:基地二楼

----------------------------------------------------------------------

一、原题

【Description】

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

----------------------------------------------------------------------

二、我的思路

  针对该题,首先字符数集本来不多,才10个数字加26个字母,浪费几个byte的内存如果能在速度上更快总可以吧,联系到哈希表,我想把输入全部隐射为一个哈希值,表示该字符在字符串中出现的次数,在这里数字符号0到9对应的ASCII值为48到57,如果我把他都减去48,一方面映射到了数字本身0到9,我正想用他们做数组下标,以便能统计该字符在字串串中出现的次数,对小写字母a-z,它的ASCII为97到122,都减去87,于是同样可映射为数组索引10到35,这样我们就可以用一个大小为36的数组统计每个字符出现的次数,虽然在外界看来,我们的输出还涉及到排序,但这样的设计显然是不用排序了,哇~,自我觉得还是挺不错的一个想法!现在我每次对数组进行从前之后的扫描,遇到数组元素的取值非0得就打印,而打印是我们再可逆向将索引值+48或87就可以了反向映射到字符本身了,是不是?

代码实现如下;

#include<iostream>
using namespace std;
size_t FindFistIndex(int* map_arry);
//Postconditon:
//Postcondition:
bool CharacterMap(int* map_array);
//Postconditon:
//Postcondition:
void CharacterPrint(int* map_array);
//Postconditon:
//Postcondition:
int main()
{
	int char_count[37] = { 0 };
	char_count[36] = 1;  //This extra element used to mark an loop end
	bool is_input_leagal=CharacterMap(char_count);
	if (is_input_leagal)
		CharacterPrint(char_count);
	cout << endl;
	return EXIT_SUCCESS;
}
bool CharacterMap(int* map_array)
{
	char ch;
	while (cin >> ch)
	{
		if (ch >= 48 && ch <= 57)
			++map_array[ch - 48];
		else if (ch >= 97 && ch <= 122)
			++map_array[ch - 87];
		else
		{
			cout << "<invalid input string>" << endl;
			return false;
		}
	}
	return true;
}
size_t FindFistIndex(int* map_array,int old_start)
{
	size_t start_index = old_start;
	while (start_index <= 36)
	{
		if (map_array[start_index])
			break;
		else
			++start_index;
	}
	return start_index;
}
void CharacterPrint(int* map_array)
{
	size_t start_pose = FindFistIndex(map_array, 0);
	size_t last_pose = 35,loop_last_pose = 35;;
	while (start_pose <= last_pose)
	{
		start_pose = FindFistIndex(map_array, start_pose);
		for (size_t index = start_pose; index <=last_pose; ++index)
		{		
			if (map_array[index] != 0)
			{
				if (index < 10)
					cout << static_cast<char>(index + 48);
				else
					cout << static_cast<char>(index + 87);
				--map_array[index];
				loop_last_pose = index;
			}
		}
		last_pose = loop_last_pose;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值