A Magic Lamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2563 Accepted Submission(s): 1001
The question is: give you an integer, you are allowed to delete exactly m digits. The left digits will form a new integer. You should make it minimum.
You are not allowed to change the order of the digits. Now can you help Kiki to realize her dream?
Each test case will contain an integer you are given (which may at most contains 1000 digits.) and the integer m (if the integer contains n digits, m will not bigger then n). The given integer will not contain leading zero.
If the result contains leading zero, ignore it.
178543 4 1000001 1 100001 2 12345 2 54321 2
13 1 0 123 321#include<cstdio> #include<cstring> #include<cmath>
char s[1010]; char ans[1020]; int st[1010][20];
int Min(int x,int y) { return s[x] <= s[y] ? x : y; }
void RMQ_Init(int len) { for(int i = 0; i < len; i++) st[i][0] = i; for(int j = 1; (1<<j) < len; j++) for(int i = 0; i+(1<<j)-1 < len;i++) st[i][j] = Min(st[i][j-1],st[i+(1<<(j-1))][j-1]); }
int Query(int l,int r) { int k = (int)(log((double)(r-l+1))/log(2.0)); return Min(st[l][k],st[r-(1<<k)+1][k]); }
int main() { int len, m, i; while(scanf("%s%d",s, &m)!=EOF) { //memset(ans,0,sizeof(ans)); /// memset(s,0,sizeof(s)); // memset(st,0,sizeof(st)); len=strlen(s); RMQ_Init(len); int n=len-m; int pos=0; int num=0; while(n--) { pos=Query(pos,m++); ans[num++]=s[pos++]; } i=0; while(ans[i]=='0'&&i<num) i++; if(i==num) { printf("0"); } else { for(;i<num;i++) printf("%c",ans[i]); } printf("\n"); } return 0; }