P1106 删数问题

P1106 删数问题

题目

思路

每次删除s[i] > s[i+1]的位置,最后注意去除前导零

代码

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 1e6 + 10;

int a[N], b[N];
char s[N];
int len, k;

void del(int pos) {
	for (int i = pos ; i < len ; i ++) s[i] = s[i + 1];
	len --;
}

int main ()
{
	ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> s >> k;
	len = strlen(s);
	while (k > 0) {
		int i = 0 ;
		while (i < len - 1 && s[i] <= s[i + 1]) i ++; //找到s[i] > s[i+1] 的位置
		del(i); // 删除i
		k --;
	}
	// 去除前导零
	while (s[0] == '0' && len > 1) del(0);
	for (int i = 0 ; i < len ; i ++) cout << s[i] ;

	return 0;
}
posted @ 2022-01-29 11:10  彬腾  阅读(36)  评论(0)    收藏  举报