[Test room 20191006] brackets sequence

可怜不喜欢括号序列,但是她发现总是有人喜欢出括号序列的题
为了让全世界都能感受到她的痛苦,她想要写-个转换器
它能把普通的小写字符串转换成长度相同的合法的括号序列
在可怜的构思中,这样的转换器需要满足如下两个条件:
1. 结果的括号序列必须要是合法的,即左右括号必须要是相匹配的。
2. 对于一堆相匹配的左右括号,他们所在的位置原来的小写字母必须相同。
举例来说,对于字符串aabaab,()(()) 就是一个合法的答案,而()()()不满足第二个条件,((())不满足第一个条件
可怜发现对于一个小写字符串,有时候有很多满足条件的括号序列,有些时候一个都没有
于是可怜给出了一个小写字符串,她想让你帮她算一下,有多少不同的子串可以转化为满足条件的括号序列

This idea of a little fairy ah ...... question
of violence is to maintain a stack for each letter of
each enumeration backwards, encounter different letter on the stack, if the same letter and the previous one, both pop (both:?)
But such time complexity is \ (\ Theta {(n ^ 2)} \) , so we think optimization

In fact, we only need to open a stack, each to a position after the completion of the operation, the stack of the current record with hash look
the same if you have multiple locations hash value, it shows the letters between these positions are pop-up
instructions to this location is several endpoints, the interval between which must be lawful, and continuous
then we look at all the hash values sort order, see each successive period
if a piece of length len, its contribution to the answer is \ (\ dfrac {len * (len-1 )} {2} \)

And ended

Guess you like

Origin www.cnblogs.com/tqr06/p/11627329.html