算法學習筆記(五)---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;
}
相關文章
相關標籤/搜索