2426: Minimize the number
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
![]() | 1s | 8192K | 1100 | 153 | Standard |
Give you a n-bit (decimal base) number, please remove k bits (decimal base) of the number to get the minimal number according to its original sequence.
Input
There are multiple test cases, first line of each test case is the n-bit number, the next line contains the number k(k <= n).
Output
Print out the minimal number in one line. Don't output the first zeros before the first none-zero number.
Sample Input
178543 2
Sample Output
1543
Problem Source: daoyuanlee
#include<iostream>
//#include<fstream.h>
#include<cmath>
#include<string.h>
using namespace std;
char a[10000];
int main()
{
int i,j;
int icount ,inum;
while(cin>>a>>inum)
{
bool check=true;
icount=strlen(a);
j=0;
for(i=1;i<icount;i++)
{
if(a[i]>=a[j])
{
j++;
a[j]=a[i];
}
else
{
while((a[i]<a[j])&&j>=0&&inum>0)
{
j--;
inum--;
}
j++;
a[j]=a[i];
}
}
if(inum>0)
{
j-=inum;
}
for(i=0;i<=j;i++)
{
if((a[i]=='0')&&check){}
else
{
cout<<a[i];
check=false;
}
}
if(check)
{
cout<<'0';
}
cout<<endl;
}
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n,i,j;
string a;
while(cin>>a>>n)
{
if(a.length()<=n) {printf("/n");continue;}
while(n)
{
int flag=0;
for(i=0;i<a.size();i++)
{
if(a[i]>a[i+1]||i==a.size()-1) {a.erase(a.begin()+i);flag=1;n--;break;}
}
if(!flag) { while(n--) {a.erase(a.end()-1);}break;}
//cout<<a<<" "<<n<<" "<<a.length()<<endl;
}
for(i=0;i<a.size();i++)
{
if(a[i]!='0') break;
}
for(;i<a.size();i++) printf("%c",a[i]);
printf("/n");
}
return 0;
}