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

本文介绍了一个字符串重组算法,该算法能够处理包含特定ASCII字符的输入,并按递增顺序重新排列这些字符,同时确保后续子串为前一个子串的子集。文章提供了详细的解题思路及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目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


解题思路:

关键是对字符串的熟练操作,若是自己不熟悉,一遍测试一遍写,就慢很多。

算法中用到的重要操作细节举例:

string str="abcdefg";

string::iterator iter=str.begin();

str.erase(iter); //执行这条语句,会删除字符 'a', 返回的指向下一个字符'b'的迭代器

cout<<*iter<<endl; //特别注意这里iter会指向字符  'b' 了


下面附上自己实现的一个代码,写的不好,望见谅

#include <iostream>
#include <cstdio> //包含语言重定向函数freopen的库
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <iterator>
using namespace std;
bool valiade(char a){
	if((a>='a' && a<='z') ||(a>='0' && a<='9')){
		return true;
	}else{
		return false;
	}
}
int main(){
	freopen("input.txt","r",stdin); //重定向输入流
	//freopen("ouput.txt","w",stdout);
	string str;
	while(cin>>str){
		bool flag=true;
		sort(str.begin(),str.end());
		//cout<<str<<endl;
		string temp;
		while(!str.empty()){
			string::iterator iter=str.begin();
			char lastch=*(iter);
			if(!valiade(lastch)){
				flag=false;
				break;
			}
			temp += *iter;
			(str.erase(iter));
			for(string::iterator it=str.begin(); it < str.end();){ 
				if(!valiade(lastch)){
					flag=false;
					goto here;
				}
				if( *it != lastch ){
					temp += *it;
					lastch = *it;
					str.erase(it);
				}else{
					++it;
				}
			}
		}
here: 
		if(flag){
			cout<<temp<<endl;
		}else{
			cout<<"<invalid input string>"<<endl;
		}
		
	}
	return 0; 
}

自己又修改了一下,使代码更好看了些。 ^_^

#include <iostream>
#include <cstdio> //包含语言重定向函数freopen的库
#include <algorithm>
#include <string>
#include <iterator>
using namespace std;
bool valiade(char a){
	if((a>='a' && a<='z') ||(a>='0' && a<='9')){
		return true;
	}else{
		return false;
	}
}
int main(){
	freopen("input.txt","r",stdin); //重定向输入流
	//freopen("ouput.txt","w",stdout);
	string str;
	while(cin>>str){
		bool flag=true;
		sort(str.begin(),str.end());
		string temp;
		while(!str.empty()){
			string::iterator iter=str.begin();
			char lastch=' ';
			do{
				if( *iter != lastch){
					temp += *iter;
					lastch = *iter;
					str.erase(iter);
				}else{
					iter++;
				}
				if(!valiade(lastch)){// validate later
					flag=false;
					goto done;
				}
			}while(iter<str.end());
		}
done: 
		if(flag){
			cout<<temp<<endl;
		}else{
			cout<<"<invalid input string>"<<endl;
		}
	}
	return 0; 
}

若你对此问题有疑问,或写出了更紧凑更好的代码,欢迎交流。转载请注明出处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值