CodeForces-1132F Clear the String


題目鏈接

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還是很玄學的啊...


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM