题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1309
#include <cstdio> #include <iostream> using namespace std; const int MAXN = 200*2+5; int a[MAXN]; int dp[MAXN][MAXN]; int main () { int N, C, K; while(scanf("%d%d%d", &N, &C, &K) != EOF) { for(int i=0; i<N; i++) { scanf("%d", &a[i]); a[N+i] = a[i]; } for(int step=0; step<N; step++) { for(int i=0; i+step<2*N; i++) { int j = i + step; dp[i][j] = dp[i+1][j] + 1; int t = min(j, i+K-1); for(int k=i+1; k<=t; k++) { if(a[k]==a[i]) { dp[i][j] = min(dp[i][j], dp[i+1][k] + dp[k+1][j]); } } } } int ans = N; for(int i=0; i<N; i++) { ans = min(ans, dp[i][i+N-1]); } printf("%d\n", ans); } return 0; }
时间: 2024-10-26 07:20:59