微软2014实习生及秋令营技术类职位在线测试 原创解答 题目1 : String reorder
来源:互联网 发布:怎样入驻阿里云大厦 编辑:程序博客网 时间:2024/04/30 05:39
- 样例输入
aabbccdd007799aabbccddeeff113355zz1234.89898abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
- 样例输出
abcdabcd013579abcdefz013579abcdefz<invalid input string>abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa
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.
解答如下:
#include <iostream>#include <stdio.h>#include <vector>#include <algorithm>#include <cstring>using namespace std;bool parseChar(char * c);class CInputStr{public:char m_cStr;int m_iNum;};struct Cmp_by_name //为排序编写的结构体{bool operator()(const CInputStr &a, const CInputStr &b){return a.m_cStr<b.m_cStr;}};int main() {char c[5][100]; char * result[5];int inumcount; int index=0; int temp; int minnum=10000;for (int i=0;i<5;i++){cin.getline(c[i],100);if (parseChar(c[i])){}else{char b[100]="<invalid input string>";strcpy(c[i],b);}}for (int i=0;i<5;i++){cout<<c[i]<<endl;}}bool parseChar(char * c){int icount=0;char * result=c;vector <CInputStr> vInput;while(c[icount]!='\0'){if ((c[icount]>='0'&&c[icount]<='9')||(c[icount]>='a'&&c[icount]<='z')){if (0==vInput.size()){CInputStr cinput;cinput.m_cStr=c[icount];cinput.m_iNum=1;vInput.push_back(cinput);}else{for (int i=0;i<vInput.size();i++){if (c[icount]==vInput[i].m_cStr){vInput[i].m_iNum++;break;}if (i==vInput.size()-1){CInputStr cinput;cinput.m_cStr=c[icount];cinput.m_iNum=1;vInput.push_back(cinput);break;}}}}else{//cout<<"<invalid input string>";return false;}icount++;}int iVectorCount=0;icount=0;sort(vInput.begin(),vInput.end(),Cmp_by_name());while (0!=vInput.size()){if (0!=vInput[iVectorCount].m_iNum){result[icount]=vInput[iVectorCount].m_cStr;vInput[iVectorCount].m_iNum-=1;icount++;}else{if(0==vInput[vInput.size()-1].m_iNum){vInput.pop_back();}}if (iVectorCount+1>=vInput.size()){iVectorCount=0;}else{iVectorCount++;}}vInput.empty();return true;}
- 微软2014实习生及秋令营技术类职位在线测试 原创解答 题目1 : String reorder
- 微软2014实习生及秋令营技术类职位在线测试-题目1 : String reorder
- 微软2014实习生及秋令营技术类职位在线测试-题目1 : String reorder
- 微软2014实习生及秋令营技术类职位在线测试 题目1 : String reorder
- 微软2014实习生及秋令营技术类职位在线测试(题目1 : String reorder)
- 【微软2014实习生及秋令营技术类职位在线测试】题目1 : String reorder
- 【微软2014实习生及秋令营技术类职位在线测试】题目1 : String reorder
- 微软2014实习生及秋令营技术类职位在线测试 String reorder java
- 微软2014实习生及校招秋令营技术类职位在线测试:1.String reorder
- 微软2014实习生及秋令营技术类职位在线测试——String reorder
- 微软2014实习生及秋令营技术类职位在线测试——String reorder
- 微软2014实习生及秋令营技术类职位在线测试:String reorder
- 微软2014实习生及秋令营技术类职位在线测试--String reorder
- 微软2014实习生及秋令营技术类职位在线测试第一题:String reorder
- 微软2014实习生及秋令营技术类职位在线测试-1_String reorder
- 微软2014实习生及秋令营技术类职位在线测试-题目2 : K-th string
- 【微软2014实习生及秋令营技术类职位在线测试】题目2 : K-th string
- 微软2014实习生及秋令营技术类职位在线测试题目1+题目2
- 实现多个Raspberry Pi相互传送文件的同步
- Pat(Advanced Level)Practice--1068(Find More Coins)
- 把vim打造成IDE
- Qt 获取字符串的UTF8编码值
- EAS BOS开发引入外部JAR
- 微软2014实习生及秋令营技术类职位在线测试 原创解答 题目1 : String reorder
- DSP调试心得
- 观察者模式
- c++ 使用函数使用指南
- Android 常用XML属性介绍
- 基本滴简易的feed系统
- extjs开发辅助spket插件安装
- Python语言的创始人解释为什么Python数组的索引从0开始
- Mac 10.9 使用 protobuf 2.4.1