1笨办法
#include<iostream>
using namespace std;
const int N=200010;
int a[N],b[N];
int main()
{
int T;
cin >> T;
while(T--){
int n, d;
cin >> n >> d;
string s;
int cnt = 0;
cin >> s;
for (int i = 0; i < n; i++){
a[cnt ++] = s[i] - '0';
}
int z=0;
bool flag = false;
for(int i=0;i<n;i++) {
if(d>a[i]) {
z=i;
flag = true;
break;
}
}
if (!flag)z = n;
for(int i=0;i<z;i++){
b[i] =a[i];
}
b[z]=d;
for(int i=z+1;i<=n;i++){
b[i]=a[i-1];
}
for(int i=0;i<=n;i++) cout << b[i];
cout << endl;
}
return 0;
}
2简便方法
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 100010;
ll a[N], b[N];
int main()
{
ll t;
cin >> t;
while (t --){
ll n, d;
string a;
cin >> n >> d;
cin >> a;
ll cnt = 0;
for (int i = 0; i < n; i ++){
ll t = a[i] - '0';
if (d > t){
b[cnt] = d;
d = -1;
i --;
}
else b[cnt] = t;
cnt ++;
}
if (d >= 0) b[cnt ++] = d;
for (int i = 0; i < n + 1; i ++)cout << b[i];
puts("");
}
return 0;
}