String reorder

来源:互联网 发布:想学网络推广 编辑:程序博客网 时间:2024/05/21 12:42

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.

样例输入
aabbccdd007799aabbccddeeff113355zz1234.89898abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
样例输出
abcdabcd013579abcdefz013579abcdefz<invalid input string>abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa
解答:可以把这些字符和数字存储映射到0-35(10+26)。用一个数组arr[36]初始化为{0},遍历str[i],如果是‘0’-‘9’则,arr[str[i]-'0']++;

否则arr[str[i]-'a'+10]++;因为前10个是存储‘0’-‘9’的。

#include<iostream>#include<string>using namespace std;void Test(string str){int n=str.size();int sum=0,i;int all[36]={0};for(i=0;i<n;i++){if(str[i]<'0'||str[i]>'9'&&str[i]<'a'||str[i]>'z'){cout<<"<invalid input string>"<<endl;    return;}}for(i=0;i<n;i++){if (str[i]>='0'&&str[i]<='9')all[str[i]-'0']++;elseall[str[i]-'a'+10]++;}i=0;while(true){if(all[i%36]>0){if((i%36)<10)cout<<char(i%36+'0');elsecout<<char(i%36-10+'a');all[i%36]--;sum++;}if(sum==n)break;i++;}}int main(){string str;while(cin>>str){Test(str);cout<<endl;}return 0;}







0 0
原创粉丝点击