微软2014年4月 实习生招聘机试题 1.String reorder

本文介绍了一种处理包含特定ASCII字符集的输入字符串的算法,该算法将字符串中的字符重新排序并分割为多个段落,确保每个段落内字符递增排序且后续段落包含前一或更少字符,并在遇到无效字符时输出提示。

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

《1.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

 

个人给出的一种解:

#include <fstream>
#include <iostream>
#include <string>

using namespace std;
#define USE_IFSTREAM 

class CHAR_COUNTER{
public:
	char charValue;
	long charCount;
};
//计数:这里我们需要降序
int compare_CHAR_COUNTER(const void * ptr1,const void * ptr2){
	long value1=(*(CHAR_COUNTER*)ptr1).charCount;
	long value2=(*(CHAR_COUNTER*)ptr2).charCount;
	return (value1>value2)?-1:(value1==value2?0:1);

}
//字符:这里排成降序,后面颠倒顺序后(按升序)输出
int compare_char(const void * ptr1,const void * ptr2){
	char value1 = *(char*)ptr1;
	char value2=  *(char*)ptr2;
	return (value1>value2)?-1:(value1==value2?0:1);

}
void main(){
	CHAR_COUNTER charArray[36];
	char charSet[36+1];
	int valueCharacters=0;
	string str ;
	long length = 0;
	long i = 0;	
	long j = 0;
	int  k = 0;
	int w;
	long curOutPutTime = 0;
	long maxOutPutTime =0;
	long pausePoint =0;
#ifdef USE_IFSTREAM
	ifstream ifs=ifstream("testCase.txt");
#endif

	while(1){
		i=0;
		valueCharacters = 0;
		memset(charArray,0,sizeof(charArray));

		for( i=0;i<36;i++){
			if(i<10)
				charArray[i].charValue = char('0'+ i);
			else 
				charArray[i].charValue = char('a'+(i-10));
			charArray[i].charCount = 0;
		}
#ifndef USE_IFSTREAM
		getline(cin,str);
#else  
		if(ifs.eof())
			break;
		getline(ifs,str);

#endif

		length = str.length();
		i=0;
		while(i<length){
			char charTmp = str[i];
			if(charTmp>='0' && charTmp<='9')
				charArray[charTmp-'0'].charCount++;
			else if(charTmp>='a' && charTmp<='z')
				charArray[charTmp-'a'+10].charCount++;
			else{ 
				cout<<"<invalid input string>"<<endl;
				goto again;
			}
			i++;
		}
		qsort(charArray,sizeof(charArray)/sizeof(CHAR_COUNTER),sizeof(CHAR_COUNTER),compare_CHAR_COUNTER);

		memset(charSet,0,sizeof(charSet));
		i=0;
		while(charArray[i].charCount){
			charSet[i]=charArray[i].charValue;
			i++;
		}
		valueCharacters = i;//最少 1
		qsort(charSet,sizeof(charSet)/sizeof(char)-1,sizeof(char),compare_char);//最后一个不参与排序
		
		for(i=0;i<(valueCharacters/2);i++){ //逆序方便输出
			char tmp=charSet[i];
			charSet[i] = charSet[(valueCharacters-1)-i];
			charSet[(valueCharacters-1)-i] = tmp;
		}

		curOutPutTime= 0;
		maxOutPutTime = charArray[0].charCount;
		pausePoint = charArray[(valueCharacters-1)].charCount;
		while(curOutPutTime < maxOutPutTime){
			if(curOutPutTime == pausePoint){
				//w = valueCharacters;
				if(valueCharacters > 1){
					while(charArray[(valueCharacters-1)].charCount==pausePoint){
						int left=0,right = (valueCharacters-1);
						int index = ((left+right)/2);
						char expectValue = charArray[valueCharacters-1].charValue;
						while(left<=right){//二分查找
							if(charSet[index] == expectValue){
								for( k=index;k<(valueCharacters);k++)
									charSet[k]=charSet[k+1];
								break;
							}
							else if(charSet[index] < expectValue){
								left=index+1;
								index = (left+right)/2;
							}
							else if(charSet[index] > expectValue){
								right =index-1;
								index = (left+right)/2;
							}

						}
						//charSet[j]= 0 ;				
						valueCharacters--;
					}
					if(valueCharacters > 0)
						pausePoint = charArray[valueCharacters-1].charCount;
					charSet[valueCharacters] = '\0';
				}
				
					
			}
			cout<<(char*)(charSet);
			curOutPutTime++;

		}
		cout<<endl;
		flush(cout);
		

		/*
		char chartest[]="again";
		cout<<chartest<<endl;
		*/




again:
		continue;
	}




};




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值