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,我不知道为什么。
- 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的更多相关文章
- 微软2014实习生及秋令营技术类职位在线测试(题目1 : String reorder)
题目1 : String reorder 时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description For this question, your program ...
- 微软在线测试题String reorder
问题描述: Time Limit: 10000msCase Time Limit: 1000msMemory Limit: 256MB DescriptionFor this question, yo ...
- 微软2014校招笔试题-String reorder
Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB Description For this question, your pro ...
- G面经prepare: Reorder String to make duplicates not consecutive
字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...
- 【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 ...
- hdoj 5500 Reorder the Books
Reorder the Books Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- 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 ...
- hdu5362 Just A String(dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Just A String Time Limit: 2000/1000 MS (J ...
- [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 ...
随机推荐
- 【转】CwRsync简介
rsync是linux下一款用于同步文件的优秀软件,window下也可以使用它,不过名字为cwRsync.cwRsync也分为客户端和服务端,官方网址:https://www.itefix.no/i2 ...
- ios开发 iphone中获取网卡地址和ip地址
这是获取网卡的硬件地址的代码,如果无法编译通过,记得把下面的这几个头文件加上把. #include <sys/socket.h> // Per msqr#include <sys/s ...
- VPS及LNMP(一)
1.试用了搬瓦工.interserver.天翼云.网安互联之后,分别写下感受: 前两个是美国服务器,天翼云是国内服务器,网安互联是香港主机. a.搬瓦工非常便宜,512M的1年9.99刀,但是用了之后 ...
- lua 和 c
lua程序其实本身并不能执行,它必须依靠c语言编写的解释器来解释执行,或者说解释器为lua脚本的执行,提供了一个运行环境(lua_state),其中包括函数堆栈,内存分配和回收等机制. 理论上,lua ...
- Excel 如何按条件计数和按条件求和(如按月求和)
1.使用SUMPRODUCT进行多条件计数语法:=SUMPRODUCT((条件1)*(条件2)*(条件3)* …(条件n))作用:统计同时满足条件1.条件2到条件n的记录的个数.实例:=SUMPROD ...
- cookie sessionStorage localStorage 区别
sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...
- Java和C++中的static
1.Java类中的static变量和static方法会在类装载的过程中就得到内存分配,然后就会进行初始化工作.最多可能会被初始化3次,静态代码块的执行在main方法之前. static变量不可以在构造 ...
- KBMMW 4.90.00 发布
kbmMW is a portable, highly scalable, high end application server andenterprise architecture integra ...
- (转)jQuery中的extend()方法
本文转自:http://www.xiabingbao.com/jquery/2015/05/30/jquery-extend 原文的排版要比这里美观很多,建议去原文查看.本文仅仅作为个人的mark,方 ...
- eclipse 使用