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

题解:P15520 [CCC 2016 J3] Hidden Palindrome

介绍三种做法。

设字符串长度为 \(n\)

(一)暴力枚举左右端点,再判断中间是否是回文串,可以得到 \(O(n^3)\) 解法,足以通过本题。

(二)动态规划,设 \(f_{l,r}\) 表示子串 \([l,r]\) 是不是回文串,则初值为 \(f_{i,i}=1\)\(f_{i,i+1}=[s_i=s_{i+1}]\)\([\textrm{cond}]\) 是艾弗森括号,若 \(\textrm{cond}\) 为真,其值为 \(1\),否则为 \(0\))。对于 \(r-l+1\ge 3\),有 \(f_{l,r}=[s_l=s_r]\land f_{l+1,r-1}\)。DP 出来之后求 \(r-l+1\) 的最大值,使得 \(f_{l,r}=1\) 即可。时间复杂度 \(O(n^2)\)

(三)使用 Manacher 算法求出以每个位置为中心的回文串长度,取最大值即可,时间复杂度 \(O(n)\)。鉴于此做法难度显著高于本题要求,Manacher 略过不讲,有兴趣的读者可以前往 P3805 【模板】Manacher 学习。

代码是第二种做法:

// Problem: P15520 [CCC 2016 J3] Hidden Palindrome
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P15520
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x, y, z) for(int x = (y); x <= (z); ++x)
#define per(x, y, z) for(int x = (y); x >= (z); --x)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do {freopen(s".in", "r", stdin); freopen(s".out", "w", stdout);} while(false)
#define endl '\n'
using namespace std;
typedef long long ll;mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
int randint(int L, int R) {uniform_int_distribution<int> dist(L, R);return dist(rnd);
}template<typename T> void chkmin(T& x, T y) {if(y < x) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}template<int mod>
inline unsigned int down(unsigned int x) {return x >= mod ? x - mod : x;
}template<int mod>
struct Modint {unsigned int x;Modint() = default;Modint(unsigned int x) : x(x) {}friend istream& operator>>(istream& in, Modint& a) {return in >> a.x;}friend ostream& operator<<(ostream& out, Modint a) {return out << a.x;}friend Modint operator+(Modint a, Modint b) {return down<mod>(a.x + b.x);}friend Modint operator-(Modint a, Modint b) {return down<mod>(a.x - b.x + mod);}friend Modint operator*(Modint a, Modint b) {return 1ULL * a.x * b.x % mod;}friend Modint operator/(Modint a, Modint b) {return a * ~b;}friend Modint operator^(Modint a, int b) {Modint ans = 1; for(; b; b >>= 1, a *= a) if(b & 1) ans *= a; return ans;}friend Modint operator~(Modint a) {return a ^ (mod - 2);}friend Modint operator-(Modint a) {return down<mod>(mod - a.x);}friend Modint& operator+=(Modint& a, Modint b) {return a = a + b;}friend Modint& operator-=(Modint& a, Modint b) {return a = a - b;}friend Modint& operator*=(Modint& a, Modint b) {return a = a * b;}friend Modint& operator/=(Modint& a, Modint b) {return a = a / b;}friend Modint& operator^=(Modint& a, int b) {return a = a ^ b;}friend Modint& operator++(Modint& a) {return a += 1;}friend Modint operator++(Modint& a, int) {Modint x = a; a += 1; return x;}friend Modint& operator--(Modint& a) {return a -= 1;}friend Modint operator--(Modint& a, int) {Modint x = a; a -= 1; return x;}friend bool operator==(Modint a, Modint b) {return a.x == b.x;}friend bool operator!=(Modint a, Modint b) {return !(a == b);}
};const int N = 45;int n, f[N][N], ans;
string s;int main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);cin >> s;n = s.size();s = " " + s + " ";rep(i, 1, n) f[i][i] = 1;rep(i, 1, n - 1) if(s[i] == s[i + 1]) f[i][i + 1] = 1;rep(len, 3, n) {rep(l, 1, n - len + 1) {int r = l + len - 1;if(s[l] == s[r]) f[l][r] = f[l + 1][r - 1];}}rep(l, 1, n) rep(r, l, n) if(f[l][r]) chkmax(ans, r - l + 1);cout << ans << endl;return 0;
}
http://www.jsqmd.com/news/424618/

相关文章:

  • 设备预测性维护的投入产出与成本效益深度解析
  • 2026年湖北开放大学全省四级体系办学,能为江浙粤等地提供公平学历提升吗 - 工业设备
  • 设备预测性维护的成本与效益分析
  • 2026年评价高的上海厂房维修/安徽厂房维修热门推荐 - 行业平台推荐
  • 2026年国密门禁前端识别设备主要包括三大主流形态:多功能复合型国密门禁读卡器、人脸识别国密门禁读卡器和国密指纹门禁读卡器。它们共同构成了一个从基础到高端、从单一到融合的完整产品矩阵,以满足不同场景
  • 真的太省时间!千笔·专业学术智能体,继续教育论文写作神器
  • 3分钟搞懂深度学习AI:一条切片面包看懂AI张量
  • 生活困境 --- 为什么简单的含义要用不知所云的术语表示
  • 市面上回收快的沃尔玛购物卡回收平台推荐 - 京顺回收
  • PyTorch MPS 加速完全教程:在 Apple Silicon Mac 上玩转深度学习
  • 2026大吨位气动葫芦工厂市场份额排行,知名大厂占比高,3吨气动葫芦/8吨气动葫芦/HQ气动葫芦,大吨位气动葫芦产品排行 - 品牌推荐师
  • 专科生也能用!万众偏爱的AI论文工具 —— 千笔写作工具
  • 2026年口碑好的模压桥架/电缆桥架厂家选择参考建议 - 行业平台推荐
  • 论文写不动?一键生成论文工具 千笔写作工具 VS PaperRed 更贴合专科生需求!
  • 2026年诚信的昆山千灯注册公司/昆山注册公司可靠企业榜 - 行业平台推荐
  • 权值计算
  • 三大搜索引擎 URL 推送 API 详解:百度、必应、谷歌
  • 学长亲荐!专科生专属AI论文写作神器 —— 千笔
  • 2月热门!受欢迎的成都火锅品牌评测,地摊火锅/社区火锅/牛肉火锅/火锅/重庆火锅/美食/成都火锅,成都火锅品牌排行榜单 - 品牌推荐师
  • 2026年质量好的涂料膨润土/河北涂料膨润土生产商哪家强 - 行业平台推荐
  • 用过才敢说 8个降AIGC平台测评:专科生降AI率必备工具全解析
  • 分析2026年技术强的RGV平板车加工厂,新乡兰灵机械优势明显 - 工业品网
  • 2026年知名的昆山财务代理记账公司/昆山代理注册记账公司方案选择推荐 - 行业平台推荐
  • 2026年评价高的非营业性爆破资质代办/一级爆破资质代办服务机构哪家靠谱怎么选 - 行业平台推荐
  • 聊聊河南尚泰净化板,好用的防火洁净板,推荐哪家? - 工业设备
  • 【飞书_获取日程冲突的技术支持名单_使用查询日程视图接口】
  • YOLOv13涨点改进| TGRS 2026 |独家创新首发、特征融合改进篇| 引入SCFM 语义引导跨注意力融合模块,使高层语义信息与低层细节信息更加协调,适合小目标检测、红外小目标检测、小目标分割
  • 2026年河南新乡RGV平板车年度排名,配套服务完善的厂家推荐 - 工业品网
  • 2026年洁净板老牌厂家推荐,尚泰净化板性价比高值得拥有 - mypinpai
  • 《创业之路》-878-华为管理哲学的核心:“中学为体,西学为用”。“形”是西方的(流程、制度、架构),“神”是东方的(价值观、动力机制、组织氛围)