题意:一个字符串有n个位置每个位置只可能是L或者U,问你在所有可能出现的字符串中最少出现一次三个U连在一起的字符串的个数
题解:首先从左向右枚举每个位置i,保证i,i+1,i+2是U,并且i+2(不包含)前面没有连续三个U连在一起的情况,这样i+2(不包含)后面随便怎么放都可以
接着只需要保证i-1是L并且[1,i-2]没有超过三个U在一起就好了,这儿又使用dp
开二维dp[2][n],第一维表示当前最后一个数是L或者U,第二维表示前j个位置
注意当前最后一个位置i是U时,方程式等于i-1位置是L加上i-2位置是L
不能加上i-1是U再减去i-2是U,因为i-1位置是U没有完全包含i-2位置是U的情况,减多了
#include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<vector> #include<string> #include<cstdio> #include<cstring> #include<iomanip> #include<stdlib.h> #include<iostream> #include<algorithm> using namespace std; #define eps 1E-8 /*注意可能会有输出-0.000*/ #define sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型 #define cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化 #define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0 #define mul(a,b) (a<<b) #define dir(a,b) (a>>b) typedef long long ll; typedef unsigned long long ull; const int Inf=1<<28; const ll INF=1LL<<60; const double Pi=acos(-1.0); const int Mod=1e9+7; const int Max=200010; int dp[2][31]; void DP(int n) { dp[0][0]=1LL; dp[1][0]=0LL; dp[0][1]=dp[1][1]=1LL; dp[0][2]=dp[1][2]=2LL; for(int i=3;i<n;++i) { dp[0][i]=dp[0][i-1]+dp[1][i-1]; dp[1][i]=dp[0][i-1]+dp[0][i-2]; } return; } int Solve(int n) { DP(n); if(n<3) return 0; int ans=(1<<(n-3)); for(int i=4;i<=n;++i) { ans+=(1<<(n-i))*(dp[0][i-4]+dp[1][i-4]); } return ans; } int main() { int n; while(~scanf("%d",&n)&&n) { printf("%d ",Solve(n)); } return 0; }