当前位置: 首页 > news >正文

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

以下是代码:

/*
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
828127127732Output
765443
10
544
6666
613579
987531
98765443210123456789
773737
210000000
70589590
8281271277321Note:
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;
}

http://www.dtcms.com/a/438378.html

相关文章:

  • 企业网站建立要做的准备更新电脑版wordpress
  • 阳江做网站公司南昌seo优化公司
  • 光子桥揭开可调激光PIC的面纱
  • C++——类和对象3
  • Linux第二十四讲:多路转接epoll
  • 专业网站定制流程网站建设公司是怎么找客户
  • Unity学习之C#的反射机制
  • Python环境管理工具全景对比:Virtualenv, Pipenv, Poetry 与 Conda
  • 郑州企业如何建网站wordpress微信付费
  • 微信小程序入门学习教程,从入门到精通,微信小程序开发进阶(7)
  • 数据结构和算法篇--带哨兵节点的双链表
  • 6黄页网站建设做网站怎么去工信部缴费
  • 三支一扶面试资料
  • pytorch 52 基于SVD从全量训练模型中提取lora模型
  • Process Monitor 学习笔记(5.7):长时间运行追踪与日志体积控制
  • 深入解析需求变更:从本质认知到实践指南
  • 商城网站建设的步骤网络设计教程
  • Day 30 - 错误、异常与 JSON 数据 - Python学习笔记
  • 吴恩达机器学习笔记(10)—支持向量机
  • 电商网站建设与运行xd网页设计教程
  • 基于websocket的多用户网页五子棋(四)
  • 深入浅出 C++20 协程
  • 想做个小网站怎么做主机壳 安装wordpress
  • 永兴县网站建设专业山东省城乡建设厅官网
  • ip prefix-list(IP前缀列表)概念及题目
  • [工作流节点9] 删除记录节点的风险与使用规范 —— 明道云工作流数据清理实战指南
  • 做网站推广的销售怎么打电话如何做网站发产品销售
  • MongoDB GEO 项目场景 ms-scope 实战
  • 医美三方网站怎么做网站外链建设可以提升网站
  • 在算法比赛中高效处理多行输入