微軟2014實習生及秋令營技術類職位線上測試--String reorder

來源:互聯網
上載者:User

題目1 : String reorder

時間限制:10000ms單點時限:1000ms記憶體限制: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.


範例輸入
aabbccdd007799aabbccddeeff113355zz1234.89898abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
範例輸出
abcdabcd013579abcdefz013579abcdefz<invalid input string>abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa


解題思路:

關鍵是對字串的熟練操作,若是自己不熟悉,一遍測試一遍寫,就慢很多。

演算法中用到的重要操作細節舉例:

string str="abcdefg";

string::iterator iter=str.begin();

str.erase(iter); //執行這條語句,會刪除字元 'a', 返回的指向下一個字元'b'的迭代器

cout<<*iter<<endl; //特別注意這裡iter會指向字元  'b' 了


下面附上自己實現的一個代碼,寫的不好,望見諒

#include <iostream>#include <cstdio> //包含語言重新導向函數freopen的庫#include <vector>#include <algorithm>#include <string>#include <cstring>#include <iterator>using namespace std;bool valiade(char a){if((a>='a' && a<='z') ||(a>='0' && a<='9')){return true;}else{return false;}}int main(){freopen("input.txt","r",stdin); //重新導向輸入資料流//freopen("ouput.txt","w",stdout);string str;while(cin>>str){bool flag=true;sort(str.begin(),str.end());//cout<<str<<endl;string temp;while(!str.empty()){string::iterator iter=str.begin();char lastch=*(iter);if(!valiade(lastch)){flag=false;break;}temp += *iter;(str.erase(iter));for(string::iterator it=str.begin(); it < str.end();){ if(!valiade(lastch)){flag=false;goto here;}if( *it != lastch ){temp += *it;lastch = *it;str.erase(it);}else{++it;}}}here: if(flag){cout<<temp<<endl;}else{cout<<"<invalid input string>"<<endl;}}return 0; }

自己又修改了一下,使代碼更好看了些。 ^_^

#include <iostream>#include <cstdio> //包含語言重新導向函數freopen的庫#include <algorithm>#include <string>#include <iterator>using namespace std;bool valiade(char a){if((a>='a' && a<='z') ||(a>='0' && a<='9')){return true;}else{return false;}}int main(){freopen("input.txt","r",stdin); //重新導向輸入資料流//freopen("ouput.txt","w",stdout);string str;while(cin>>str){bool flag=true;sort(str.begin(),str.end());string temp;while(!str.empty()){string::iterator iter=str.begin();char lastch=' ';do{if( *iter != lastch){temp += *iter;lastch = *iter;str.erase(iter);}else{iter++;}if(!valiade(lastch)){// validate laterflag=false;goto done;}}while(iter<str.end());}done: if(flag){cout<<temp<<endl;}else{cout<<"<invalid input string>"<<endl;}}return 0; }

若你對此問題有疑問,或寫出了更緊湊更好的代碼,歡迎交流。轉載請註明出處。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.