UVA 580 Critical Mass (两次dp)

题意:一个字符串有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\n",Solve(n));
    }
return 0;
}
复制代码

 

posted @   专注如一  阅读(258)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示