String reorder
来源:互联网 发布:想学网络推广 编辑:程序博客网 时间:2024/05/21 12:42
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.
aabbccdd007799aabbccddeeff113355zz1234.89898abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
abcdabcd013579abcdefz013579abcdefz<invalid input string>abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa解答:可以把这些字符和数字存储映射到0-35(10+26)。用一个数组arr[36]初始化为{0},遍历str[i],如果是‘0’-‘9’则,arr[str[i]-'0']++;
否则arr[str[i]-'a'+10]++;因为前10个是存储‘0’-‘9’的。
#include<iostream>#include<string>using namespace std;void Test(string str){int n=str.size();int sum=0,i;int all[36]={0};for(i=0;i<n;i++){if(str[i]<'0'||str[i]>'9'&&str[i]<'a'||str[i]>'z'){cout<<"<invalid input string>"<<endl; return;}}for(i=0;i<n;i++){if (str[i]>='0'&&str[i]<='9')all[str[i]-'0']++;elseall[str[i]-'a'+10]++;}i=0;while(true){if(all[i%36]>0){if((i%36)<10)cout<<char(i%36+'0');elsecout<<char(i%36-10+'a');all[i%36]--;sum++;}if(sum==n)break;i++;}}int main(){string str;while(cin>>str){Test(str);cout<<endl;}return 0;}
- String reorder
- string reorder
- String reorder
- String reorder
- 题目1 : String reorder
- 练习 题目1 : String reorder
- 【微软2014实习生】 String reorder
- 2014微软在线测试-String reorder-WA
- String reorder(一道自定义排序题)
- 2014微软实习生笔试题-String reorder
- 微软2014校招笔试题-String reorder
- 微软2014实习生招聘编程测试string reorder
- 微软实习机试题 String reorder 的 Java 实现
- 微软2014实习生在线测试之String reorder
- 微软2014实习生及秋令营之String reorder问题
- (2014微软实习生笔试题)1.String reorder
- 微软2014实习生在线测试之String reorder .
- 微软实习生测试题题目1 : String reorder
- AutomationException: 0x80040220
- close与RST
- 王建硕的2001交大Microsoft Club宣讲会
- java中的内部接口
- 设计模式学习之——单例模式
- String reorder
- 共轭分布(Conjugate distributions)
- matlab中的diag,spdiags函数
- 在eclipse中结合maven调试web项目
- 黑马程序员---一DAY17总结--java中你必须掌握的集合框架技术
- ZOJ-3767
- 判断单链表是否有环
- 栈的基本操作
- Git和github的初步使用