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 shocould reorder and split all input string characters into multiple segments, and output all segments as one concatenated string. The following requirements shoshould also be met,
1. characters in each segment shoshould 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 shocould 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 abve.
-
Sample Input
-
aabbccdd007799aabbccddeeff113355zz1234.89898abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
-
Sample output
-
abcdabcd013579abcdefz013579abcdefz<invalid input string>abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa
Solution:
The key is to be proficient in string operations. If you are not familiar with it, testing and writing will be much slower.
Examples of important operations used in algorithms:
String str = "abcdefg ";
String: iterator iter = str. begin ();
Str. erase (iter); // when this statement is executed, the 'A' character is deleted and the iterator pointing to the next 'B' character is returned.
Cout <* iter <endl; // note that the iter points to the character 'B '.
The following is a self-implemented code, which is not well written. Sorry.
# Include <iostream> # include <cstdio> // library containing the language Redirect function 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); // redirect the input stream // 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 ;}
I modified it to make the Code look better. Pai_^
# Include <iostream> # include <cstdio> // library containing the language Redirect function 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); // redirect the input stream // freopen ("ouput.txt", "w", stdout); string str; while (cin> str) {bool flag = true; sort (str. begin (), str. end (); string temp; whi Le (! 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 ;}
If you have any questions or have written more compact and better code, please contact us. Indicate the source for reprinting.