Educational Codeforces Round 61 (Rated for Div. 2) F题解

 

                                               F

 

 

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.d

 

题解: 区间dp,对于d[i][j]考察第j个字符,若它是单独删除的则d[i][j]=d[i][j-1],否则从i——j-1中考察与第j个字符相等的字符,假设j和第k个字符一起被删除,那么应该先删除k+1——j-1中的所有字符,然后再讲第k个字符和第j个字符合并成一个,状态转移方程为 d[i][j]=min{d[i][k]+d[k+1][j-1]}(s[j]==s[k])

 

写在后面:

其实当时是想过dp的但是当时没有想到合并的想法(一开就把相同的字符合并了却没有想到合并的想法,太专注于找回文字串了吧)cf上这种字符串操作的题好像套路就是先想模拟贪心(简单题),再dp。。。。

 

 

 

 

 

 

你可能感兴趣的:(dp)