CF 607B - Zuma(区间 dp 记忆化搜索写法)

记搜大法好

Zuma

题面翻译

题目描述

Genos最近在他的手机上下载了祖玛游戏。在祖玛游戏里,存在n个一行的宝石,第i个宝石的颜色是Ci 。这个游戏的目标是尽快的消灭一行中所有的宝石。 在一秒钟,Genos能很快的挑选出这些有颜色的宝石中的一个回文的,连续的子串,并将这个子串移除。每当一个子串被删除后,剩余的宝石将连接在一起,形成一个新的行列。你的任务是:求出把整个宝石串都移除的最短时间。 让我们给你一个提示:如果一个串正着读或倒着读都一样,那么这个串(或子串)叫回文串。在我们这道题中,“回文”指这个宝石串中的第一个珠子的颜色等于最后一个珠子的颜色,第二个珠子的颜色等于倒数第二个珠子的颜色,等等。
输入输出格式
输入格式:

第一行包含一个整数n(1<=n<=500) ——宝石串的长度。 第二行包含n个被空格分开的整数,第i(1<=i<=n) 个表示这行中第i个珠子的颜色。
输出格式:

输出一个整数,把这行珠子移除的最短时间。 (样例略)
说明:

在第一个例子中,Genos可以在一秒钟就把这行珠子全部移走。 在第二个例子中,Genos一次只能移走一个珠子,所以移走三个珠子花费他三秒。 在第三个例子中,为了达到2秒的最快时间,先移除回文串4 4,再移除回文串1 2 3 2 1。

感谢@Administrator2004 提供的翻译

题目描述

Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color c[i] . The goal of the game is to destroy all the gemstones in the line as quickly as possible.

In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?

Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.

输入格式

The first line of input contains a single integer n ( 1<=n<=500 ) — the number of gemstones.

The second line contains n space-separated integers, the i-th of which is c[i] ( 1<=c[i]<=n ) — the color of the i-th gemstone in a line.

输出格式

Print a single integer — the minimum number of seconds needed to destroy the entire line.

样例 #1

样例输入 #1

3
1 2 1

样例输出 #1

1

样例 #2

样例输入 #2

3
1 2 3

样例输出 #2

3

样例 #3

样例输入 #3

7
1 4 4 2 3 2 1

样例输出 #3

2

提示

In the first sample, Genos can destroy the entire line in one second.

In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.

In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1.

题意:

给定一个长度最多为 500 的串,每次可以删掉其中的一串回文串,消耗时间为 1s,问删掉整个串最少需要多少秒?一个字符也算一个回文串。

思路:

四种情况:

  1. l = r,直接 return 1;(边界情况)

  2. [l, r] 是回文的,return 1;

  3. s[l] == s[r],转移表示为 dp[l][r] = dp[l + 1][r - 1];(这是因为我们可以在消除 [l + 1, r - 1] 的最后一次操作时顺手把 l、r 两端消掉)

  4. 一般情况:枚举每个中间点 mid,取分成的两个区间代价和的最小值。

要注意的是,情况 3 不能直接返回,要和情况 4 联合起来取 min 值。

时间复杂度: O ( n 2 ) O(n ^ 2) O(n2)

代码:

#include<bits/stdc++.h>

using namespace std;
const int N = 510, inf = 510;
int memo[N][N];
int n, a[N];

int dfs(int l, int r)
{
	int& res = memo[l][r];
	if (l == r) return res = 1;	//情况 1 作为边界情况要加在判断记忆化数组是否有值之前,防止越界
	if (res != inf) return res;	//答案被计算过,直接返回,记搜核心
	//判断当前区间是否是回文串
	bool ok = false;
	int idx = 0;
	for (int i = l; i <= l + r >> 1; ++i) {
		if (a[i] != a[r - idx]) {
			ok = true;
		}
		++idx;
	}
	if (!ok) return res = 1;	//情况 2
	if (a[l] == a[r]) res = dfs(l + 1, r - 1);	//情况 3
	for (int i = l; i < r; ++i) {	//情况 4
		int mid = i;	//枚举分割点
		res = min(res, dfs(l, mid) + dfs(mid + 1, r));
	}
	//注意 res 是情况 34 联合取 min 的结果
	return res;
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			memo[i][j] = inf;
		}
	}
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
	int ans = dfs(1, n);
	cout << ans << '\n';

	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是祖玛游戏的代码,使用C++实现: ```c++ #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> using namespace std; int n, ans; // n表示小球的个数,ans表示最少需要几次操作 char s[200]; int find(int l, int r) // 找到最长的一段相同颜色的小球 { int i = l, j = r; while (i < j) { while (i < j && s[i] == s[i+1]) i++; while (i < j && s[j] == s[j-1]) j--; if (i < j && s[i] == s[j]) i++, j--; else break; } return i; } void solve() { ans = n; for (int i = 0; i < n; i++) { int j = find(i, n-1); if (j < n-1) // 如果有可以消除的小球 { int k; for (k = j+1; k < n; k++) if (s[k] != s[j]) break; // 找到第一个与s[j]颜色不同的小球 for (int l = k-j; l >= 0; l--) { // 枚举将s[j]和后面的l个小球消除 int start = max(0, i-l); // left int end = min(n-1, j+1); // right for (int r = start; r <= end; r++) { int t = find(start, end); char c = s[t]; int len = t - r + 1; for (int p = t+1; p < n; p++) s[p-len] = s[p]; // 将后面的小球向前移len个位置 n -= len; ans = min(ans, l+1 + solve()); // 递归 n += len; for (int p = n-1; p >= t+1-len; p--) s[p+len] = s[p]; // 将小球复原 for (int p = t-l+1; p <= t; p++) s[p] = c; } } break; } } } int main() { while (cin >> s && s[0] != 'E') { n = strlen(s); solve(); cout << ans << endl; } return 0; } ``` 该代码使用了分治法,对于每个区间,找到其中最长的一段相同颜色的小球,然后枚举将这一段小球和之后的小球一起消除的情况,递归求解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值