CF-915C. Permute Digits

本文介绍CodeForces平台上的915C题目的解题思路及实现代码。该题目要求对给定的两个正整数a和b进行处理,通过改变a的数字排列顺序来构造一个不超过b的最大可能数字。文章详细解释了解决方案,并提供了一个使用深度优先搜索(DFS)算法的C++实现。

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

http://codeforces.com/problemset/problem/915/C

Problem Description

You are given two positive integer numbers a and b. Permute (change order) of the digits of a to construct maximal number not exceeding b. No number in input and/or output can start with the digit 0.

It is allowed to leave a as it is.

Input

The first line contains integer a (1 ≤ a ≤ 10^18). The second line contains integer b (1 ≤ b ≤ 1018). Numbers don’t have leading zeroes. It is guaranteed that answer exists.

Output

Print the maximum possible number that is a permutation of digits of a and is not greater than b. The answer can’t have any leading zeroes. It is guaranteed that the answer exists.

The number in the output should have exactly the same length as number a. It should be a permutation of digits of a.

Examples

input

123
222

output

213

input

3921
10000

output

9321

input

4940

5000

output

4940

思路

DFS

代码

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool cmp(const char& a, const char& b) { return a > b; }
bool found, mark;
int cnt[10], ans[100];
string a, b;
void f(int idx)
{
    if (found)
        return;
    if (idx == a.size())
    {
        for (int i = 0; i < (int)a.size(); i++)
            cout << ans[i];
        cout << endl;
        found = true;
    }
    else for (int i = 9; i >= 0; i--)
    {
        if (idx && ans[idx - 1] < b[idx - 1] - '0')
            mark = true;
        if (mark)
        {
            if (cnt[i])
            {
                cnt[i]--;
                ans[idx] = i;
                f(idx + 1);
                cnt[i]++;
            }
        }
        else if (cnt[i] && i <= b[idx] - '0')
        {
            cnt[i]--;
            ans[idx] = i;
            f(idx + 1);
            cnt[i]++;
        }
    }
}
int main()
{
    cin >> a >> b;
    if (a == b)
    {
        cout << a;
        return 0;
    }
    else if (a.size() < b.size())
    {
        sort(a.begin(), a.end(), cmp);
        cout << a << endl;
        return 0;
    }
    else
    {
        for (int i = 0; i < (int)a.size(); i++)
            cnt[a[i] - '0']++;
        f(0);
    }
}
这段代码看起来是用来从`q`张量中提取特征并转换成适合某种代理模型(如注意力机制下的池化)的形式。这里是逐步解释和可能的修改建议: 原始代码含义: 1. `q[:, 1:, :]`: 切片操作,取`q`张量除了第一个时间步的信息(假设`q`是三维,时间步、高度和宽度) 2. `.reshape(b, h, w, c)`: 将切片后的结果重塑为一个四维张量,其中b代表批次数,h、w是高度和宽度,c是通道数 3. `.permute(0, 3, 1, 2)`: 排序张量的维度,将批次和通道放在前面,然后是高度和宽度 4. `.reshape(b, c, -1)`: 再次重塑张量,保持batch和channel维度不变,高度和宽度合并到一个新的轴(-1)上 5. `.permute(0, 2, 1)`: 最后一次调整维度,将新的高度轴移动到最后一位 潜在问题或优化: 1. 如果`h`, `w`或`c`的值很大,连续的操作可能导致内存消耗过大,可以考虑只在必要时再进行reshape,比如在池化之前。 2. 如果`pool`函数对输入有特定的要求,例如接受的是(batch_size, channels, height, width)格式的张量,那么当前的顺序可能会有问题,需要调整reshape顺序。 修改建议示例: ```python # 首先,尝试只在需要时进行重塑 temp_q = q[:, 1:].contiguous() # 使用contiguous()保留连续的内存区域,减少不必要的复制 temp_q = temp_q.view(b, -1, c, h, w) # 如果c<h*w,这一步可能更合适 # 然后,根据pool的需求进行必要的维度变换 if pool_func.supports_input_shape(temp_q.shape): # 如果pool支持这种形状 agent_tokens = pool(temp_q.permute(0, 3, 4, 1, 2)) # 如果需要高度和宽度在前,就按这个顺序 else: agent_tokens = pool(temp_q.permute(0, 2, 3, 4, 1)) # 如果高度和宽度应该在最后 # 最后,再次根据池化后的形状进行整理 agent_tokens = agent_tokens.reshape(b, c, -1).permute(0, 2, 1) ``` 记得检查`pool_func.supports_input_shape()`这部分,看具体的pool函数文档或实现,确保输入张量满足其需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值