AcWing 5067. 删除数字(递归)
原题链接
简单
作者:
涩谷辣妹
,
2024-09-13 21:51:18
,
所有人可见
,
阅读 10
C++ 代码
#include<bits/stdc++.h>
using namespace std;
string solution(string s, long long n) {
if (s.length() == 0) return "";
if (n == 0) return s;
if (s.length() == 1 && n == 1) return "";
int len = s.length();
int minNum = INT_MAX;
//前n+1个数
for (int i = 0; i < n + 1; i++) {
minNum = min(minNum, s[i] - '0');
}
//minNum前的都删掉
int p = 0;
while (s[p] - '0' > minNum) {
p++;
n--;
}
//截取
s = s.substr(p, len - p + 1);
//删了
if (s.length() < len) return solution(s, n);
//没删
else {
char c = s[0];
s = s.substr(1, len-1);
return (c + solution(s, n));
}
}
int main()
{
int n;
string s;
cin >> s >> n;
//从n+1里面选个最小的,前面的都删掉
cout << solution(s, n);
}