首页 > 试题广场 >

交换到最大

[编程题]交换到最大
  • 热度指数:3291 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}给定一个仅由数字 \texttt{0-9} 构成的字符串 s。一次操作可按如下方式进行:
\hspace{23pt}\bullet\,s 中选择既不是最左端字符也不为 \texttt{0} 的某一字符;
\hspace{23pt}\bullet\, 将该字符的数值减少 1
\hspace{23pt}\bullet\, 随后把它与左侧相邻字符交换位置。

\hspace{15pt}例如,字符串 \texttt{ 经过一次操作可以变成 \texttt{\texttt{

\hspace{15pt}你可以不限次数地执行上述操作。请计算能够得到的字典序最大的字符串,并输出该字符串。

输入描述:
\hspace{15pt}第一行输入一个整数 t\left(1\leqq t\leqq 10^4\right),表示测试用例数量。
\hspace{15pt}此后 t 行,每行输入一个不含前导零的数字字符串 s,满足 1\leqq |s|\leqq 2\times 10^5
\hspace{15pt}保证所有测试用例的 |s| 之和不超过 2\times 10^5


输出描述:
\hspace{15pt}对于每个测试用例,在一行上输出通过任意次数操作后能够得到的字典序最大的字符串。
示例1

输入

6
19
1709
11555
51476
9876543210
5891917899

输出

81
6710
33311
55431
9876543210
7875567711

说明

\hspace{15pt}\texttt{ 为例:
\hspace{23pt}\bullet\, \texttt{ \rightarrow \texttt{
\hspace{23pt}\bullet\, \texttt{ \rightarrow \texttt{,得到答案 \texttt{

\hspace{15pt}再以 \texttt{ 为例,可按如下序列操作:
\hspace{23pt}\bullet\, \texttt{ \rightarrow \texttt{
\hspace{23pt}\bullet\, \texttt{ \rightarrow \texttt{
\hspace{23pt}\bullet\, \texttt{ \rightarrow \texttt{,最终得到答案 \texttt{
头像 Silencer76
发表于 2025-08-09 18:03:20
题目链接 交换到最大 题目描述 给定一个仅由数字 0-9 构成的字符串 s。你可以执行无限次如下操作: 选择 s 中一个既不是最左端、也不是 '0' 的字符 s[i]。 将该字符的数值减 1。 将该字符与它左侧的相邻字符交换位置。 目标是求出通过以上操作能够得到的字典序最大的字符串。 解题思路 展开全文
头像 BraveCoder
发表于 2025-09-04 09:53:21
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n 展开全文
头像 我是芭芭拉的狗
发表于 2025-12-28 18:22:49
n = int(input()) for i in range(n): a = list(input()) for j in range(10): for i in range(len(a)-1,0,-1): if int(a[i]) - in 展开全文
头像 归虚梦演
发表于 2025-07-31 20:28:13
#include<bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; cin >> s; int len = s.siz 展开全文
头像 湮雨
发表于 2025-09-22 17:09:32
#include <iostream> using namespace std; int main() { int t; cin>>t; while(t--){ string s; cin>>s; 展开全文
头像 Drink0318
发表于 2026-01-07 09:06:12
import sys n=int(input()) ss=sys.stdin.read().splitlines() for s in ss: s=list(s)#将字符串转换成列表,便于修改 len_s=len(s) res="" for i i 展开全文
头像 何成HN
发表于 2025-10-28 16:21:30
t = int(input()) for _ in range(t): s = list(input()) n = len(s) #从第i未开始固定最有价值的值,任何距离当前位置超过 9 的字符,移动过来的价值都必然是负数 for i in range(n): 展开全文
头像 lahm66
发表于 2025-09-23 21:43:24
贪心 import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner i 展开全文
头像 Drink0318
发表于 2026-01-07 08:55:52
import sys n=int(input()) ss=sys.stdin.read().splitlines() for s in ss: s=list(s)#将字符串转换成列表,便于修改 len_s=len(s) res="" for i i 展开全文
头像 强大的海豚在炒股
发表于 2026-01-13 23:39:24
#include <stdio.h> #include <string.h> #include <stdlib.h> static char res[2000000]; void ChangeID( char* a, char* b) { char te 展开全文