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. iOS文件的管理(添加,删除,拷贝,移动)

    #import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super vie ...

  2. Android开发Tips(3)

    欢迎Follow我的GitHub, 关注我的CSDN. 我会介绍关于Android的一些有趣的小知识点. 本文是第三篇, 其余第一篇, 第二篇. imageMogr2/auto-orient/stri ...

  3. 前端编程提高之旅(五)----写给大家看的css书

       自实习也有几个月的时间了,以爱奇艺实习为敲门砖.进入了眼下这家公司.假设说当初能进爱奇艺是暂时袭击DIV+CSS的话,眼下在这家公司体验到.不论什么技术都必须悉知原理,这样才干做到庖丁解牛.做一 ...

  4. vs2010 msvcr100.DLL 丢失!!! 用release 就可以了

  5. 图像物体检測识别中的LBP特征

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xinzhangyanxiang/article/details/37317863 图像物体检測识别中 ...

  6. Linux内核日志开关

    Linux内核日志开关 1.让pr_debug能输出 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -59,7 +59,7 ...

  7. HDU2476 String painter —— 区间DP

    题目链接:https://vjudge.net/problem/HDU-2476 String painter Time Limit: 5000/2000 MS (Java/Others)    Me ...

  8. POJ1094 Sorting It All Out —— 拓扑排序

    题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  9. 织梦CMS被挂马特征汇总

    一.织梦CMS被挂马特征汇总 2013织梦CMS被挂马特征汇总.最近很多朋友反应后台多了几个系统管理员用户:service.spider等,而且自己之前的管理员用户登陆时候会提示用户名不存在.还有朋友 ...

  10. ZOJ 3706 Break Standard Weight 解题报告

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5009 题目意思:给出两个mass:x 和 y,问如何将其中一个 ma ...