Codeforces Round 863 A. Insert Digit (1811)

https://codeforces.com/contest/1811/problem/A

以上是本题地址。

You have a positive number of length n 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 76543,and the additional digit is 4.

Then the maximum number you can get is 765443,and it can be obtained in two ways:

by inserting a digit after the 3th or after the 4th digit of the number.

Input

The first line contains a single integer t(1 <= 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 N and D(1 <= n <= 2 * 100000; 0 <= 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 n digits-

the number that you have inintially.

It is guaranteed that the number does not contain leading zeros.

It is guaranteed that the number does not contain leading zeros.

It is guaranteed that the sum of n for all test cases does not exceed 2*100000

*/

/*

Example

Input

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

765443

10

544

6666

613579

987531

98765443210123456789

773737

210000000

70589590

8281271277321

Note:

Also you can input one case and output one result.

根据题意:

将一个数字插入到一个字符串里,需要满足的条件是:使得字符串数字变得最大。

注意:字符串数字的里面的顺序不可以进行移动,只能插入最新的数据。

思路:将数据先插入到头,然后比较和后面的数字,如果大,就不比较了,如果小,就移动这个数据到下一个,循环,直到这个数字>后面的数字,就停止。

在codeforces上测试,AC100

以下是代码:

cpp 复制代码
/*
You have a positive number of length n 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 76543,and the additional digit is 4.
Then the maximum number you can get is 765443,and it can be obtained in two ways:
by inserting a digit after the 3th or after the 4th digit of the number.

Input
The first line contains a single integer t(1 <= 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 N and D(1 <= n <= 2 * 100000; 0 <= 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 n digits-
the number that you have inintially.
It is guaranteed that the number does not contain leading zeros.


It is guaranteed that the number does not contain leading zeros.
It is guaranteed that the sum of n for all test cases does not exceed 2*100000 

*/

/*
Example
Input
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
765443
10
544
6666
613579
987531
98765443210123456789
773737
210000000
70589590
8281271277321

Note:
Also you can input one case and output one result.
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void move_to_good_pos(string &str1, int d)
{
	string a = to_string(d);
	str1 = a+str1;

	int len = str1.length();
	for (int i = 0; i < len-1; i++)
	{
		if (a[0]>str1[i + 1])
		{
			break;
		}

		if (str1[i] < str1[i + 1])
		{
			swap(str1[i], str1[i + 1]);
		}
	}
}
string insert_digit()
{
	string str1;
	int n, d;
	cin >> n >> d;
	getchar();
	getline(cin, str1);
	move_to_good_pos(str1,d);
	return str1;
}
int main()
{
	int t = 0;
	cin >> t;
	int pos = 0;
	vector<string> list;
	string str1="";
	while (t--)
	{
		pos++;
		str1=insert_digit();
		list.push_back(str1);
	}

	for (int i = 0; i < list.size(); i++)
	{
		cout << list[i] << endl;
	}
	return 0;
}
相关推荐
Learn Beyond Limits2 小时前
Using per-item Features|使用每项特征
人工智能·python·神经网络·算法·机器学习·ai·吴恩达
小南家的青蛙3 小时前
LeetCode第51题 - N 皇后
算法·leetcode·职场和发展
文火冰糖的硅基工坊3 小时前
[创业之路-682]:实即虚,虚即实。真正的技术壁垒,藏在光路之外、电路之下、代码之中。
人工智能·算法·系统架构·制造·创业·产业链
2401_841495644 小时前
【计算机视觉】霍夫变换检测
图像处理·人工智能·python·opencv·算法·计算机视觉·霍夫变换
半桶水专家4 小时前
C语言中的setitimer函数详解
c语言·开发语言·算法
-雷阵雨-4 小时前
数据结构——栈和队列(模拟实现)
java·开发语言·数据结构·intellij-idea
FS_tar5 小时前
高斯消元矩阵
c++·算法·矩阵
小蝙蝠侠5 小时前
安德烈·卡帕西:深入探索像ChatGPT这样的大语言模型内容列表
人工智能·算法·机器学习
闻缺陷则喜何志丹6 小时前
【贪心之临项交换】P8732 [蓝桥杯 2020 国 ABC]|普及
c++·算法·蓝桥杯·贪心·洛谷