UVA 580 Critical Mass (两次dp)
2024-08-24 22:41:03
题意:一个字符串有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=<<;
const ll INF=1LL<<;
const double Pi=acos(-1.0);
const int Mod=1e9+;
const int Max=;
int dp[][];
void DP(int n)
{
dp[][]=1LL;
dp[][]=0LL;
dp[][]=dp[][]=1LL;
dp[][]=dp[][]=2LL;
for(int i=;i<n;++i)
{
dp[][i]=dp[][i-]+dp[][i-];
dp[][i]=dp[][i-]+dp[][i-];
}
return;
}
int Solve(int n)
{
DP(n);
if(n<)
return ;
int ans=(<<(n-));
for(int i=;i<=n;++i)
{
ans+=(<<(n-i))*(dp[][i-]+dp[][i-]);
}
return ans;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
printf("%d\n",Solve(n));
}
return ;
}
最新文章
- 创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople 和AmericanPeople类重写父类的三个方法)。
- 加州大学伯克利分校Stat2.3x Inference 统计推断学习笔记: Section 4 Dependent Samples
- silverlight和wpf中暴露 给子类override
- css标签选择器的优先级
- 期权put和call
- 【Zookeeper】源码分析之Watcher机制(二)
- SpringBoot webmvc项目导出war包并在外部tomcat运行产生的诸多问题以及解决方案
- hihoCoder #1646 : Rikka with String II(容斥原理)
- LeetCode【88. 合并两个有序数组】
- ES脑裂问题
- THML分组元素
- linux环境下Mysql的卸载和重新安装和启动
- ASP.NET WebApi 图片上传
- 使用wget命令爬取整站
- 【IneliJ 】使用IneliJ IDEA 2016将Java Web项目导出为War包
- c# mvc 获取 HtmlHelper 表达式值和时间格式化 去边框
- Oracle sqlloader
- OC基础:getter和setter,@public @protected @private 分类: ios学习 OC 2015-06-15 19:23 22人阅读 评论(0) 收藏
- CH1602 The XOR Largest Pair【Trie树】
- Bookmarklet编写指南
热门文章
- 【BZOJ4810】[Ynoi2017]由乃的玉米田 bitset+莫队
- 巨蟒django之权限10,内容梳理&;&;权限组件应用
- Ubuntu 系统下可以做什么?
- 实用 35 个 jQuery 小技巧
- Vue页面上实时显示当前时间,每秒更新
- Linux中的正则表达式
- HDFS权限管理指南(HDFS Permissions Guide)
- Version 1.5 of the JVM is not suitable for this product.Version:1.6 or greater is required
- Log4j详细配置解释
- text_field text_tag 用法