[Section] DP Light OJ 1422 - Halloween Costumes

Light OJ 1422 - Halloween Costumes

When a [st] == ​​a [ed] time, can easily know the middle undressed that only the innermost one, it is easy to obtain dp [st] [ed] = dp [st] [ed - 1]

Otherwise, or the use of breakpoints enumeration mode, i.e. equal to the sum of inter-cell dp [st] [ed] = dp [st] [mid] + dp [mid + 1] [ed]

 

something:

This question is to launch its own, but on a question is to look at the blog. The two channels are quite simple. Sentiment is a sample of dp really want to write about the process, then you will find find a dp equation. It can be said that there is no habit of it, anyway, the future still have to manually dp it.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <limits>
#include <set>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#define INF 0x3f3f3f3f

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 100 + 5;
int N, a[maxN];
int dp[maxN][maxN];

int main()
{
    int TAT; scanf("%d", &TAT);
    for(int Case = 1; Case <= TAT; Case ++ )
    {
        scanf("%d", &N);
        memset(dp, INF, sizeof(dp));
        for(int i = 0; i < N; i ++ )
        {
            scanf("%d", &a[i]);
            dp[i][i] = 1;
        }
        for(int len = 2; len <= N; len ++ )
        {
            for(int st = 0; st < N; st ++ )
            {
                int ed = st + len - 1;
                if(ed >= N) break;
                if(a[st] == a[ed])
                    dp[st][ed] = dp[st][ed - 1];
                else
                {
                    for(int mid = st; mid < ed; mid ++ )
                    {
                        dp[st][ed] = min(dp[st][ed], dp[st][mid] + dp[mid + 1][ed]);
                    }
                }
            }
        }
        printf("Case %d: %d\n", Case, dp[0][N - 1]);
    }
    return 0;
}

 

Published 180 original articles · won praise 54 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44049850/article/details/103863752