zjgsu609

我删我删,删删删

Time Limit
1s
Memory Limit
64KB
Judge Program
Standard
Ratio(Solve/Submit)
22.73%(5/22)
Description:

有一个大整数.不超过1000位.假设有N位.我想删掉其中的任意S个数字.使得删除S位后,剩下位组成的数是最小的.

Input:

有多组数据数据,每组数据为两行.第一行是一个大整数.第二行是个整数S,其中S小于大整数的位数. 输入以EOF结束。

Output:

对于每组输入数据,请输出其删除后的最小数.

Sample Input:
178543
4
100002
1
Sample Output:
13
2

"12321"这种,每次从ts[0]开始遍历,相邻的高位优先删除,如果遇到相等或者小于的,调整mmin的大小,继续遍历,
比如第一次删去'2'(ts[3]),后面的字符串依次前移 第二次删去'2'(原来的ts[4])
特殊的,如果遇到后面字符串全部相等,比如"5555",那么只要len--,就可以了,不需要操作删去最后一位的操作
//比较相邻的元素,高位大的优先减小
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>

const int inf = (1<<31)-1;
const int MAXN = 1e3+10;

using namespace std;

char ts[MAXN];


int main()
{
    char mmin;
    int n,len,i;
    while(scanf("%s%d",ts,&n)!=EOF){
        len = strlen(ts);
        while(n--){
            mmin = ts[0];
            for(i=1;i<len;i++){
                if(mmin>ts[i]){
                    for(int j=i-1;j<len-1;j++){ //5555 因为减去最后一个,所以这种只要长度减少就可以了
                        ts[j] = ts[j+1];
                    }
                    break;
                }else{
                    mmin = ts[i];
                }
            }

            len--;
        }
        int flag= 0;
        for(i=0;i<len;i++){
            if(ts[i]=='0'&&!flag)continue;
            else{ printf("%c",ts[i]);flag = 1;}
        }
        if(!flag)cout<<0;
        cout<<endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}
View Code


在一个谎言的国度,沉默就是英雄
原文地址:https://www.cnblogs.com/EdsonLin/p/5442542.html