比赛 4043级NOIP2022欢乐赛5th 评测结果 AAAAAAAAAA
题目名称 涂色 最终得分 100
用户昵称 该账号已注销 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2022-11-14 21:51:35
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;

int a[60] = {0};

int f[60][60] = {0};
string s;
int main() {
	freopen("tuse.in", "r", stdin);
	freopen("tuse.out", "w", stdout);
	memset(f, 0x3f, sizeof(f));
	for (int i = 0; i < 55; i++) {
		f[i][i] = 1;
	}
	cin >> s;
	int n = s.length();
	for (int i = 0; i < n; i++) {
		a[i + 1] = s[i] - 'A';
	}
	for (int p = 1; p <= n; p++) {
		for (int i = 1; i + p <= n; i++) {
			int j = i + p;
			if (a[i] == a[j])
				f[i][j] = min(f[i][j], min(f[i + 1][j], f[i][j - 1]));
			else
				for (int k = i; k <= j; k++) {
					f[i][j] = min(f[i][k] + f[k + 1][j], f[i][j]);
				}
		}
	}
	cout << f[1][n] << endl;
	return 0;
}