zoukankan      html  css  js  c++  java
  • Codeforces909C Python Indentation(动态规划)

    http://codeforces.com/problemset/problem/909/C

    dp[i][j]表示第i行缩进j的方案数。

    当第i-1行为f时,无论当前行是f或s都必须缩进,即dp[i][j+1]=dp[i-1][j];

    当第i-1行为s时,无论当前行时f或s,缩进j的方案数为第i-1行缩进j~n的方案数。

    最后注意%1e+7

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cstdlib>
     6 #include<cmath>
     7 #include<vector>
     8 #include<stack>
     9 #include<queue>
    10 #define lson l, m, rt<<1
    11 #define rson m+1, r, rt<<1|1
    12 #define IO ios::sync_with_stdio(false);cin.tie(0);
    13 #define INF 0x3f3f3f3f
    14 #define MAXN 500010
    15 const int MOD=1e9+7;
    16 typedef long long ll;
    17 using namespace std;
    18 char c[5010];
    19 int dp[5010][5010], n;
    20 int main()
    21 {
    22     while(cin >> n){
    23         int sum, flag=0;
    24         memset(dp, 0, sizeof(dp));
    25         for(int i = 0; i < n; i++){
    26             cin >> c[i];
    27         }
    28         dp[0][0] = 1;//第i行缩进j的方案数 
    29         for(int i = 1; i < n; i++){
    30             if(c[i-1] == 'f'){
    31                 for(int j = 0; j < n; j++){
    32                     dp[i][j+1] = dp[i-1][j]%MOD;
    33                 } 
    34             }
    35             else{//s
    36                 sum=0;
    37                 for(int j = n-1; j >= 0; j--){//<=
    38                     sum = (sum+dp[i-1][j])%MOD;
    39                     dp[i][j] = sum;
    40                 }
    41             }
    42         }
    43         sum=0;
    44         for(int j = 0; j < n; j++){
    45             sum = (sum+dp[n-1][j])%MOD;
    46         }
    47         cout << sum << endl;
    48     }
    49     return 0;
    50 } 
  • 相关阅读:
    jQuery的动画效果
    jQuery的event事件
    设计模式 命令行模式
    桥接模式
    享元模式
    代理模式
    门面模式
    代理模式
    python基础-abstractmethod、__属性、property、setter、deleter、classmethod、staticmethod
    库存负数
  • 原文地址:https://www.cnblogs.com/Surprisezang/p/8727107.html
Copyright © 2011-2022 走看看