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的更多相关文章

  1. Codeforces 909C - Python Indentation

    909C - Python Indentation 思路:dp. http://www.cnblogs.com/Leohh/p/8135525.html 可以参考一下这个博客,我的dp是反过来的,这样 ...

  2. Codeforces 909C Python Indentation:树状数组优化dp

    题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...

  3. Codeforces 1108D - Diverse Garland - [简单DP]

    题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...

  4. Codeforces - 702A - Maximum Increase - 简单dp

    DP的学习计划,刷 https://codeforces.com/problemset?order=BY_RATING_ASC&tags=dp 遇到了这道题 https://codeforce ...

  5. Codeforces - 1081C - Colorful Bricks - 简单dp - 组合数学

    https://codeforces.com/problemset/problem/1081/C 这道题是不会的,我只会考虑 $k=0$ 和 $k=1$ 的情况. $k=0$ 就是全部同色, $k=1 ...

  6. Codeforces - 474D - Flowers - 构造 - 简单dp

    https://codeforces.com/problemset/problem/474/D 这道题挺好的,思路是这样. 我们要找一个01串,其中0的段要被划分为若干个连续k的0. 我们设想一个长度 ...

  7. CodeForces 30C Shooting Gallery 简单dp

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq574857122/article/details/36944227 题目链接:点击打开链接 给定 ...

  8. Codeforces - 1033C - Permutation Game - 简单dp - 简单数论

    https://codeforces.com/problemset/problem/1033/C 一开始觉得自己的答案会TLE,但是吸取徐州赛区的经验去莽了一发. 其实因为下面这个公式是 $O(nlo ...

  9. CodeForces 22B Bargaining Table 简单DP

    题目很好理解,问你的是在所给的图中周长最长的矩形是多长嗯用坐标(x1, y1, x2, y2)表示一个矩形,暴力图中所有矩形易得递推式:(x1, y1, x2, y2)为矩形的充要条件为: (x1, ...

随机推荐

  1. BUPT复试专题—科学计算器(2009)

    题目描述 给你一个不带括号的表达式,这个表达式只包含加.减.乘.除,请求出这个表 达式的最后结果,最后结果一定是整数: 输入 一个数学表达式,只包括数字,数字保证是非负整数,以及五种运算符 " ...

  2. 【翻译自mos文章】怎么找到OGG Director Server使用的数据库和username?

    APPLIES TO: Management Pack for Oracle GoldenGate - Version: 1.0.0.0 - Release: 1.0 Information in t ...

  3. android-custom-tab-with-viewpager

    https://github.com/eltld/android-custom-tab-with-viewpager

  4. (testng多个class文件执行时混乱,不是等一个class内的所有methods执行完再去执行下一个class内的内容)问题的解决

    问题描述如下: We use TestNG and Selenium WebDriver to test our web application. Now our problem is that we ...

  5. Chrome浏览器 js 关闭窗口失效解决方法

    //获取元素ID var DelHtml = document.getElementById("imgdel"); //alert(DelHtml); //添加点击事件 DelHt ...

  6. Spark 学习笔记:(四)MLlib基础

    MLlib:Machine Learning Library.主要内容包括: 数据类型 统计工具 summary statistics correlations stratified sampling ...

  7. java8--面向对象 下(疯狂java讲义3) 复习笔记

    1.如果一个包装类和一个基本类型比较大小,或者是两个基本类型进行比较大小,直接用==就好: 如果是两个包装类进行比较大小,那么使用equals(),返回值是true,false,或者使用Xxx.com ...

  8. intellij IDEA 更新java后不用重启tomcat

    最近项目大了,每次修改后重启都要等和很久,那个煎熬…… 为了解决这个问题,万能的Google  装了这个  JREBEL 5.63最新的 安装步骤: 一.IDEA在线搜索 jrebel  安装 二.破 ...

  9. bash_action

    https://stackoverflow.com/questions/12076326/how-to-install-maven2-on-redhat-linux #!/bin/bash # Tar ...

  10. idea 破解注册方法总结

    注册码(无期限) JetbrainsCrack-2.6.2.jar适用于ideaIU-2017.2.之前版本,若版本较新适用 JetbrainsCrack-2.6.3_proc.jar. 其中Jetb ...