Lightoj 1205-palindromic numbers (Digital DP)

Source: Internet
Author: User

Lightoj 1205-palindromic numbers (Digital DP)

ACM

Address: spoj myq10 mirror number

Question:
Evaluate the number of input records in [a, B.

Analysis:
It is a simple version of spoj myq01... Actually there are non-recursive methods.

Code:

/** Author: zhangz <iw.zen [at] gmail.com> * blog: http://blog.csdn.net/hcbbt* file: 1205. CPP * Create Date: 16:21:04 * descripton: digit DP, palindrome */# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; # define repf (I, a, B) for (INT I = (a); I <= (B); I ++) typedef long ll; const int n = 20; int T, Len, REC [N], bit [N]; ll a, B, DP [N] [N] [2]; ll DFS (INT St Art, int cur, bool Ismir, bool limit) {If (cur <0) return Ismir; If (! Limit & DP [start] [cur] [Ismir]! =-1) return DP [start] [cur] [Ismir]; int mMax = limit? Bit [cur]: 9; ll ret = 0; repf (I, 0, mMax) {rec [cur] = I; if (START = cur & I = 0) {// If have lead zeroret + = DFS (START-1, cur-1, Ismir, (Limit & (I = mMax);} else if (Ismir & cur <(start + 1)/2) {// If ismirror and can judgeret + = DFS (START, cur-1, (I = rec [start-cur]), (Limit & (I = mMax);} else {RET + = DFS (START, cur-1, Ismir, (Limit & (I = mMax) ;}} return limit? RET: DP [start] [cur] [Ismir] = ret;} ll calc (ll n) {Len = 0; while (N) {bit [Len ++] = n % 10; N/= 10;} bit [Len] = 0; return DFS (LEN-1, len-1, 1, 1);} bool check () {repf (I, 0, Len/2-1) {If (bit [I]! = Bit [Len-1-I]) return false;} return true;} int main () {memset (DP,-1, sizeof (DP )); scanf ("% d", & T); int CAS = 1; while (t --) {scanf ("% LLD", & A, & B ); if (A <B) Swap (a, B); printf ("case % d: % LLD \ n", CAS ++, Calc (a)-Calc (B) + check ();} 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.