fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b) {
  14. int x = 1;
  15. a %= M;
  16. while (b) {
  17. if (b & 1) x = (x * a) % M;
  18. a = (a * a) % M;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30.  
  31. string consistency(string num){
  32.  
  33. vector<int> even;
  34. vector<int> odd;
  35.  
  36. for(int i=0; i<num.size(); i++){
  37. int d = num[i]-'0';
  38. if(d & 1) odd.push_back(num[i]);
  39. else even.push_back(num[i]);
  40. }
  41.  
  42.  
  43. int m = even.size();
  44. int n = odd.size();
  45.  
  46. string ans = "";
  47.  
  48. int i = 0, j = 0;
  49. while(i<m && j<n){
  50. if(even[i] < odd[j]){
  51. ans.push_back(even[i]);
  52. i++;
  53. }
  54. else {
  55. ans.push_back(odd[j]);
  56. j++;
  57. }
  58. }
  59.  
  60. while(i<m){
  61. ans.push_back(even[i]);
  62. i++;
  63. }
  64. while(j<n){
  65. ans.push_back(odd[j]);
  66. j++;
  67. }
  68.  
  69.  
  70. return ans;
  71. }
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. string practice(string num){
  88.  
  89.  
  90. return "";
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97. void solve() {
  98.  
  99. string num;
  100. cin >> num;
  101.  
  102. cout << consistency(num) << endl;
  103.  
  104.  
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. int32_t main() {
  112. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  113.  
  114. int t = 1;
  115. cin >> t;
  116. while (t--) {
  117. solve();
  118. }
  119.  
  120. return 0;
  121. }
Success #stdin #stdout 0s 5320KB
stdin
3
0709
1337
246432
stdout
0079
1337
234642