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

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

题目:数组

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’).ide

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).ui


Input


Input consists of multiple cases, one case per line. Each case is one string consisting of ASCII characters.this

Output


For each case, print exactly one line with the reordered string based on the criteria above.spa


  • 样例输入code

  • aabbccdd
    007799aabbccddeeff113355zz
    1234.89898
    abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
  • 样例输出ip

  • abcdabcd
    013579abcdefz013579abcdefz
    <invalid input string>
    abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa

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

#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;
}
相关文章
相关标签/搜索