OpenJ_Bailian - 4137(模拟,思维)

版权声明:如需转载,私聊博主 https://blog.csdn.net/lylzsx20172018/article/details/91460661

题目链接https://cn.vjudge.net/contest/305866#problem/H
思路:不能对字符串从小到大排序,即不能打破原顺序,删除k个字符后,保证是最小的,即保证字符串是递增的即可,把不是递增的字符删掉,下一位代替。

Sample Input

2
9128456 2
1444 3

Sample Output

12456
1

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
    int T;
    scanf("%d",&T);
    char st[1000];
    int k,len;
    while(T--){
        scanf("%s %d",st,&k);
        len=strlen(st);
        for(int n=0; n<k; n++){
            for(int i=0; i<len-1; i++){
                if(st[i]>st[i+1]){
                    for(int j=i+1; j<len; j++)
                        st[j-1]=st[j];
                }
            }
        }
        for(int i=0;i<len-k;i++)
            printf("%c",st[i]);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lylzsx20172018/article/details/91460661