HDU 3183 a magic lamp (rmq)

Source: Internet
Author: User

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 ;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.