POJ 1953

题意:求出长度为n的01串中,不出现连续1的串的个数

思路:

dp0[x]表示以0开头长度为x的这类01串的个数,dp1[x]表示以1开头长度为x的这类01串的个数

dp0[x] = dp0[ x-1 ] + dp1[ x-1 ], dp0[1] = 1;

dp1[x] = dp0[ x-1 ] , dp1[1] = 1;

 

总结:要用long long,开始错了两次,应该先测试最大数据再提交的,囧了。0ms

 

#include <iostream> #define F(i,a,b) for (int i=a;i<=b;i++) using namespace std; long long dp0[50], dp1[50]; long long dfs1(int x); long long dfs0(int x) { if (x == 1 ) return 1; if ( dp0[x] ) return dp0[x] ; dp0[x] = dfs0(x-1) + dfs1(x-1); return dp0[x]; } long long dfs1( int x ) { if (x == 1) return 1; if ( dp1[x] ) return dp1[x]; dp1[x] = dfs0(x-1); return dp1[x]; } int main() { int n, T; cin >> T; memset(dp0, 0, sizeof(dp0)); memset(dp1, 0, sizeof(dp1)); F(t,1,T) { cin >> n; cout << "Scenario #" << t << ":/n"; cout << dfs0(n) + dfs1(n) << endl << endl; } return 0; }

你可能感兴趣的:(测试,BI)