算法学习笔记(五)---reorder string

本文解析了一道微软线上测试题,题目要求对包含特定ASCII字符的字符串进行重新排序和分割,并输出符合特定条件的字符串。文章提供了完整的代码实现,包括输入验证及按要求输出排序后的字符串。

微软线上测试最简单的一道题,直接贴上来好了。。。

题目:

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.


  • 样例输入

  • aabbccdd
    007799aabbccddeeff113355zz
    1234.89898
    abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
  • 样例输出

  • abcdabcd
    013579abcdefz013579abcdefz
    <invalid input string>
    abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa

代码。。。先判断输入合法,然后遍历数组,依次把0-9,a-z找到打印,数组相应位置置1表示数据已经使用过来,再遍历第二遍找出0-9,a-z...一直到所有数据都提取出来。

#include <iostream>
using namespace std;
int main(void)
{
	char s[100],firstS[36];
    cin>>s;
	int length=0,key=48,i=0,count=0,truenum=1;
while(s[i]!=0)
	{
		if(s[i]<48||(57<s[i]&&s[i]<97)||s[i]>122)
		{
			cout<<"<invalid input string>"<<endl;
			truenum=0;
			break;
		}
		i++;
	}
   length=i;
   while(count!=length&&truenum==1){
	   for(key=48;key<123;key++){
		   for(i=0;i<length;i++){
		       if(s[i]==key)
			   {
				   cout<<s[i];
				   s[i]=1;
				   count++;
				   break;
			   }
		   }
	   if(key==57) key=96;
	   }
   }
   if(truenum==1)
	   cout<<endl;
   return 0;
}


转载于:https://my.oschina.net/u/1475850/blog/221780

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值