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 ...
随机推荐
- DestroyWindow函数注意事项
最近遇到这样一个问题:将一个窗口句柄以参数的形式传递给一个线程,在线程中使用完之后要将窗口销毁,调用DestroyWindow销毁窗口是返回false,GetLastError的结果为5:拒绝访问,而 ...
- Codeforces Round #226 (Div. 2) B
B. Bear and Strings time limit per test 1 second memory limit per test 256 megabytes input standard ...
- '<', hexadecimal value 0x3C, is an invalid 问题解决
你的web.config 里面一定有个节点的不完整,如 错误如下: 正确的如下:
- EXT学习之——EXT下拉框默认绑定第一个值
//默认第一个下拉框绑定值if (this.moduleCombo.store.getAt(0) != undefined) { this.moduleCombo.setValue(this.modu ...
- Python TCP客户端
import socket target_host="www.baidu.com" target_port=80 # 建立一个socket对象 client=socket.sock ...
- NHibernate系列文章十二:Load/Get方法
摘要 NHibernate提供两个方法按主键值查找对象:Load/Get. 1. Load/Get方法的区别 Load: Load方法可以对查询进行优化. Load方法实际得到一proxy对象,并不立 ...
- 通知 Notification
1. NotificationManager ma=(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVIC ...
- Java中的容器
参考链接:Java容器与泛型(1) 认识容器,Java容器与泛型(2) ArrayList.LinkedList和Vector比较,Java容器与泛型(3) Hashset.TreeSet和Linke ...
- Android源代码结构分析
Google提供的Android包含了:Android源代码,工具链,基础C库,仿真环境,开发环境等,完整的一套.第一级别的目录和文件如下所示:----------------├── Makefile ...
- Error -27780: [GENERAL_MSG_CAT_SSL_ERROR]connect to host "124.202.213.70" failed: [10054] Connection reset by peer [MsgId: MERR-27780]
解决方案一: 备注: 此方案如果请求响应时间太长,勾选"WinInet replay instead of Sockets(Windows only)"将会导致如下错误: