【CF5C Longest Regular Bracket Sequence】题解
本文搬运自本人初中博客园博客,若图片加载不出来,可到原文查看:https://www.cnblogs.com/zhangtingxi/p/15783397.html
题目链接
题目
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
思路
拿个栈来维护括号序列,最后剩下在栈里的就是不能合并的。
总结
这道题是道pj-的题,却生生折腾我一个月。
他其实涉及的是一个很经典的括号匹配问题。对于这类问题,这里是其中用栈解决的一大问题。
其实,不仅左括号可以塞进栈里,右括号也可以。
同时,运用栈里剩下不能匹配的,也能求最长匹配的。
Code
1 | // Problem: CF5C Longest Regular Bracket Sequence |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 zhangxixi的博客!





