首页 > 其他 > 详细

CodeForces-1132F Clear the String

时间:2019-03-08 13:05:47      阅读:195      评论:0      收藏:0      [点我收藏+]

题目链接

https://vjudge.net/problem/CodeForces-1132F

题面

Description

You are given a string \(s\) of length \(n\) 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 \(s\).

Input

The first line contains one integer \(n\) (\(1 \le n \le 500\)) — the length of string \(s\).

The second line contains the string \(s\) (\(|s| = n\)) consisting of lowercase Latin letters.

Output

Output a single integer — the minimal number of operation to delete string \(s\).

Examples

Input

5
abaca

Output

3

Input

8
abcddcba

Output

4

题解

区间dp,设\(f[l][r]\)是消除\(l...r\)区间所花的最小次数,对于每次更新:

  1. 如果\(s[l] == s[r]\), \(f[l][r] = f[l+1][r-1]+1\)
  2. 如果\(s[l] != s[r], f[l][r] = min(f[l + 1][r], f[l][r-1]) + 1\)
  3. 枚举中间点k,\(f[l][r] = min(f[l][r], f[l][k] + f[k][r] -1)\),这样的话k这个点删了两次,所以要减一,因为\(l, k, r\)可能被同时删去,所以要这样转移

AC代码

#include <bits/stdc++.h>
#define N 505
using namespace std;
int max(int a, int b) {
    return a > b ? a : b;
}
int min(int a, int b) {
    return a < b ? a : b;
}
int f[N][N];
char s[N];
int main() {
    int n;
    scanf("%d", &n);
    scanf("%s", s + 1);
    for (int i = 1; i <= n; i++) {
        f[i][i] = 1;
    }
    for (int len = 2; len <= n; len++) {
        for (int l = 1; l + len - 1 <= n; l++) {
            int r = l + len - 1;
            if (s[l] == s[r]) f[l][r] = f[l + 1][r - 1] + 1;
            else f[l][r] = min(f[l + 1][r], f[l][r - 1]) + 1;
            for (int k = l; k <= r; k++) {
                f[l][r] = min(f[l][r], f[l][k] + f[k][r] - 1);
            }
        }
    }
    printf("%d\n", f[1][n]);
    return 0;
}

DP还是很玄学的啊...

CodeForces-1132F Clear the String

原文:https://www.cnblogs.com/artoriax/p/10495095.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!