当前位置: 首页 > news >正文

P4170 学习笔记

省流:区间 DP

题目传送门

这种蓝的区间 DP 还是有亿点点费脑子的,实在不行点开题解深度膜拜一下

正片开始

首先使用区间 DP 的基础操作,设 \(dp[i][j]\) 表示给区间 \([l,r]\) 染色的最小步数,于是答案为

\[dp[0][n - 1] \]

为啥是 \(0\)\(n-1\) 呢?因为我们的字符串是从 \(0\) 开始存的,这里的 \(n\) 表示字符串长度。

你是在看不习惯的话可以存字符数组然后

scanf("%s", s + 1)

或者在字符串前面加上一空格,这里不多赘述。

考虑 DP 的边界。

考虑啥?给 \([i,i]\) 染色不就一步的事吗?于是

for (int i = 0; i < (int)s.size(); i++)dp[i][i] = 1;

考虑 DP 的转移方程。

考虑啥?分类讨论呗。
\(1^\circ\)\(s_l=s_r\),可以在 \([l,r-1]\) 的基础上不改变步数地涂色,于是

\[dp[l][r]=dp[l][r-1] \]

\(2^\circ\)\(s_l \ne s_r\),则 \(\exists x \in [l,r)\),使得不存在左端点 \(l'<=x\) 且右端点 \(r'>x\) 的染色。枚举 \(x\) 即可。

于是乎

\[dp[l][r]=\min_{i=l}^{r} dp[l][i]+dp[i+1][r] \]

这个转移方程也是非常符合区间 DP 的风格。

code
/*********************************************************** Author        : dingziyang888* Website       : https://www.luogu.com.cn/problem/* Created Time  :* FileName      :* Warning!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!* 1.MLE?* 2.array size enough?* 3.long long?* 4.overflow long long?* 5.multiple task cleaned?* 6.freopen?* 7.TLE?* *******************************************************/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <iterator>
#include <map>
#include <unordered_map>
#include <queue>
#include <string>
#include <cstring>
#include <set>
#include <bitset>
#include <unordered_set>
#include <vector>
#include <deque>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <list>
#include <array>
#include <iterator>
#include <cmath>
#include <new>
#include <random>
#include <cfloat>
#include <cstdlib>
#include <climits>
#include <numeric>
#include <complex>
#include <ctime>
#include <chrono>
#include <thread>
#include <mutex>
#include <future>
#include <exception>
#include <stdexcept>
#include <cstdint>
#include <cassert>
#include <stack>
#include <cctype>
#define DEBUG
#define Ofile(s) freopen(s".in", "r", stdin), freopen (s".out", "w", stdout)
#define Cfile(s) fclose(stdin), fclose(stdout)
#define fast ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;using ll = long long;
using ull = unsigned long long;
using lb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;constexpr int mod = 998244353;
constexpr int maxk = 55;int dp[maxk][maxk];string s;int main() {fast;cin >> s;for (int i = 0; i < (int)s.size(); i++)dp[i][i] = 1;for (int l = 1; l < (int)s.size(); l++)for (int i = 0; i + l < (int)s.size(); i++)if (s[i] == s[i + l])dp[i][i + l] = dp[i][i + l - 1];else {dp[i][i + l] = dp[i][i] + dp[i + 1][i + l];for (int j = i + 1; j < i + l; j++)dp[i][i + l] = min(dp[i][i + l], dp[i][j] + dp[j + 1][i + l]);}cout << dp[0][(int)s.size() - 1];return 0;
}
http://www.jsqmd.com/news/371569/

相关文章:

  • 2026年嘎嘎降AI好用吗?对比4款主流降AI工具给你答案
  • Java毕设项目:基于springboot的物流管理系统设计与实现(源码+文档,讲解、调试运行,定制等)
  • Java计算机毕设之基于JAVA的物流管理系统的设计与实现基于springboot的物流管理系统设计与实现(完整前后端代码+说明文档+LW,调试定制等)
  • 2026年毕业答辩前AI率还没降下来?3天应急方案全攻略
  • 【数据分析】灰狼算法优化模糊C-均值聚类分析(GWO-FCM)附Matlab代码
  • 热血江湖2.0完整版源码包:GS服务端+数据库+登录器+百宝阁传送/宝宝挂/武勋居中染色插件+智能假人辅助
  • Java计算机毕设之基于SpringBoot+Vue的旅游信息咨询网站的设计与实现基于springboot的旅游咨询分享平台的设计与实现(完整前后端代码+说明文档+LW,调试定制等)
  • 【图像识别】基于神经网络的字符识别附Matlab代码
  • 【毕业设计】基于springboot的物流管理系统设计与实现(源码+文档+远程调试,全bao定制等)
  • 【计算机毕业设计案例】基于Springboot的物流物流中心信息化管理系统设计与实现基于springboot的物流管理系统设计与实现(程序+文档+讲解+定制)
  • 《企业AI应用商店建设,AI应用架构师的战略思考与布局》
  • AI原生应用的自动化运维:减少人工干预
  • 上海计算机学会YACS月赛题解 | 2026年1月丙组
  • LongCat 发布原生「深度研究」智能体,为你量身定制更可靠的吃喝玩乐攻略
  • AI运维协同:多智能体在测试-开发冲突调解的决策框架
  • 2026年豆包降AI效果怎么样?不如试试这几款专业工具
  • 用例价值评估:聚类算法在测试用例退役决策的支持工具‌
  • 第一次用AI写论文?照着这个流程做AI率低于15%
  • 大数据数据服务中的联邦学习技术应用
  • 开发具有视觉-语言多模态生成能力的AI Agent
  • 科研论文,图片配色到底怎么判断和决定?
  • 提示工程架构师大厂经验:优化提示系统效率的8个秘诀,内部资料首次公开
  • 提示工程架构师实战:如何为医疗Agentic AI系统设计可解释性提示
  • Java毕设项目:基于springboot的旅游咨询分享平台的设计与实现(源码+文档,讲解、调试运行,定制等)
  • 如何用SSH访问远程服务器上的内网服务(如:MySQL、Redis、Kafka)?
  • FastAPI框架在FastGPT二次开发中的应用实践
  • 【毕业设计】基于springboot的旅游咨询分享平台的设计与实现(源码+文档+远程调试,全bao定制等)
  • P4155 学习笔记
  • 《构建之法》第三章读后感
  • 26.2.11