hdu3183A Magic Lamp 贪心有关问题

hdu3183A Magic Lamp 贪心问题
//贪心问题,只需要保证在前面的数大于后面的数时
//删除前面的数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
const int maxn = 1010;
stack<int> sta;
int main()
{
    int ans[maxn];
    string str;
    int k;int i;
    while(cin>>str>>k)
    {
       sta.push(str[0]-'0');
       for(i=1;i<str.size();i++)
       {
           while(k&&sta.size()&&(sta.top()>str[i]-'0'))
           {
                sta.pop();
                k--;
           }
           sta.push(str[i]-'0');
       }
       while(k--)
       sta.pop();
       int j=0;
       while(sta.size())
       {
           ans[j++] = sta.top();
           sta.pop();
       }
       for(i = j-1; !ans[i] ;i--);
       if(i<0) printf("0");
       for(; i >= 0; i--)
       printf("%d" , ans[i]);
       puts("");
    }
    return 0;
}