Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- #include <map>
- using namespace std;
- bool lest(vector<int>& a, vector<int>& b, map<int, int> b_map, int n, long long& c_num, vector<int>& c, int i, int coun) {
- if (coun == 0) {
- return true;
- }
- if (b_map[c[i] - a[i] - b[i]] > 0) {
- b_map[c[i] - a[i] - b[i]]--;
- b[i] = c[i] - a[i] - b[i];
- coun--;
- if (lest(a, b, b_map, n, c_num, c, i + 1, coun)) {
- return true;
- }
- }
- else if (b_map[c[i] - a[i] + 10 - b[i]] > 0) {
- b_map[c[i] - a[i] + 10 - b[i]]--;
- b[i] = c[i] - a[i] + 10 - b[i];
- b[i + 1] = 1;
- coun--;
- if (lest(a, b, b_map, n, c_num, c, i + 1, coun)) {
- return true;
- }
- }
- return false;
- }
- int main()
- {
- string x, y, z;
- cin >> x >> y >> z;
- long long c_num = 0, coun = 0;
- int n = max(x.length(), max(y.length(), z.length()));
- n++;
- vector<int> a(x.length(), 0), b(n, 0), c(n, 0);
- map<int, int> b_map;
- for (int i = 0; i < x.length(); ++i) {
- a[i] = x[x.length() - i - 1] - '0';
- }
- sort(a.begin(), a.end());
- for (int i = 0; i < y.length(); ++i) {
- b_map[y[i] - '0']++;
- coun++;
- }
- for (int i = 0; i < z.length(); ++i) {
- c[i] = z[z.length() - i - 1] - '0';
- c_num = c_num * 10 + z[i] - '0';
- }
- int x_ans = -1, y_ans = -1;
- do {
- reverse(a.begin(), a.end());
- a.push_back(0);
- if (lest(a, b, b_map, n, c_num, c, 0, coun)) {
- reverse(a.begin(), a.end());
- long long ans_x = 0;
- for (int i = 0; i < a.size(); ++i) {
- ans_x = ans_x * 10 + a[i];
- }
- reverse(a.begin(), a.end());
- long long ans_y = 0;
- reverse(b.begin(), b.end());
- for (int i = 0; i < b.size(); ++i) {
- ans_y = ans_y * 10 + b[i];
- }
- if (x_ans == -1 || x_ans > ans_x) {
- x_ans = ans_x;
- y_ans = ans_y;
- }
- }
- a.pop_back();
- reverse(a.begin(), a.end());
- b.clear();
- b.assign(n, 0);
- } while (next_permutation(a.begin(), a.end()));
- if (x_ans == -1) {
- cout << "NO";
- }
- else {
- cout << "YES\n" << x_ans << " " << y_ans;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement