-
Notifications
You must be signed in to change notification settings - Fork 13
/
rearrange-strings-to-two-numbers.cpp
119 lines (100 loc) · 2.1 KB
/
rearrange-strings-to-two-numbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <numeric>
using namespace std;
bool isNb( const vector<int>& v )
{
int len = accumulate(v.begin(), v.end(), 0);
return ( !count_if(v.begin(), v.end(), [](int i){ return i < 0; }) && len )
&& ( (len == 1) || (len == 19 && v[0] == 18 && v[1] == 1) || (len < 19 && v[0] < len) );
}
string toNb( vector<int> v )
{
string ret;
v.resize(10, 0);
for ( int i = 1; i < v.size(); ++i )
{
if ( v[i] )
{
--v[i];
ret += (char)(i + '0');
break;
}
}
for ( int i = 0; i < v.size(); ++i )
{
for ( int j = 0; j < v[i]; ++j )
{
ret += (char)(i + '0');
}
}
return ret;
}
bool test( vector<int>& a, const vector<int>& b )
{
for (size_t i = 0; i < b.size(); ++i)
{
a[i] -= b[i];
}
if (isNb(a))
{
return true;
}
for (size_t i = 0; i < b.size(); ++i)
{
a[i] += b[i];
}
return false;
}
int main()
{
string s;
vector<int> a(10, 0), b(10,0);
cin >> s;
for ( auto& c : s ) { ++a[c -'0']; }
if ( test(a, {1}) )
{
cout << toNb({1}) << " " << toNb(a) << endl;
}
else if (test(a, {18, 1}))
{
cout << toNb(a) << " " << toNb({18, 1}) << endl;
}
else
{
int k = 18;
int last = -1;
for (int i = 9; i >= 0; --i)
{
int d = min(k, a[i]);
if ( d > 0 && i > 0 )
{
last = i;
}
a[i] -= d;
b[i] += d;
k -= d;
}
if ( !isNb(a) && last != -1 )
{
a[last] += 1;
b[last] -= 1;
if (a[0] > 0)
{
a[0] -= 1;
b[0] += 1;
}
}
if (isNb(a) && isNb(b))
{
cout << toNb(a) << " " << toNb(b) << endl;
}
else
{
cout << -1 << " " << -1 << endl;
}
}
return 0;
}