hdoj 3555 Bomb(DFA+dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555

思路分析:该问题要求求解1—N中的数中含有49的数的个数,可以使用DFA来递推dp公式;详细解释点击链接查看;

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; const int MAX_N = + ;
long long dp[MAX_N][];
int digit[MAX_N]; int NextState(int cur_state, int next_char)
{
if (cur_state == )
{
if (next_char == )
cur_state++;
}
else if (cur_state == )
{
if (next_char == )
++cur_state;
else if (next_char != )
--cur_state;
}
return cur_state;
} int main()
{
int case_times;
long long n; scanf("%d", &case_times);
while (case_times--)
{
int cur_state = , count = ;
int len = ;
long long ans = , temp_value = ; scanf("%I64d", &n);
temp_value = ++n;
while (temp_value)
{
digit[++len] = temp_value % ;
temp_value /= ;
} for (int i = len; i >= ; -- i)
{
++count;
for (int j = ; j < digit[i]; ++ j)
{
memset(dp, , sizeof(dp));
dp[count][NextState(cur_state, j)] = ;
for (int k = count + ; k <= len; ++ k)
{
dp[k][] = * dp[k-][] + * dp[k-][];
dp[k][] = dp[k-][] + dp[k-][];
dp[k][] = dp[k-][] + * dp[k-][];
}
ans += dp[len][];
}
cur_state = NextState(cur_state, digit[i]);
}
printf("%I64d\n", ans);
}
return ;
}
上一篇:snmp 简单网管协议


下一篇:Linux echo, sort, sed 等一些命令总结