[CQOI2007]涂色PAINT
思路
显然我们可以考虑用dpdpdp来求解问题,碰到那种一眼没思路的题稳是dpdpdp没跑了,那么我们就往dpdpdp方面去考虑吧。
我们定义dp[i][j]dp[i][j]dp[i][j],表示把[i,j][i, j][i,j]这个区间涂上颜色要用多少步,显然有dp[i][j]=1,i==jdp[i][j] = 1, i == jdp[i][j]=1,i==j,
接下来我们考虑如何使这个状态进行转移,当有两个邻近的颜色使一样的时候,我们可以把它们当成一种颜色一起涂色,所以当str[i]==str[i+1]str[i] == str[i + 1]str[i]==str[i+1]时,显然有dp[i][i]=dp[i][i+1]dp[i][i] = dp[i][i + 1]dp[i][i]=dp[i][i+1],同样的这个性质可以拓展到整条链上,当str[i]==str[j]str[i] == str[j]str[i]==str[j],我们一定有dp[i][j]=min(dp[i+1][j],dp[i][j−1])dp[i][j] = min(dp[i + 1][j], dp[i][j - 1])dp[i][j]=min(dp[i+1][j],dp[i][j−1])。
这里我们已经把大多的情况给考虑完了,还剩下一种str[i]!=str[j]str[i] != str[j]str[i]!=str[j],这个时候我们显然要把这个区域分成两份来进行涂色,这个时候我们就可以枚举端点k∈[l,r]k \in [l, r]k∈[l,r],然后取这些断点和的最小值就行。
代码
/*Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define endl '\n'using namespace std;typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x;
}void print(ll x) {if(x < 10) {putchar(x + 48);return ;}print(x / 10);putchar(x % 10 + 48);
}const int N = 55;char str[N];int dp[N][N], n;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);cin >> str + 1;n = strlen(str + 1);memset(dp, 0x3f, sizeof dp);for(int len = 1; len <= n; len++) {for(int l = 1; l + len - 1 <= n; l++) {int r = l + len - 1;if(len == 1) {dp[l][r] = 1;}else if(str[l] == str[r]) {dp[l][r] = min(dp[l + 1][r], dp[l][r - 1]);}else {for(int k = l; k < r; k++) {dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]);}}}}cout << dp[1][n] << endl;return 0;
}