Title Link: http://acm.uestc.edu.cn/#/problem/show/250
Title Description:
Number of WindyTime limit:3000/1000ms (java/others) Memory limit:65535/65535kb (java/others)SubmitStatus
Windy defines a windy number.
A positive integer that does not contain a leading 0 and the difference of at least 2 of the adjacent two digits is called the windy number.
Windy want to know, in A And B Between, including A And B How many windy are there in total?
Input
Contains two integers, A B 。
Meet 1≤A≤B≤2000000000 .
Outputsample Input and output
| sample Input |
sample Output |
1 |
9 |
Sourcewindy Test Instructions: Title said, test instructions is very clear; thought: This is my study of digital DP, the first full self-thinking + code implementation of the problem; Dp[i][j][z]: The first I-digit, ending with J, Z==1 indicates that a leading is a 0,z==0, leading is not 0 Contains number of windy memory search ~ Of course, the table is OK ~
#include <iostream> #include <stdio.h> #include <string> #include <string.h>using namespace std ; int dp[25][25][3];int bit[25];int dfs (int pos,int pre,int flag,int z) { if (pos==0) return 1; if (!flag && dp[pos][pre][z]!=-1) return dp[pos][pre][z]; int end=flag?bit[pos]:9; int ans=0; for (int i=0;i<=end;i++) { if (z==1 | | pre-i>=2 | | i-pre>=2) ANS+=DFS (pos-1,i,flag&& (i==end), z&& (i==0)); } if (!flag) Dp[pos][pre][z]=ans; return ans;} int cal (int x) {int len=0; memset (bit,0,sizeof (bit)); Memset (Dp,-1,sizeof (DP)); while (x) { bit[++len]=x%10; x/=10; } return Dfs (len,0,1,1);} int main () { int l,r; while (scanf ("%d%d", &l,&r)!=eof) { int s1=cal (r); int s2=cal (L-1); printf ("%d\n", s1-s2); } return 0;}
UESTC Windy Digital DP