题目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
解题思路:建立一个统计所有字符的数组,然后依次从最小字符到最大字符向容器中存放,并且每当放入容器中一个字符,要将相应的对应位上的统计数自减1,直到所有字符输出完毕。

要注意的是对空格的处理,如果直接用字符串做变量,就无法正确读入空格,因此要用getline(cin, str);


//source here
#include <iostream>
#include <vector>
#include <string>

using namespace std;

string reorderStr(string str){
	int c[256] = {0};
	if(0 == str.size()) return "<invalid input string>";
	for(size_t i = 0; i < str.size(); i++){
		if(!(str[i] >= '0' && str[i] <= '9' || str[i] >= 'a' && str[i] <= 'z')){
			return "<invalid input string>";
		}
		c[str[i]]++;
	}

	
	vector<char> vc;

	int count = str.size();

	while(count){
		for(int i = 0; i < 256; i++){
			if(c[i]){
				vc.push_back(i);
				c[i]--;
				count--;
			}
		}
	}
	string retStr(vc.begin(), vc.end());

	return retStr;
}

int main(){
	string str;

	getline(cin, str); // cin >> str;这样写只能得90分
	cout << reorderStr(str) << endl;

	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值