windy数,数位dp

中文题面:http://www.lydsy.com/JudgeOnline/problem.php?id=1026

用着这样的数位dp姿势搞下,就好了。

#include <cstdio>

#include <cstring>

#include <algorithm>

#include <iostream>

#include <string>

#include <vector>

#include <cmath>

#include <queue>

#include <map>

#include <set>

using namespace std;

#define INF 1000000000





int dp[20][20][2];



int pos[20];

int judge(int x, int y)

{

	if (abs(x - y) >= 2) return 1;

	return 0;

}



int nong(int x, int last, int first, int flag)

{

	int ans = 0;

	if (x == 0) return 1;

	if (!flag&&~dp[x][last][first]) return dp[x][last][first];

	int bound = flag ? pos[x] : 9;

	for (int i = 0; i <= bound; i++){

		if (first == 0){

			ans += nong(x - 1, i, first || i, flag&&i == bound);

		}

		else{

			if (judge(i, last)) ans += nong(x - 1, i, first || i, flag&&i == bound);

		}

	}

	if (!flag) dp[x][last][first] = ans;

	return ans;

}



int gao(int x)

{

	int len = 0;

	while (x){

		pos[++len] = x % 10;

		x /= 10;

	}

	return nong(len, 0, 0, 1);

}





int main()

{

	int A, B;

	memset(dp, -1, sizeof(dp));

	while (cin >> A >> B){

		cout << gao(B) - gao(A - 1) << endl;

	}

	return 0;

}

  

你可能感兴趣的:(dp)