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

《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;
	}




};




 

基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip 个人大四的毕业设计、课程设计、作业、经导师指导并认可通过的高分设计项目,评审平均分达96.5分。主要针对计算相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 [资源说明] 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设或者课设、作业,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96.5分,放心下载使用! 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),供学习参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值