洛谷P5658 [CSP-S 2019] 括号树一题的题解 注意到有两个fii-1,也就是说他爹必在它前一个位置即这棵树退化成一条链直接暴力枚举所有情况再写一个check函数用来检查子串是否合法。顺带提一嘴检查方法为用一个栈从头到脚依次压入字符当出现“”时弹出栈顶元素看是否匹配。#includebits/stdc.husingnamespacestd;intn,f[500005],ans0;intm;string s;boolcheck(inti,intj){stackcharst;for(intli;lj;l){if(s[l]()st.push(();else{if(st.empty())returnfalse;st.pop();}}returnst.empty();}intmain(){cinn;cins;s s;for(inti1;in;i){cinf[i];//这玩意儿目前还没用}for(inti1;in;i){ans0;for(intl1;ln;l){for(intrl;ri;r)if(check(l,r)){ans;}}m^(i*ans);}coutm;return0;}但是这个方法只能得20分对我来说足够了说明超时了我们应当考虑优化算法。因为树已经退化成了链式结构我们可以想想用dp。咋个用呢如果当前字符是’(直接将序号入栈如果当前字符是 ‘)’1.栈为空说明无法匹配dp[i]02.栈不为空弹出匹配的左括号位置 pos匹配一对 ()同时 pos 左侧连续的合法括号串可以拼接进来。#includebits/stdc.husingnamespacestd;longlongn,f[500005],dp[500005],pos,ans,sum;longlongm;string s;intmain(){cinn;cins;s s;for(longlongi1;in;i){cinf[i];//这玩意儿目前还没用}stacklonglongst;for(longlongi1;in;i){if(s[i](){st.push(i);}else{if(!st.empty()){posst.top();st.pop();dp[i]dp[pos-1]1;}}}for(longlongi1;in;i){sumdp[i];ans^(sum*i);}coutans;return0;}然而还是只有55分。考虑把第一段和第二段结合一下满足链式结构时用dp不满足时用个暴力深搜能多骗一些是一些。#includebits/stdc.husingnamespacestd;intn,m;string s;vectorintG[100005];charval[100005];boolcheck(string t){stackintst;for(intl0;lt.size();l){if(t[l]()st.push(();else{if(st.empty())returnfalse;st.pop();}}returnst.empty();}longlongxdp(){vectorlonglongdp(n1,0);vectorintst;longlongsum0,ans0;for(inti1;in;i){if(s[i-1](){st.push_back(i);dp[i]0;}else{if(!st.empty()){intpostst.back();st.pop_back();dp[i]dp[post-1]1;}else{dp[i]0;}}sumdp[i];ans^(1LL*i*sum);}returnans;}longlongdfs(intu,string path){path.push_back(val[u]);intLpath.size();intk0;for(intl0;lL;l){for(intrl;rL;r){string subpath.substr(l,r-l1);if(check(sub))k;}}longlongans1LL*u*k;for(inti0;iG[u].size();i){intvG[u][i];ans^dfs(v,path);}returnans;}intmain(){cinn;cins;for(inti0;in;i){val[i1]s[i];}boolisftrue;vectorintf(n1);for(inti2;in;i){intx;cinx;f[i]x;if(f[i]!i-1)isffalse;G[f[i]].push_back(i);}longlongans;if(isf){ansxdp();}else{ansdfs(1,);}coutans;return0;}这样就可以再多15分了。但最后还是得写满分代码不然写这题解没意义。可以把dp迁移到树上dp[u]dp[f[m]]1。#includebits/stdc.husingnamespacestd;longlongn,sum0,ans0;string s;vectorlonglongG[500005];longlongf[500005];longlongdp[500005];vectorlonglongst;voiddfs(longlongu){longlongoldsumsum;longlongm-1;if(s[u-1](){st.push_back(u);dp[u]0;}else{if(!st.empty()){mst.back();st.pop_back();dp[u]dp[f[m]]1;}else{dp[u]0;}}sumdp[u];ans^(1LL*u*sum);for(longlongi0;i(longlong)G[u].size();i){dfs(G[u][i]);}sumoldsum;if(s[u-1](){st.pop_back();}else{if(m!-1){st.push_back(m);}}}intmain(){cinn;cins;f[1]0;for(inti2;in;i){cinf[i];G[f[i]].push_back(i);}dfs(1);coutans;return0;}

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/10/1 19:32:24
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/9/30 21:32:07
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/9/30 19:41:56
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/10/1 19:32:23
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/1 19:32:35
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/9/30 21:32:11

日新闻

周新闻

月新闻