传送门:【HDU】3886 Final Kichiku “Lanlanshu”
题目分析:要求满足所给字符串形式起伏的数字的个数。
设dp[cur][pos][j]表示枚举到数的第cur位,字符串下一个可行的起伏是buf[pos],上一个数字为j时的方案数。
注意两点:
1.为了确保每个数只被计算一次,当能进入一个新的起伏时,尽量先进入,如果不能再判断是否符合之前的起伏。
2.前导零不应该被算入起伏中,起伏只能在没有前导零的数中匹配。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )
const int MAXN = 101 ;
const int mod = 100000000 ;
int dp[MAXN][MAXN][10] ;
char s[MAXN] , a[MAXN] , b[MAXN] ;
int digit[MAXN] ;
int m ;
inline void add ( int& x , int y ) {
x += y ;
if ( x >= mod ) x -= mod ;
}
inline bool check ( int x , int y , char c ) {
if ( c == '/' && x < y ) return true ;
if ( c == '-' && x == y ) return true ;
if ( c == '\\' && x > y ) return true ;
return false ;
}
int dfs ( int cur , int pos , int j , int limit , int pre_zero ) {
if ( cur == 0 ) return pos == m ;
if ( !limit && ~dp[cur][pos][j] ) return dp[cur][pos][j] ;
int n = limit ? digit[cur] : 9 , ans = 0 ;
For ( i , 0 , n ) {
if ( pre_zero ) {
add ( ans , dfs ( cur - 1 , pos , i , limit && i == n , pre_zero && i == 0 ) ) ;
} else if ( pos < m && check ( j , i , s[pos] ) ) {
add ( ans , dfs ( cur - 1 , pos + 1 , i , limit && i == n , 0 ) ) ;
} else if ( pos > 0 && check ( j , i , s[pos - 1] ) ) {
add ( ans , dfs ( cur - 1 , pos , i , limit && i == n , 0 ) ) ;
}
}
return limit ? ans : dp[cur][pos][j] = ans ;
}
void debug ( int n1 ) {
rev ( i , n1 , 1 ) printf ( "%d" , digit[i] ) ;
puts ( "" ) ;
return ;
}
int solve ( char a[] , int f ) {
int x = 0 , l = strlen ( a ) ;
while ( a[x] == '0' ) ++ x ;
int n1 = l - x ;
For ( i , 1 , n1 ) digit[i] = a[l - i] - '0' ;
if ( f ) {
For ( i , 1 , n1 ) {
if ( digit[i] ) {
-- digit[i] ;
break ;
} else digit[i] = 9 ;
}
}
//debug ( n1 ) ;
int ans = dfs ( n1 , 0 , 0 , 1 , 1 ) ;
return ans ;
}
int main () {
while ( ~scanf ( "%s%s%s" , s , a , b ) ) {
m = strlen ( s ) ;
clr ( dp , -1 ) ;
printf ( "%08d\n" , ( solve ( b , 0 ) - solve ( a , 1 ) + mod ) % mod ) ;
}
return 0 ;
}