LightOJ 1422 Halloween Costumes(记忆化搜索)

题意:给你n天分别要穿的衣服,可以套着穿,但是一旦脱下来就不能再穿了,问这n天要准备几件衣服。
    
=================================================================================
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const LL INF = 0xfffffff;
const LL maxn = 200005;
int a[105], dp[105][105];

int DFS(int L,int R)
{
    if(dp[L][R])
        return dp[L][R];
    if(L == R)
        return 1;
    if(L > R)
        return 0;
    dp[L][R] = DFS(L+1, R) + 1;

    for(int k=L+1; k<= R; k++)
    {
        if(a[L] == a[k])
        {
            dp[L][R] = min(dp[L][R], DFS(L,k-1) + DFS(k+1,R) );
        }
    }
    return dp[L][R];
}

int main()
{
    int T, n, cas = 1;
    scanf("%d", &T);

    while(T--)
    {
        scanf("%d", &n);
        memset(dp, 0, sizeof(dp));
        for(int i=1; i<=n; i++)
            scanf("%d", &a[i]);

        printf("Case %d: %d
",cas++, DFS(1, n));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/chenchengxun/p/4836288.html