微软在线笔试第一题 复杂算法

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.

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

#define MAXWORDS 200

void character_sort(char s[], int n);
void final_process(char s[], int n);

int main()
{
	char str[MAXWORDS];
	char c;
	int i, j, num;

	memset(str,'@',sizeof(str));
	//memset(str2,0,sizeof(str1[1]));

	i = 0;
	j = 0;
	num = 0;
	printf("INPUT THE STRING YOU WANT!\n");
	while((c = getchar()) != '\n')
	{
		//str1[0][i] = c;
		if ((48 <= c && c <= 57) || (97 <= c && c <= 122))
		{
			str[i] = c;
			i++;
			num++;
		}
		else
		{
			printf("<invalid input string>\n");
			return -1;
		}
	}

	character_sort(str,num);
	for(i = 0; i < num; i++)
	{
		printf("%c", str[i]);
	}
	printf("\n");
	final_process(str,num);


	i = 0;
	while((str[i] != '@'))
	{
		printf("%c", str[i]);
		i++;
	}
	return 0;
}

void character_sort(char A[], int N)
{
	char tmp;
	int i,j;
	for (i = 0; i < N - 1; i++)
	{
		for (j = i + 1; j < N; j++)
		{
			if (A[j] < A[i])
			{
				tmp = A[j];
				A[j] = A[i];
				A[i] = tmp;
			}
		}
	}
}

void final_process(char str[], int num)
{
	char str1[2][MAXWORDS];
	char str2[MAXWORDS];
	int i, j, k, tmp, tmp1;


	memset(str1[0],'@',sizeof(str1[0]));
	memset(str1[1],'2',sizeof(str1[1]));
	memset(str2,'@',sizeof(str2));

	i = 0;
	j = 0;
	k = 0;

	for (i = 0; i < num; i++)
	{
		str1[0][i] = str[i];
	}

	for (i = 0; i < num; i++)
	{
		
		if (str1[1][i] == '2')
		{
			str2[k] = str1[0][i];
			tmp = str1[0][i];
			for (j = 0; j < num; j++)
			{
				if (str1[1][j] == '2')
				{
					if (str1[0][j] > tmp)
					{

						str2[++k] = str1[0][j];
						str1[1][j] = 1;
						tmp1 = str1[0][j];
						tmp = tmp1;

					}
				}
			}
			k++;
		}
		
	}

	for (i = 0; i < MAXWORDS; i++)
	{
		str[i] = str2[i];
	}
}

基本算法思想——

先将输入 的字符按照ASCII码的顺序列下来,重复的将被排列到一起,即112339xxxyyyyyz这种形式放在数组str[MAXWORDS]存储下来;然后创建一个二维数组str1[2][MAXWORDS],第一行全部存储str数组中的元素,第二行存放标志位,判断该元素是否已经使用过。

函数final_process(),此时str1[0][]中的字符都是按照顺序排好的,我们只需处理重复的问题即可。使用元素依次与后面的元素进行大小比较,大的存放进结果数组中,其标志位被置1(标志位开始的初值为2)并成为新的比较标准,这样当i=0时,排列出第一个无重复的升序序列;i++;在进行下一轮大小比较,并进行判断,看该元素是否之前一轮中已被置入到结果数组str中.这样便得到最终结果。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值