程序员人生 网站导航

2014微软实习生在线编程试题1

栏目:互联网时间:2014-08-30 16:40:39

2014微软实习生在线编程试题1,大家一起来看看微软有什么题目吧
1,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
该问题主要采用哈希比较简单,我就不做介绍了直接上代码:下面代码提交时TLE,实际上可以优化例如设置temp1[255]存储hash值是没必要这么大。
  1. 01.#include<stdio.h>    
  2. 02.#include<stdlib.h>    
  3. 03.#include<string.h>    
  4. 04.char temp[10][100];   
  5. 05.   
  6. 06.void process(char *String)   
  7. 07.{   
  8. 08.    int temp1[256] = {0};   
  9. 09.   
  10. 10.    int index = 0;   
  11. 11.       
  12. 12.    if(String==NULL)   
  13. 13.        return ;   
  14. 14.       
  15. 15.    int len = strlen(String);   
  16. 16.    int i;   
  17. 17.    for(i = 0; i < len; i++)   
  18. 18.    {   
  19. 19.        if((String[i] >='0' && String[i] <='9') || (String[i] >= 'a' && String[i] <='z') )   
  20. 20.        {   
  21. 21.            index = String[i] - '0';   
  22. 22.            temp1[index]++;   
  23. 23.        }   
  24. 24.        else   
  25. 25.        {   
  26. 26.            printf("<invalid input string>");   
  27. 27.        }   
  28. 28.    }   
  29. 29.       
  30. 30.    index = 0;   
  31. 31.       
  32. 32.    while(1)   
  33. 33.    {   
  34. 34.        for(i = 0; i < 256; i++)   
  35. 35.        {   
  36. 36.               
  37. 37.            if(temp1[i] !=0)   
  38. 38.            {   
  39. 39.                String[index] = i + '0';   
  40. 40.                index++;   
  41. 41.                temp1[i]--;   
  42. 42.            }   
  43. 43.               
  44. 44.        }   
  45. 45.           
  46. 46.        if(index == len)   
  47. 47.        {   
  48. 48.            printf("%s\n", String);   
  49. 49.            return;   
  50. 50.        }   
  51. 51.    }   
  52. 52.}   
  53. 53.   
  54. 54.int main()   
  55. 55.{   
  56. 56.    int i = 0;   
  57. 57.    while(gets(temp[i++]) != NULL);   
  58. 58.    i--;   
  59. 59.    int j;   
  60. 60.    for(j = 0; j<i; j++)   
  61. 61.    {   
  62. 62.        process(temp[j]);   
  63. 63.    }   
  64. 64.    return 0;   
  65. 65.}   
  66. 66.       

 

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐