[codeforces]#350F. Restore a Number

本文介绍了一种算法,用于解决给定一组打乱顺序的数字串和一个已知子串的情况下,如何找出原始整数的最小可能值。通过两种方法实现:一种涉及贪心策略与特殊情况处理;另一种则是通过构造不同部分的组合来逐步逼近最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

F. Restore a Number

        time limit per test2 seconds
    memory limit per test256 megabytes
            inputstandard input
           outputstandard output

Vasya decided to pass a very large integer n to Kate. First, he wrote that number as a string, then he appended to the right integer k — the number of digits in n.

Magically, all the numbers were shuffled in arbitrary order while this note was passed to Kate. The only thing that Vasya remembers, is a non-empty substring of n (a substring of n is a sequence of consecutive digits of the number n).

Vasya knows that there may be more than one way to restore the number n. Your task is to find the smallest possible initial integer n. Note that decimal representation of number n contained no leading zeroes, except the case the integer n was equal to zero itself (in this case a single digit 0 was used).

Input
The first line of the input contains the string received by Kate. The number of digits in this string does not exceed 1 000 000.

The second line contains the substring of n which Vasya remembers. This string can contain leading zeroes.

It is guaranteed that the input data is correct, and the answer always exists.

Output
Print the smalles integer n which Vasya could pass to Kate.

Examples
input
003512
021
output
30021
input
199966633300
63
output
3036366999
题目大意:给定一组数字字符串,其中包含一个大数和大数的长度,且这些数字被随机存储
第二行为大数包含的子数列
求出这个大数能够得到的最小数
首先求出大数的长度
len为数字字符串的长度,digits从0递增判断(len-digits)的位数+(len-digits)是否与len相等
求出大数的长度
接着求最小数
方法一(有些复杂):
把整个数列分成3部分子数列前pre,子数列,子序列后
基本上是贪心加特殊处理。

#include <cstdio>
#include <set>
#include <vector>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
int cnt[13];
string s;
string remaind, tt;
int len, ll;
int main() {
    cin>> s;
    cin>>tt;
    int len = s.length();
    int n = 0;
    ll = 0;
    if (len == 2) {
        cout << tt << endl;
        return 0;
    }
    for (int i = 0; i < 10; i++) {
        cnt[i] = 0;
    }
    for (int i = 0; i < len; i++) {
        cnt[s[i]-'0']++;
    }
    int t = len-1;
    int digits = 1;
    int dt;
    int f = t;
    dt = 0;
    while (f > 0) {
        f/=10;
        dt++;
    }
    while (t+dt != len) {
        digits++;
        t = len-digits;
        int f = t;
        dt = 0;
        while (f > 0) {
            f/=10;
            dt++;
        }
    }
    len = t;
    while (t) {
        cnt[t%10]--;
        t /= 10;
    }
    int firstt = tt[0]-'0', flag = 0, mintt = 1;
    for (int i = 0; i < tt.length(); i++) {
        cnt[tt[i]-'0']--;
        if (i != 0 && !flag) {
            if (tt[i]-'0' < firstt) {
                flag = 1;
                mintt = tt[i]-'0';
            } else if (tt[i]-'0' > firstt)
                flag = 2;
        }
    }
    int countzero = 0;
    if (mintt == 0) {
        int flagg = 0;
        for (int i = 0; i < tt.length(); i++) {
            if (tt[i] == '0') {
                flagg = 1;
                countzero++;
            } else if (flagg == 1) {
                flag = 3;
                break;
            }
        }
        if (flag == 3 && countzero > cnt[0])
            flag = 1;
    }
    for (int i = 1; i < 10; i++) {
        if (cnt[i] > 0) {
            ll = i;
            break;
        }
    }
    string result;
    if (firstt != 0 && ll != 0) {
        if (ll < firstt) {
            result += (char)(ll+'0');
            cnt[ll]--;
            for (int i = 0; i < firstt; i++) {
                while (cnt[i] > 0) {
                    result += (char)(i+'0');
                    cnt[i]--;
                }
                ll = i+1;
            }
        }
        else if (ll == firstt && cnt[0] > 0 && (mintt != 0 || flag != 1)) {
            result += (char)(ll+'0');
            cnt[ll]--;
            while (cnt[0] > 0) {
                result += '0';
                cnt[0]--;
            }
        }
        if (ll == firstt && flag != 1 && flag != 3) {
            while (cnt[ll] > 0) {
                result += (char)(ll+'0');
                cnt[ll]--;
            }
            ll = ll+1;
        }
    } else if (ll != 0){
        result += (char)(ll+'0');
        cnt[ll]--;
        while (cnt[0] > 0) {
            result += '0';
            cnt[0]--;
        }
    }
    result += tt;
    for (int i = 0; i < 10; i++) {
        while (cnt[i] > 0) {
            result += (char)(i+'0');
            cnt[i]--;
        }
    }
    cout << result << endl;
    return 0;
}

方法二:
把整个数列分成3部分子数列前pre,子数列,子序列后
首先
将字符串best弄成子数列+子序列后的字符串
接着

设置子序列前的首字母为除0最小数字
循环i从0~9 :
在子序列前添加为i的全部数字
判断best是否大于该序列+子序列+子序列后
是就修改
结束循环
输出

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <numeric>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <vector>
#include <math.h>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef long double ld;

template <typename T>
T nextInt() {
    T x = 0, p = 1;
    char ch;
    do { ch = getchar(); } while(ch <= ' ');
    if (ch == '-') {
        p = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return x * p;
}

const int maxN = (int)1e6 + 10;
const int maxL = 17;
const int INF = (int)1e9;
const int mod = (int)1e9 + 7;
const ll LLINF = (ll)1e18;

vector <int> a(10);
vector <int> b(10);
char s[maxN];
char t[maxN];

string get(const vector<int>&cnt) {
    string ret = "";
    for (int i = 0; i < 10; ++i) {
        ret += string(cnt[i], char('0' + i));
    }
    return ret;
}

int main() {

  //  freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);
    gets(s);
    gets(t);

    if (strcmp(s, "01") == 0 || strcmp(s, "10") == 0) {
        puts("0");
        return 0;
    }

    int n = strlen(s);
    int m = strlen(t);

    for (int i = 0; i < n; ++i) {
        a[s[i] - '0']++;
    }
    for (int i =0 ; i < m; ++i) {
        b[t[i] - '0']++;
    }

    string T = (const char *) t;

    for (int len = 1; ; ++len) {
        vector <int> cnt = a;
        int tmp = len;
        int c = 0;
        while(tmp > 0) {
            cnt[tmp % 10]--;
            tmp /= 10;
            c++;
        }
        bool ok = true;
        for (int i = 0; i < 10; ++i) {
            cnt[i] -= b[i];
            if (cnt[i] < 0) ok = false;
        }
        if (ok && (t[0] != '0' || *max_element(cnt.begin() + 1, cnt.end())) > 0 && n - c == len) {

        } else continue;
        bool have = false;
        string best = "";
        {
            string cur = T + get(cnt);
            if (cur[0] != '0') {
                if (!have || best > cur) {
                    best = cur;
                    have = true;
                }
            }
        }
        string cur = "";
        for (int i = 1; i < 10; ++i) {
            if (cnt[i] > 0) {
                cur += char(i + '0');
                cnt[i]--;
                break;
            }
        }
        for (int i = 0; i <= 10; ++i) {
            string now = cur + T + get(cnt);

            if (now[0] != '0') {
                if (!have || best > now) {
                    best = now;
                    have = true;
                }
            }
            if (i != 10) {
                cur += string(cnt[i], char(i + '0'));
                cnt[i] = 0;
            }
        }
        assert(have);
     //   cout << len << '\n';
        puts(best.c_str());
        exit(0);
    }

    return 0;
}
### 关于Codeforces Round 925 Div. 3 的题目及解析 #### A. Plus One on the Subset 在这个问题中,给定一个整数序列 \(a_1, a_2, \ldots, a_n\) 和若干次操作。每次可以选择任意子集并将这些位置上的元素增加一。目标是最少的操作次数使得所有元素都变成相同的值[^1]。 ```cpp #include <iostream> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; bool all_same = true; int first_value; cin >> first_value; for (int i = 1; i < n; ++i) { int temp; cin >> temp; if (temp != first_value) all_same = false; } if (all_same || n == 1) cout << "0\n"; else cout << "1\n"; } } ``` 这段代码通过判断输入数组中的所有元素是否已经相同来决定最少需要几次操作可以完成任务。如果所有的数值一开始就相等,则不需要任何改变;如果有不同的数值存在,则只需一次操作就可以让整个集合内的数字变得一致。 #### B. Multiply by Two, Divide by One 此题要求处理一系列由两个正整数构成的询问,对于每一个询问给出的结果是能否仅利用两种变换——将当前数翻倍或是除以二(当且仅当该数为偶数时),最终达到另一个指定的目标值。 为了实现这一点,可以从终点反向推导回起点,在每一步尝试逆运算直到回到起始状态或者发现不可能的情况为止。具体来说就是不断地执行减半(如果是奇数则无法继续)以及加上其一半的过程,直至到达原点或确认不可达性。 #### C. Make It Increasing 这个问题的任务是在不违反特定条件下尽可能多地保留原始数组里的项的同时构建一个新的严格递增序列。允许的操作是从已有的列表里移除某些成员而不改变其余部分相对顺序。 一种有效的策略是对初始数据先做预处理排序之后再逐一遍历比较相邻两项之间的关系,以此为基础做出取舍决策。这样做的好处是可以快速定位哪些地方出现了重复或者是不符合增长趋势的地方,并据此调整方案。 #### D. Yet Another Array Partitioning Problem 本题涉及到了如何分割一个长度固定的整型数组成多个连续片段的问题,目的是最小化各分段内部最大差值之和的最大可能值。这实际上是一个经典的动态规划问题变种版本之一。 解决方法通常涉及到定义合适的DP表格用于记录中间计算结果以便后续查询使用。这里的关键在于合理设计转移方程从而能够高效求得最优解路径。同时还需要注意边界条件设定以免造成逻辑错误影响整体性能表现。 #### E. The Strongest Build 针对这一挑战,背景设置在一个虚拟编程竞赛平台之上,参赛者们被鼓励提交自己的解决方案参与评分竞争。然而有趣的是,评判标准并非单纯基于运行效率而是取决于所选算法复杂度级别与实际消耗资源量之间是否存在显著差异作为奖励依据。 因此解答此类问题往往需要综合考量时间空间开销等因素的影响程度,进而挑选出最适宜当下环境状况的最佳实践方式来进行编码实现。此外还需特别留意特殊测试用例的存在可能性及其应对措施安排等问题细节之处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值