Insert Digit插入数字

time limit per test

2 seconds

memory limit per test

256 megabytes

You have a positive number of length nn and one additional digit.

You can insert this digit anywhere in the number, including at the beginning or at the end.

Your task is to make the result as large as possible.

For example, you have the number 7654376543, and the additional digit is 44. Then the maximum number you can get is 765443765443, and it can be obtained in two ways --- by inserting a digit after the 33th or after the 44th digit of the number.

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) --- the number of test cases.

The descriptions of the test cases follow.

The first line of the description of each test case contains two integers nn and dd (1≤n≤2⋅1051≤n≤2⋅105; 0≤d≤90≤d≤9) --- the length of the number and an additional digit, respectively.

The second line of the description of each test case contains a string consisting of nn digits --- the number that you have initially. It is guaranteed that the number does not contain leading zeros.

It is guaranteed that the sum of nn for all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output a string consisting of n+1n+1 digits --- the maximum possible number that can be obtained.

Example

Input

Copy

复制代码

11

5 4

76543

1 0

1

2 5

44

3 6

666

5 6

13579

5 8

97531

19 4

9876543210123456789

5 7

73737

8 1

20000000

7 0

7058959

12 1

828127127732

Output

Copy

复制代码
765443
10
544
6666
613579
987531
98765443210123456789
773737
210000000
70589590
8281271277321

每个测试的时间限制2秒
每个测试的内存限制256兆字节
你有一个长度为n的正数
和一个附加数字。

你可以在数字的任何位置插入这个数字,包括开头或结尾。

你的任务是使结果尽可能大。

例如,你有数字76543
,附加数字是4
。那么你可以得到的最大数字是765443
,它可以通过两种方式获得——在数字的第3位或第4位后插入一位数字。

输入
第一行包含一个整数t
(1≤t≤104
)——测试用例的数量。

测试用例的描述如下。

每个测试用例描述的第一行包含两个整数 n
和 d
(1≤n≤2⋅105
; 0≤d≤9
) — 分别表示数字的长度和附加数字。

每个测试用例描述的第二行包含一个由 n
位数字组成的字符串 — 您最初拥有的数字。保证该数字不包含前导零。

保证所有测试用例的 n
之和不超过 2⋅105
。

输出
对于每个测试用例,输出一个由 n+1
位数字组成的字符串 — 可以获得的最大可能数字。

示例
输入副本
11
5 4
76543
1 0
1
2 5
44
3 6
666
5 6
13579
5 8
97531
19 4
9876543210123456789
5 7
73737
8 1
20000000
7 0
7058959
12 1
828127127732
输出副本
765443
10
544
6666
613579
987531
98765443210123456789
773737
210000000
70589590
8281271277321 

代码:

cpp 复制代码
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void solve() {
    int t;
    cin >> t;
    
    while (t--) {
        int n, d;
        cin >> n >> d;
        string s;
        cin >> s;

        string result;
        bool inserted = false;

        for (char c : s) {
            if (!inserted && d > (c - '0')) {
                result += (d + '0');
                inserted = true;
            }
            result += c;
        }

        if (!inserted) {
            result += (d + '0');
        }

        cout << result << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
相关推荐
J先生x4 分钟前
【IP101】图像处理进阶:从直方图均衡化到伽马变换,全面掌握图像增强技术
图像处理·人工智能·学习·算法·计算机视觉
爱coding的橙子2 小时前
每日算法刷题 Day3 5.11:leetcode数组2道题,用时1h(有点慢)
算法·leetcode
虾球xz4 小时前
游戏引擎学习第268天:合并调试链表与分组
c++·学习·链表·游戏引擎
fpcc5 小时前
跟我学c++高级篇——模板元编程之十三处理逻辑
c++
格林威5 小时前
Baumer工业相机堡盟工业相机的工业视觉中为什么偏爱“黑白相机”
开发语言·c++·人工智能·数码相机·计算机视觉
Dream it possible!6 小时前
LeetCode 热题 100_只出现一次的数字(96_136_简单_C++)(哈希表;哈希集合;排序+遍历;位运算)
c++·leetcode·位运算·哈希表·哈希集合
?abc!7 小时前
缓存(5):常见 缓存数据淘汰算法/缓存清空策略
java·算法·缓存
BioRunYiXue7 小时前
一文了解氨基酸的分类、代谢和应用
人工智能·深度学习·算法·机器学习·分类·数据挖掘·代谢组学
Dddle18 小时前
C++:this指针
java·c语言·开发语言·c++
不見星空8 小时前
2025年第十六届蓝桥杯软件赛省赛C/C++大学A组个人解题
c语言·c++·蓝桥杯