UESTC Windy Digital DP

Source: Internet
Author: User

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

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.