微软实习生第一题

/*
时间限制:10000ms

单点时限:1000ms

内存限制:256MB

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
*/



#include <stdio.h>
#include<string.h>

#define Length_MAX 500000

char input[Length_MAX];
int count[36] = {0};

//清空数组int count[36]
void clear_count(){
	int i = 0;
	for(i = 0; i < 36; i++){
		count[i] = 0;
	}
}

//判断int count[36]数组中存储的所有元素之和是否为0
int isOver(){
	int sum = 0;
	int i = 0;
	for(i = 0; i < 36; i++){
		sum = sum + count[i];
	}
	return sum;
}

/*
*参数:str[]为字符串数字,length为该字符串数组的长度(包括'\0')
*返回值:如果str[]为有效字符串(只包含0-9或者a-z),则返回1,无效字符串则返回0.
*功能:统计str[]字符串数组中各个字符的数量,并存储到数组int count[36]中,count[0-9]分别对应字符'0'到'9'
*		的数量, count[10-36]分别对应字符'a'-'z'的数量
*/
int init_Count(char str[], int length){
	int i = 0;
	int pos = 0;
	for(i = 0; i < length; i++){
		if(str[i] >= '0' && str[i] <= '9'){
			pos = (int)(str[i] - '0');
			count[pos]++;
		}else if(str[i] >= 'a' && str[i] <= 'z'){
			pos = (int)(str[i] - 'a') + 10;
			count[pos]++;			
		}else{
			return 0;
		}
	}
	return 1;
	
}

int main(){
	int i = 0;
	int j = 0;
	int length = 1; 
	int valid = 0;
	
	gets(input);
	
	for(i = 0; i < length; i++){
		valid = init_Count(input, strlen(input));
		if(valid ==0){
			printf("<invalid input string>\n");
		}else{
			char temp = '0';
			while(isOver() != 0){
				for(j = 0; j < 36; j++){
					if(count[j] > 0){
						if(j>= 0 && j <=9){
							temp = (char)('0'+j);
							printf("%c", temp);					
						}else{
							temp = (char)('a'+j-10);
							printf("%c", temp);
						}
						
						count[j]--;
					}
				}
			}
			printf("\n");
		}
	
		clear_count();
	}
	
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值