http://codeforces.com/contest/1132/problem/F
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a string ss of length nn consisting of lowercase Latin letters. You may apply some operations to this string: in one operation you can delete some contiguous substring of this string, if all letters in the substring you delete are equal. For example, after deleting substring bbbb from string abbbbaccdd we get the string aaccdd.
Calculate the minimum number of operations to delete the whole string ss.
Input
The first line contains one integer nn (1≤n≤5001≤n≤500) — the length of string ss.
The second line contains the string ss (|s|=n|s|=n) consisting of lowercase Latin letters.
Output
Output a single integer — the minimal number of operation to delete string ss.
Examples
input
Copy
5
abaca
output
Copy
3
input
Copy
8
abcddcba
output
Copy
4
一个区间dp的好题呀:
题意很好懂,不太会写
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 505;
int dp[N][N],n;
char s[N];
int main()
{
cin>>n;
scanf("%s",s+1);
for(int i = 1;i <= n; ++i) dp[i][i] = 1;
for(int len = 2; len <= n; ++len)
{
for(int l = 1,r = len;r <= n; ++l,++r)
{
if(s[l] == s[r]) dp[l][r] = dp[l+1][r-1] + 1;
else dp[l][r] = min(dp[l+1][r],dp[l][r-1]) + 1;
for(int k = l;k <= r; ++k)
dp[l][r] = min(dp[l][r],dp[l][k]+dp[k][r]-1);
}
}
cout<<dp[1][n];
return 0;
}