Time Limit:10000ms
Case Time Limit:1000ms
Memory Limit:256MB

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.

Sample Input
aabbccdd
007799aabbccddeeff113355zz
1234.89898
abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
Sample Output
abcdabcd
013579abcdefz013579abcdefz
<invalid input string>
abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa

我的水平有限,但最终也解答出来了。下面贴出个人的答案,欢迎大牛批评指正。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace StringReorder
{
class Program
{ static void Main(string[] args)
{
string input = string.Empty;
input = Console.ReadLine(); GetResults(input); } static void GetResults(string input)
{
string results = "";
if (MatchRequirement(input))
{
List<char> inputCharList = sort(input).ToList();
List<char> tempList = new List<char>();
while (inputCharList.Count > 0)
{ tempList.Add(inputCharList[0]);
inputCharList.RemoveAt(0);
int i = 0;
while (i < inputCharList.Count)
{
if (inputCharList[i] > tempList[tempList.Count - 1])
{
tempList.Add(inputCharList[i]);
inputCharList.RemoveAt(i);
}
else
{
i++;
} }
results += ListToString(tempList);
tempList.Clear();
}
Console.WriteLine(results);
}
} static string ListToString(List<char> tempList)
{
string tempStr = "";
for (int i = 0; i < tempList.Count; i++)
{
tempStr += tempList[i];
}
return tempStr;
}
static string sort(string inputString)
{
char[] inputChars = inputString.ToArray();
string tempStr = "";
Array.Sort(inputChars);
for (int i = 0; i < inputChars.Length; i++)
{
tempStr += inputChars[i];
}
return tempStr;
}
static bool MatchRequirement(string inputString)
{
bool tempValue = false;
char[] inputChars = inputString.ToArray(); for (int i = 0; i < inputChars.Length; i++)
{
if (!match(inputChars[i]))
{
Console.WriteLine("<invalid input string>");
tempValue = false;
break;
}
else
{
tempValue = true;
} }
return tempValue;
}
static bool match(char c)
{
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z'))
{
return true;
}
return false;
}
}
}

微软2014校招笔试题-String reorder的更多相关文章

  1. 微软2017校招笔试题3 registration day

    题目 It's H University's Registration Day for new students. There are M offices in H University, numbe ...

  2. 微软2017校招笔试题2 composition

    题目 Alice writes an English composition with a length of N characters. However, her teacher requires ...

  3. 阿里2014校招笔试题(南大)——利用thread和sleep生成字符串的伪随机序列

    引言:题目具体描述记不大清了,大概是:Linux平台,利用线程调度的随机性和sleep的不准确性,生成一个各位均不相同的字符数组的伪随机序列.不得使用任何库函数.(这句记得清楚,当时在想线程库算不算, ...

  4. 剑指Offer——腾讯+360+搜狗校招笔试题+知识点总结

    剑指Offer--腾讯+360+搜狗校招笔试题+知识点总结 9.11晚7:00,腾讯笔试.选择题与编程.设计题单独计时. 栈是不是顺序存储的线性结构啊? 首先弄明白两个概念:存储结构和逻辑结构. 数据 ...

  5. 剑指Offer——美团内推+校招笔试题+知识点总结

    剑指Offer--美团内推+校招笔试题+知识点总结 前言 美团9.9内推笔试.9.11校招笔试,反正就是各种虐,笔试内容如下: 知识点:图的遍历(DFS.BFS).进程间通信.二叉查找树节点的删除及中 ...

  6. 剑指Offer——CVTE校招笔试题+知识点总结(Java岗)

    剑指Offer(Java岗)--CVTE校招笔试题+知识点总结 2016.9.3 19:00参加CVTE笔试,笔试内容如下: 需要掌握的知识:Linux基本命令.网络协议.数据库.数据结构. 选择题 ...

  7. 剑指Offer——京东校招笔试题+知识点总结

    剑指Offer--京东校招笔试题+知识点总结 笔试感言 经过一系列的笔试,发觉自己的基础知识还是比较薄弱的,尤其是数据结构和网络,还有操作系统.工作量还是很大的.做到精确制导的好方法就是在网上刷题,包 ...

  8. hiho #1288 微软2016.4校招笔试题 Font Size

    #1288 : Font Size 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Steven loves reading book on his phone. The ...

  9. 2016京东Android研发校招笔试题

    一.选择题汇总,具体的记不住啦.. 1.计网:ip的网络前缀.SNMP(报文组成):http://blog.csdn.net/shanzhizi/article/details/11606767 参考 ...

随机推荐

  1. 引用计数——深拷贝&浅拷贝

    下面是用代码实现: private: char *data; size_t use_count; public: //构造函数 String_rep() { if(str == NULL) { dat ...

  2. 386. Lexicographical Numbers 输出1到n之间按lexico排列的数字序列

    [抄题]: Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,1 ...

  3. java_21 Set接口、HashSet类、LinkedSet类

    1Set 包含不可重复元素的集合,是一个无序集合. 子类:hashSet .LinkedSet 2.含有父类Collection的方法 add(): 如果 set 中尚未存在指定的元素,则添加此元素( ...

  4. cmp指令

    cmp是比较指令,cmp的功能相当于减法指令,只是不保存结果.cmp指令执行后,将对标志寄存器产生影响.其他相关指令通过识别这些被影响的标志寄存器位来得知比较结果. cmp指令格式: cmp 操作对象 ...

  5. docker 支持ipv6 (核心要点是ndp需要把docker内的ip全部加入到ndplist中来)

    IPv6 with Docker Estimated reading time: 10 minutes The information in this section explains IPv6 wi ...

  6. MySQL数据查询之多表查询

    多表查询 多表联合查询 #创建部门 CREATE TABLE IF NOT EXISTS dept ( did int not null auto_increment PRIMARY KEY, dna ...

  7. Java 日志体系

    Java 日志体系 <java 日志和 SLF4J 随想>:http://ifeve.com/java-slf4j-think/ 一.常用的日志组件 名称 jar 描述 log4j log ...

  8. vue中鼠标移入字体下面显示颜色并改变字体颜色的问题

    <template> <div class="smart_nav" :class="{'fixedTop':fixedTop}"> &l ...

  9. PHP-循环结构-数组(难)

    今日目标: (1)循环结构 —— do..while.. —— 掌握 (2)循环结构 —— for —— 重点 (3)数组 —— 重点 1.PHP中的循环结构 —— do..while... do: ...

  10. 数据库-SQL语句:删除和修改语句-列类型-列约束

    使用MySQL客户端连接服务器的两种方式: (1)交互模式: ——查 mysql.exe  -h127.0.0.1  -uroot  -p mysql   -uroot (2)脚本模式:——增删改 m ...