Halloween Costumes Topic Links:
http://lightoj.com/volume_showproblem.php?problem=1422
Test instructions
Gappu want to go to some party, he went to each one to put a specific number of clothes on the outside, he can put on or take off the clothing (take off the clothing can not be worn again, but may wear a new dress of the same number, the recent clothing will be put on the outside of the clothing before), Ask Gappu at least how many sets of clothing to prepare.
Exercises
Set DP[I][J] for the interval I to J (set Len to interval length, j=i+len) The minimum number of clothing required, then you can find Dp[i][j] can be obtained in two ways:
①dp[i][j]=dp[i][j-1]+1
② find a point k,k in the interval [i][j-1] and party K needs costumes and party J needs the same costumes, then Dp[i][j]=dp[i][k]+dp[k+1][j-1] (k+1 is to ensure that the party K required to take off the garment)
Take ① and ② smaller values, dp[1][n] as the end state
Code
#include <stdio.h>
const int n=101;
int p[n],dp[n][n];
int mmin (int x,int y)
{
Return x<y?x:y;
}
void Solve ()
{
int t,n,case=0;
scanf ("%d", &t);
while (t--)
{
scanf ("%d", &n);
for (int i=1;i<=n;++i)
{
scanf ("%d", &p[i]);
Dp[i][i]=1;
}
for (int len=1;len<n;++len)
for (int i=1;i+len<=n;++i)
{
int J=i+len;
dp[i][j]=dp[i][j-1]+1;
for (int k=i;k<=j-1;++k)
if (P[k]==p[j])
{
Dp[i][j]=mmin (Dp[i][j],dp[i][k]+dp[k+1][j-1]);
}
}
printf ("Case%d:%d\n", ++case,dp[1][n]);
}
}
int main ()
{
Solve ();
return 0;
}
Light OJ 1422 Halloween Costumes interval DP basic problem