Codeforces - 909C - Python Indentation - 简单dp
http://codeforces.com/problemset/problem/909/C
好像以前做过,但是当时没做出来,看了题解也不太懂。
一开始以为只有上面的for有了循环体,这里的statement就可以随便放,但其实并不是这样,statement的位置会取消比它缩进更多或相等的for的可append性。所以设计dp顺序的时候要用dp[i][j]表示第i行剩余可选for循环数量为j的选法数。那么递推公式也不难(才怪),看看代码就好了。
有几个可以优化的,一个是滚动数组,另一个是线性求前缀和然后均摊O(1)减少一个维度的时间复杂度。
注意读入%c之前要加一个空格来过滤前面还保留在流中的换行符。
#include<bits/stdc++.h>
using namespace std;
#define ll long long ll dp[][];
char ch[];
int n;
int p=; int main(){
scanf("%d",&n); for(int i=;i<n;i++){
scanf(" %c",&ch[i]);
//printf("%c\n",ch[i]);
} int cntf=;
if(ch[]=='f')
cntf++;
dp[][cntf]=;
//可跟随的f的数量
/*for(int j=0;j<=cntf;j++){
printf("dp[%d][%d]=%lld\n",0,j,dp[0][j]);
}
printf("\n");*/ for(int i=;i<n;i++){
if(ch[i]=='f'){
cntf++;
if(i->=&&ch[i-]=='f'){
//dp[i]=dp[i-1];
//不得不跟随
for(int j=;j<=cntf;j++){
dp[i][j]=dp[i-][j-];
}
}
else if(i->=&&ch[i-]=='s'){
//任选一个f进行跟随
ll sum=;
for(int j=;j<=cntf-;j++){
sum=(sum+dp[i-][j])%p;
} ll presum=;
for(int j=;j<=cntf;j++){
dp[i][j]=(sum-presum+p)%p;
presum=(presum+dp[i-][j-])%p;
}
}
}
else{
if(i->=&&ch[i-]=='f'){
//dp[i]=dp[i-1];
//不得不跟随
for(int j=;j<=cntf;j++){
dp[i][j]=dp[i-][j];
}
}
else if(i->=&&ch[i-]=='s'){
//任选一个f进行跟随
ll sum=;
for(int j=;j<=cntf;j++){
sum=(sum+dp[i-][j])%p;
} ll presum=;
for(int j=;j<=cntf;j++){
dp[i][j]=(sum-presum+p)%p;
presum=(presum+dp[i-][j])%p;
}
}
}
/*for(int j=0;j<=cntf;j++){
printf("dp[%d][%d]=%lld\n",i,j,dp[i][j]);
}
printf("\n");*/
} ll sum=;
for(int j=;j<=cntf;j++){
sum=(sum+dp[n-][j])%p;
}
printf("%lld\n",sum); }
---恢复内容结束---
Codeforces - 909C - Python Indentation - 简单dp的更多相关文章
- Codeforces 909C - Python Indentation
909C - Python Indentation 思路:dp. http://www.cnblogs.com/Leohh/p/8135525.html 可以参考一下这个博客,我的dp是反过来的,这样 ...
- Codeforces 909C Python Indentation:树状数组优化dp
题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...
- Codeforces 1108D - Diverse Garland - [简单DP]
题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...
- Codeforces - 702A - Maximum Increase - 简单dp
DP的学习计划,刷 https://codeforces.com/problemset?order=BY_RATING_ASC&tags=dp 遇到了这道题 https://codeforce ...
- Codeforces - 1081C - Colorful Bricks - 简单dp - 组合数学
https://codeforces.com/problemset/problem/1081/C 这道题是不会的,我只会考虑 $k=0$ 和 $k=1$ 的情况. $k=0$ 就是全部同色, $k=1 ...
- Codeforces - 474D - Flowers - 构造 - 简单dp
https://codeforces.com/problemset/problem/474/D 这道题挺好的,思路是这样. 我们要找一个01串,其中0的段要被划分为若干个连续k的0. 我们设想一个长度 ...
- CodeForces 30C Shooting Gallery 简单dp
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq574857122/article/details/36944227 题目链接:点击打开链接 给定 ...
- Codeforces - 1033C - Permutation Game - 简单dp - 简单数论
https://codeforces.com/problemset/problem/1033/C 一开始觉得自己的答案会TLE,但是吸取徐州赛区的经验去莽了一发. 其实因为下面这个公式是 $O(nlo ...
- CodeForces 22B Bargaining Table 简单DP
题目很好理解,问你的是在所给的图中周长最长的矩形是多长嗯用坐标(x1, y1, x2, y2)表示一个矩形,暴力图中所有矩形易得递推式:(x1, y1, x2, y2)为矩形的充要条件为: (x1, ...
随机推荐
- expect实现无交互操作
按两下tab linux总共2000个命令,,常用的200个命令. 只要文件改变了,MD5值就会变!
- LeetCode ||& Word Break && Word Break II(转)——动态规划
一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...
- saltstack源码安装
环境 centos6.3,python2.7.5. 1.install libzmq-master $ git clone git://github.com/zeromq/libzmq.git $ c ...
- Seesion和Cookie详解2
转载来自: https://www.toutiao.com/a6693986851193094664/?tt_from=weixin&utm_campaign=client_share& ...
- VC断点失败的原因之中的一个
VC断点失败的原因之中的一个 flyfish 2014-10-23 情景 再debug状态下仅仅有一个cpp文件.命中不了断点. 提示 能够 同意源码与原始版本号不同 不採用,防止出现未知的隐患 问题 ...
- linux centos7 安装常用软件java,node,mysql,Seafile
linux centos7 安装常用软件java,node,mysql,Seafile 安装压缩解压缩软件 yum install -y unzip zip 安装git yum install -y ...
- 2016/06/16 phpexcel
程序部分 require_once './phpexcel/PHPExcel.php'; // 首先创建一个新的对象 PHPExcel object $objPHPExcel = new ...
- Spring源码深度解析——笔记
1.spring容器的基本用法 xml配置 <bean id="myTestBean" class="bean.MyTestBean"/> 调用 B ...
- 【bzoj3210】花神的浇花集会
将(x,y)转化成(x+y,x-y)可以将切比雪夫距离转化成曼哈顿距离(自己推一推) A.B的切比雪夫距离就是A‘.B‘曼哈顿距离的一半. 那么可以将x.y分离处理,排序中位数即可. 注意如果最后选的 ...
- file类简单操作
file类可表示文件或文件夹 import java.io.File; import java.io.FilenameFilter; import java.io.IOException; impor ...