Delete M bits from a given number and find the minimum value of the remaining number.
Analysis: rmq is used. Assume that the length of the original number is N, and each time a minimum value is obtained from a range, N-M is used.
View code
# Include <iostream>
# Include <algorithm>
# Include < String . H>
# Include <math. h>
Using Namespace STD;
Int N, DP [ 1010 ] [ 20 ];
Char B [1010 ];
Void Init_rmq ()
{
Memset (DP, 0 , Sizeof (DP ));
For ( Int I = 0 ; I <n; I ++)
DP [I] [ 0 ] = I;
For ( Int J = 1 ; J <log ((Double ) (N)/log ( 2.0 ); J ++)
{
Int Limit = N + 1 -( 1 <J );
For ( Int I = 0 ; I <limit; I ++)
{
Int X = DP [I] [J- 1 ];
Int Y = DP [I + (1 <(J- 1 )] [J- 1 ];
DP [I] [J] = B [x] <= B [y]? X: Y;
}
}
}
Int Rmq ( Int A, Int B)
{
Int K = ( Int ) (Log (( Double ) (B-A + 1 )/Log (2.0 ));
Int X = DP [a] [k];
Int Y = DP [B + 1 -( 1 <K)] [k];
Return B [x] <= B [y]? X: Y;
}
Int Main ()
{
Int M, L, R;
Char Ans [ 1000 ];
While (Scanf ( " % S % d " , B, & M) = 2 )
{
N = strlen (B );
Init_rmq ();
M = N-m;
L = 0 ;
R = N-m;
Int T = m, len1 = 0 ;
While (T --)
{
L = rmq (L, R );
R ++;
Ans [len1 ++] = B [l];
L ++;
// Returns the left and right intervals of each value.
}
Ans [len1] = ' \ 0 ' ;
Int Flag = 1 ;
For ( Int I =0 ; I <len1; I ++)
{
If (Flag & Ans [I] = ' 0 ' )
Continue ;
Flag = 0 ;
Printf ( " % C " , ANS [I]);
}
If (FLAG)
Puts ( " 0 " );
Else Puts ( "" );
}
Return 0 ;
}