P1106 删数问题
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x * f;
}
string str;
int k;
int cnt = -1, len = 0;
char c[300];
int main() {
cin >> str >> k;
int i = 0;
while (str[i] == '0') {
i++;
}
for (; i < str.size(); ++i) {
while (len < k && str[i] < c[cnt] && cnt != -1) {
cnt--, len++;
}
c[++cnt] = str[i];
}
i = 0;
while (c[i] == '0') {
i++;
}
int flag = 0;
for (; i < str.size() - k; ++i) {
cout << c[i];
flag = 1;
}
if (flag == 0)
cout << "0" << endl;
return 0;
}
#include <stdio.h>
#include <string.h>
char c[260];
int main(){
int len,i,j,s;
scanf("%s%d",c,&s);
len=strlen(c);
while(s--){
for(i=0;i<=len-2;i++)
if(c[i]>c[i+1]){
for(j=i;j<=len-2;j++)
c[j]=c[j+1];
break;
}
len--;//此处位置写错,之前写在if内部
}
i=0;
while(i<=len-1&&c[i]=='0')i++;//处理前导0
if(i==len)printf("0");
else
for(j=i;j<=len-1;j++)
printf("%c",c[j]);
return 0;
}
题解:一个规则,就是当前数位要不要删除,取决于下标为i的数字有没有比下标为i-1的数字小,如果是,就把下表为i-1的数字删去,如此往复,得到了一个前半段不下降的子序列,k如果大一点的话就能得到一个全段不下降子序列。要留意的点就是如果你删除完后得到的结果有前导零,也得删除,但是当结果就是一个0的时候就不要删前导零了,会把结果删没了。

644

被折叠的 条评论
为什么被折叠?



