微软的一道笔试编程试题
来源:互联网 发布:怎样打造淘宝店铺爆款 编辑:程序博客网 时间:2024/05/16 03:39
转自:http://www.oschina.net/code/snippet_928691_35005(开源中国)
TimeLimit: 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 In
aabbccdd
007799aabbccddeeff113355zz
1234.89898
abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
Sample Out
abcdabcd
013579abcdefz013579abcdefz
<invalid input string>
abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa
(改进了算法,使用了查找表的方式,上次排序那个部分测试用例有误)
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 In
aabbccdd
007799aabbccddeeff113355zz
1234.89898
abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
Sample Out
abcdabcd
013579abcdefz013579abcdefz
<invalid input string>
abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa
(改进了算法,使用了查找表的方式,上次排序那个部分测试用例有误)
标签: <无
#include <iostream>#include <string>using namespace std;int hash[36] = {'0','1','2','3','4','5', '6','7','8','9','a','b', 'c','d','e','f','g','h', 'i','j','k','l','m','n', 'o','p','q','r','s','t', 'u','v','w','x','y','z'};string subset(string s,unsigned int size){ unsigned int i; unsigned int j; unsigned int k = 0; unsigned int max; unsigned int num[128] = {0}; string st = s; for(i = 0; i < size; i++) { num[int(s[i])]++; } max = num[hash[0]]; for(i = 1; i < 36; i++) { if(num[hash[i]]>max) { max = num[hash[i]]; } } for(j = 0; j < max; j++) { for(i = 0; i < 36; i++) { if(num[hash[i]]>0) { st[k++] = hash[i]; num[hash[i]]--; } } } return st;}int main(){ string str; string result; unsigned int i; unsigned int f = 1; while(getline(cin,str)) { for(i=0; i<str.length(); i++) { if((str[i]>='0'&&str[i]<='9')||(str[i]>='a'&&str[i]<='z')) continue; else { cout<<"<invalid input string>"<<endl; f = 0; break; } } if(f) { result = subset(str,str.length()); cout<<result<<endl; } } return 0;}
0 0
- 微软的一道笔试编程试题
- 微软的一道面试题
- 微软的一道面试题
- 微软的一道面试题
- 一道微软的面试题
- 微软的一道面试题
- 一道网上找的微软笔试题
- 微软一道笔试题目
- 一道微软笔试题
- 一道微软笔试题
- 微软的一道面试题的解法
- 关于一道微软面试题的思考
- 关于一道微软面试题的解答
- 一道微软面试题的解答
- 对一道微软面试题的思考
- 微软亚洲研究院的一道面试题
- 关于微软的一道面试题
- 一道曾经微软的面试题
- hadoop 分布式安装
- 第七周作业3:循环大战!
- 提高数据库处理查询速度
- 开源 免费 java CMS - FreeCMS1.8 在线申报
- CSS3盒子模型
- 微软的一道笔试编程试题
- NYOJ20 吝啬的国度 【深搜】
- 经典SQL--输入一个数求每位的和
- free pascal(lazarus)版的android JNI进阶篇:反向调用java的方法并传递复杂参数
- Android基础知识记录|面试常见问题:activity被回收,如何保存回收前的状态
- 使用U盘制作linux服务器启动盘,U盘制作普通系统盘的制作过程也是一样
- bzoj 2120 数颜色 题解
- [Java]泛型
- Struts2教程5:使用Validation框架验证数据