查找字符串中不平衡括号的算法
PostScript/PDF 字符串文字由括号包围,并且允许包含未转义的括号只要括号完全平衡。例如,
( () ) % valid string constant
( ( ) % invalid string constant, the inner ( should be escaped
我知道一个算法可以告诉我字符串中是否有不平衡的括号;我正在寻找的是一种算法,该算法将定位一组不平衡的括号,以便我可以在它们前面添加反斜杠,使整个字符串成为有效的字符串文字。更多示例:
( ⟶ \(
() ⟶ ()
(() ⟶ \(() or (\()
()) ⟶ ()\) or (\))
()( ⟶ ()\(
PostScript/PDF string literals are surrounded by parentheses, and are allowed to contain unescaped parentheses as long as the parentheses are fully balanced. So for instance
( () ) % valid string constant
( ( ) % invalid string constant, the inner ( should be escaped
I know an algorithm to tell me if there are any unbalanced parentheses in a string; what I'm looking for is an algorithm that will locate a minimal set of parentheses that are unbalanced, so that I can then stick backslashes in front of them to make the whole a valid string literal. More examples:
( ⟶ \(
() ⟶ ()
(() ⟶ \(() or (\()
()) ⟶ ()\) or (\))
()( ⟶ ()\(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对基于标准堆栈的算法进行修改以检测不平衡括号应该适合您。这是一些伪代码:
希望有帮助。
A modification of the standard stack based algorithm to detect imbalanced parenthesis should work for you. Here's some pseudo code:
Hope this helps.