首页 > 试题广场 >

交换到最大

[编程题]交换到最大
  • 热度指数:2426 时间限制: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-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; 展开全文
头像 何成95
发表于 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 展开全文
头像 无限恒晶
发表于 2025-09-12 09:41:15
#include <iostream> using namespace std; string zds(string str); int main() { int num, i = 0; string str[10000]; cin >> num; 展开全文
头像 be_flyling
发表于 2025-11-05 15:04:55
n = int(input()) for x in range(n): a = input() s = [int(i) for i in a] for i in range(1, len(s)): j = i while j >= 1: 展开全文
头像 牛客483531254号
发表于 2025-10-08 20:55:48
#include <any> #include <cstdio> #include <iostream> using namespace std; int main() { int n; cin>>n; getchar(); 展开全文
头像 Jupiter-56
发表于 2025-10-19 17:26:31
// 逻辑严谨 #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; void solve() { 展开全文