Codeforces Round #455 (Div. 2) C. Python Indentation DP Recursion

Source: Internet
Author: User



Codeforces Round #455 (Div. 2)



C. Python Indentation



Test instructions: In Python, given n for loops or statements, ' F ' must have a statement. Combine the python indentation into a legitimate program and ask how many possible scenarios.



Tags:dp



DP[I][J] Indicates the number of possible scenarios at which the I-statement indent is J, and the transfer:



1 "If the first I is ' f ', then the first i+1 must be indented one unit more than the first, i.e. dp[i+1][j] = Dp[i][j].



2 "If the first I is ' s ', then the first i+1 can belong to any of the preceding for loop, that is, the indentation of the i+1 to <= the indent of the first I, that is dp[i+1][j] = Dp[i][k], j<=k<=n.



Complexity O (n^2).


#include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i) #define mes(a,b)  memset(a,b,sizeof(a)) #define INF 0x3f3f3f3f #define MP make_pair #define PB push_back #define fi  first #define se  second typedef long long ll; const int N = 5005, mod = 1e9+7; int n;
ll  dp[N][N]; char ch; int main()
{
    scanf("%d", &n);
    dp[1][0] = 1;
    rep(j,1,n) dp[1][j]=0;
    rep(i,1,n-1)
    {
        scanf("%*c%c", &ch); if(ch==‘f‘)
        {
            dp[i+1][0] = 0;
            rep(j,0,n) {
                dp[i+1][j+1] = dp[i][j];
            }
        } else {
            ll  sum = 0;
            per(j,n,0) {
                sum += dp[i][j];
                sum %= mod;
                dp[i+1][j] = sum;
            }

        }
    }
    scanf("%*c%c", &ch);
    ll  ans = 0;
    rep(j,0,n)
        ans += dp[n][j],  ans %= mod;
    printf("%lld\n", ans); return 0;
}


Codeforces Round #455 (Div. 2) C. Python Indentation DP recursion


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.