本问题出自:微软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,我不知道为什么。

 using System;
using System.Linq;
using System.Text;
namespace SDET_1
{
class Program
{
static void Main(string[] args)
{
while (true)
{
bool isValid = true;
string s = Console.ReadLine();
char[] c = s.ToCharArray();
byte[] b = new byte[c.Length];
for (int i = ; i < c.Length; i++)
{
b[i] = (byte)c[i];
if (b[i] < || b[i] > || (b[i] < && b[i] > ))
{
isValid = false;
}
}
if (!isValid)
{
Console.WriteLine("<invalid input string>");
continue;
}
char[] dc = c.Distinct().ToArray();
dc = dc.OrderBy(item => item).ToArray();
int[] count = new int[dc.Length];
for (int i = ; i < dc.Length; i++)
{
count[i] = (from item in c where item == dc[i] select item).Count();
}
StringBuilder str = new StringBuilder();
int num = count.Max();
for (int j = ; j < num; j++)
{
for (int i = ; i < dc.Length; i++)
{
if (count[i] > )
{
str.Append(dc[i]);
count[i]--;
}
}
}
Console.WriteLine(str.ToString());
}
}
}
}

String reorder的更多相关文章

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

    题目1 : String reorder 时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description For this question, your program ...

  2. 微软在线测试题String reorder

    问题描述: Time Limit: 10000msCase Time Limit: 1000msMemory Limit: 256MB DescriptionFor this question, yo ...

  3. 微软2014校招笔试题-String reorder

    Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB Description For this question, your pro ...

  4. G面经prepare: Reorder String to make duplicates not consecutive

    字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...

  5. 【Leetcode】Reorder List JAVA

    一.题目描述 Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must ...

  6. hdoj 5500 Reorder the Books

    Reorder the Books Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  7. LeetCode解题报告:Reorder List

    Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… Yo ...

  8. hdu5362 Just A String(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Just A String Time Limit: 2000/1000 MS (J ...

  9. [Swift]LeetCode143. 重排链表 | Reorder List

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...

随机推荐

  1. 制作HP MicroServer Gen8可用的ESXi 5.x SD/TF卡启动盘

    前些日子看到HP公司和京东在搞服务器促销活动,于是就入了一个 ProLiant MicroServer Gen8 的低配版 相比上一代产品,新一代 MicroServer系列微型服务器可更换处理器,还 ...

  2. C#EXCEL 操作类--C#ExcelHelper操作类

    主要功能如下1.导出Excel文件,自动返回可下载的文件流 2.导出Excel文件,转换为可读模式3.导出Excel文件,并自定义文件名4.将数据导出至Excel文件5.将指定的集合数据导出至Exce ...

  3. net.sf.json 时间格式的转化

    后台代码 //后台代码 response.setCharacterEncoding("UTF-8"); JsonConfig jsonConfig = new JsonConfig ...

  4. jquery datatable 参数

    DataTables(http://www.datatables.net/)应该是我到目前为止见过的,功能最强大的表格解决方案(当然,不计算其它整套框架中的table控件在内). 先把它主页上写的特性 ...

  5. nohup输入密码后继续后台运行

    Linux/Unix 是真正的多用户,多任务.Linux 提供了 fg 和bg 命令,让你轻松调度正在运行的任务. 假设你发现前台运行的一个程序需要很长的时间,但是需要干其他的事情,你就可以用 Ctr ...

  6. PCI Express(四) - The transaction layer

    原文出处:http://www.fpga4fun.com/PCI-Express4.html 感觉没什么好翻译的,都比较简单,主要讲了TLP的帧结构 In the transaction layer, ...

  7. windows 10启动盘制作工具

    Rufus 官方网站:http://rufus.akeo.ie/

  8. Android IOS WebRTC 音视频开发总结(七十)-- 移动端音视频技术优化的七个方向

    最近直播很火,很多朋友对背后的技术比较感兴趣,所以今天我们整理一篇关于移动端视频优化的文章,这篇文章是我朋友在一个技术大会上分享过的,更多内容请关注我们的微信公众号:rtcblacker 视频直播为什 ...

  9. SQLSERVER | 查询数据库中所有的表的名字 | 查询数据库中的所有数据库名

    SQLSERVER 1.查询某个数据库中所有的表名:  SELECT Name FROM SysObjects Where XType='U' ORDER BY Name 2.查询数据库中的所有数据库 ...

  10. FTP\TFTP

    FTP是文件传输协议的英文简写. FTP 文件传输协议 基于TCP,20和21端口. TFTP 简单文件传输协议,基于UDP,69 只能传输32Mb以下文件,不需要提供账号和密码.tftp xxx.x ...