本文主要是介绍微软实习生测试题题目1 : String reorder,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 consists of multiple cases, one case per line. Each case is one string consisting of ASCII characters.
For each case, print exactly one line with the reordered string based on the criteria above
aabbccdd 007799aabbccddeeff113355zz 1234.89898 abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
abcdabcd 013579abcdefz013579abcdefz <invalid input string> abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa
代码:
#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{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; }
这篇关于微软实习生测试题题目1 : String reorder的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!