您的位置:首页 > 编程语言

微软2014实习生及秋令营技术类职位在线测试(第一题)

2014-04-13 09:56 435 查看

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


附上源代码(C++)

#include <iostream>

#include <string>

using namespace std;

int main(void)

{

    string str;

    //定义一个数组,并将数组中的全部元素赋值为0

    int ascii[256]={};

    cout<<"请输入N个字符串:"<<endl;

    while (cin>>str)

    {

        //getline(cin,str);

        bool flag = true;

        //循环获取数组内每个字符串出现的次数

        for(int i=0; i<str.size(); i++)

        {

            int index = (int)str[i];

            //对字符串中的非法字符进行检测

            if(index>='0'&&index<='9'||index>='a'&&index<='z')

                ascii[index]++;

            else

            {

                cout<<"<invalid input string>";

                flag = false;

                //清空计数数组ascii[]中的数据

                for(int k=0;k<256;k++)

                {

                    ascii[k]=0;

                }

                break;

            }

        }

        //字符串合法时,继续后续操作

        if(flag == true)

        {

            int max=0;

            //打擂台,获取出现次数最多的字符串的出现次数max

            for(int i=0; i<256; i++)

                if(ascii[i]>max)

                    //cout<<(char)i<<"出现:"<<ascii[i]<<endl;

                    max = ascii[i];

            //循环max次

            while(max>0)

            {

                for(int i =0;i<256;i++)

                {

                    //每一次循环时,如果字符串个数不为空,则输出并将个数减1

                    if(ascii[i]!=0)

                    {

                        cout<<(char)i;

                        ascii[i]--;

                    }

                }

                //最大出现次数减1

                max--;

            }

        }

        cout<<endl;

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐