微软2014实习生及校招秋令营技术类职位在线测试:1.String reorder

来源:互联网 发布:mahout 推荐算法 编辑:程序博客网 时间:2024/05/09 19:43

题目:

Time Limit: 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


各位高手帮我看看我的程序:

为什么报了RE(运行时错误):

下面是我的程序源码:

#include<stdio.h>#include<ctype.h>#include<string.h>#include<stdlib.h>#define MAX 1000int main(void){    char s[MAX]={0};    int digit[10];    int letter[26];    int i=0;    int length=0;    int tag_digit=0,tag_letter=0;    int k=0;    int invalid=0;    while((scanf("%s",s))!=EOF)    {        invalid=0;        k=0;        tag_digit=0;        tag_letter=0;        length=strlen(s);        for(i=0;i<10;++i)            digit[i]=0;        for(i=0;i<26;++i)            letter[i]=0;        for(i=0;i<length && invalid!=1;++i)        {            if(isdigit(s[i]))                digit[s[i]-'0']++;            else if(s[i]<='z' && s[i]>='a')                letter[s[i]-'a']++;            else            {                invalid=1;            }        }        if(invalid==1)        {            printf("invalid input string\n");            continue;        }        while(tag_digit!=1 || tag_letter!=1)        {            tag_digit=1;            tag_letter=1;            for(i=0;i<10;++i)            {                if(digit[i]>0)                {                    tag_digit=0;                    s[k]=(char)(i+'0');                    digit[i]--;                    k++;                }            }            for(i=0;i<26;++i)            {                if(letter[i]>0)                {                    tag_letter=0;                    s[k]=(char)(i+'a');                    letter[i]--;                    k++;                }            }        }        puts(s);        memset(s,0,sizeof(s));    }    return 0;}

运行结果截图如下:



跪求各路大神帮我看看啊,真的郁闷死了。到底为什么会出现RE错误啊?

PS:已经发现了上面的问题,谢谢深圳大学的兄弟,是#define MAX 100限制了本程序,

现在贴上Java源代码:

package com.microsoft;import java.util.Scanner;/* * 微软校招笔试第一题  */public class StringOrder {public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);while (sc.hasNext()) {String s = sc.nextLine();char c[] = s.toCharArray();int[] digit = new int[10];int[] letter = new int[26];int tag_digit = 0, tag_letter = 0;int k = 0;int invalid = 0;for (int i = 0; i < c.length && invalid != 1; ++i) {if (Character.isDigit(c[i]))digit[c[i] - '0']++;else if (Character.isLetter(c[i]))letter[c[i] - 'a']++;elseinvalid = 1;}if (invalid == 1) {System.out.println("<invalid input string>");continue;}while (tag_digit != 1 || tag_letter != 1) {tag_digit = 1;tag_letter = 1;for (int i = 0; i < 10; ++i) {if (digit[i] > 0) {tag_digit = 0;c[k++] = (char) (i + '0');digit[i]--;}}for (int i = 0; i < 26; ++i) {if (letter[i] > 0) {tag_letter = 0;c[k++] = (char) (i + 'a');letter[i]--;}}}for (int i = 0; i < c.length; ++i)System.out.print(c[i]);System.out.println();}}}
唉,悔不当初啊,其实这题不难,看来我还对语言掌握地不够深啊,加油吧,少年。

0 0
原创粉丝点击