微软的一道笔试编程试题

来源:互联网 发布:怎样打造淘宝店铺爆款 编辑:程序博客网 时间:2024/05/16 03:39
转自:http://www.oschina.net/code/snippet_928691_35005(开源中国)
TimeLimit: 10000ms
Case Time Limit: 1000ms
Memory Limit: 256MB

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.

Sample In

aabbccdd
007799aabbccddeeff113355zz
1234.89898
abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee


Sample Out

abcdabcd
013579abcdefz013579abcdefz
<invalid input string>
abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa

(改进了算法,使用了查找表的方式,上次排序那个部分测试用例有误)
标签: <无
#include <iostream>#include <string>using namespace std;int hash[36] = {'0','1','2','3','4','5',                 '6','7','8','9','a','b',                 'c','d','e','f','g','h',                 'i','j','k','l','m','n',                 'o','p','q','r','s','t',                 'u','v','w','x','y','z'};string subset(string s,unsigned int size){    unsigned int i;    unsigned int j;    unsigned int k = 0;    unsigned int max;    unsigned int num[128] = {0};    string st = s;    for(i = 0; i < size; i++)    {        num[int(s[i])]++;    }    max = num[hash[0]];    for(i = 1; i < 36; i++)    {        if(num[hash[i]]>max)        {            max = num[hash[i]];        }    }    for(j = 0; j < max; j++)    {        for(i = 0; i < 36; i++)        {            if(num[hash[i]]>0)            {                st[k++] = hash[i];                num[hash[i]]--;            }        }    }    return st;}int main(){    string str;    string result;    unsigned int i;    unsigned int f = 1;    while(getline(cin,str))    {        for(i=0; i<str.length(); i++)        {            if((str[i]>='0'&&str[i]<='9')||(str[i]>='a'&&str[i]<='z'))                continue;            else            {                cout<<"<invalid input string>"<<endl;                f = 0;                break;            }        }        if(f)        {            result = subset(str,str.length());            cout<<result<<endl;        }    }    return 0;}


0 0