首页 > 其他 > 详细

微软实习生第一题

时间:2014-04-13 19:42:35      阅读:607      评论:0      收藏:0      [点我收藏+]
/*
时间限制: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();
	}
	
}


微软实习生第一题,布布扣,bubuko.com

微软实习生第一题

原文:http://blog.csdn.net/wyj7260/article/details/23599365

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!