勤奋的小孩

导航

String reorder

本问题出自:微软2014实习生及秋令营技术类职位在线测试 (Microsoft Online Test for Core Technical Positions)

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).

这道题你需要处理0~9,a~z的输入

你需要把输入的数据分段输出,每一段段内要按顺序排列(ASCII的顺序),且每一段只能出现一次,后一段是前一段的子集。

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 Input

aabbccdd

007799aabbccddeeff113355zz

1234.89898

abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee

Sample Output

abcdabcd

013579abcdefz013579abcdefz

<invalid input string>

abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa

我的解题思路

首先找到这一串字符中都有哪些字符

统计每个字符出现的个数

显示一遍所用个数不为零的字符,个数减1

循环上一步直到个数为0

C#代码

运行结果是Running Error,我不知道为什么。

 1 using System;
 2 using System.Linq;
 3 using System.Text;
 4 namespace SDET_1
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             while (true)
11             {
12                 bool isValid = true;
13                 string s = Console.ReadLine();
14                 char[] c = s.ToCharArray();
15                 byte[] b = new byte[c.Length];
16                 for (int i = 0; i < c.Length; i++)
17                 {
18                     b[i] = (byte)c[i];
19                     if (b[i] < 48 || b[i] > 122 || (b[i] < 97 && b[i] > 57))
20                     {
21                         isValid = false;
22                     }
23                 }
24                 if (!isValid)
25                 {
26                     Console.WriteLine("<invalid input string>");
27                     continue;
28                 }
29                 char[] dc = c.Distinct().ToArray();
30                 dc = dc.OrderBy(item => item).ToArray();
31                 int[] count = new int[dc.Length];
32                 for (int i = 0; i < dc.Length; i++)
33                 {
34                     count[i] = (from item in c where item == dc[i] select item).Count();
35                 }
36                 StringBuilder str = new StringBuilder();
37                 int num = count.Max();
38                 for (int j = 0; j < num; j++)
39                 {
40                     for (int i = 0; i < dc.Length; i++)
41                     {
42                         if (count[i] > 0)
43                         {
44                             str.Append(dc[i]);
45                             count[i]--;
46                         }
47                     }
48                 }
49                 Console.WriteLine(str.ToString());
50             }
51         }
52     }
53 }

 

posted on 2014-04-12 22:00  勤奋的小孩  阅读(315)  评论(0编辑  收藏  举报