Parentheses Sequence微软编程笔试
描述
You are given a sequence S of parentheses. You are asked to insert into S as few parentheses as possible so that the resulting sequence T is well matched.
It's not difficult. But can you tell how many different T you can get?
Assume S = ()), you can get either (()) or ()().
输入
One line contains the sequence S.
For 30% of the data, 1 ≤ |S| ≤ 10
For 60% of the data, 1 ≤ |S| ≤ 200
For 100% of the data, 1 ≤ |S| ≤ 1000
输出
Output 2 integers indicating the minimum number of parentheses need to be inserted into S and the number of different T. The number of different T may be very large, so output the answer modulo 109+7.
())
1 2
只考虑左边(比右边)多的情况,也就是消掉匹配的()之后的比如(((((()消掉匹配的后只有(((((的情况
现在考虑如下"("比")"多 ((()()( => ((( 那么第一个(和第二个(之间最多只能有1个),至少0个 那么第二个(和第三个(之间最多只能有2个),至少0个 那么第三个(和第四个(之间最多只能有3个),至少1个 那么第四个(和第五个(之间最多只能有3个),至少1个
dp[i][j]:到第i+1个"("为止,有j个")"的方案数 k表示当前第i个"("和第i+1个"("之间有k个")" dp[i][j] = sum(dp[i-1][j-k]) 那么))))())))())))*(((((()(((()((,*号左边全是消不掉的),右边全是消不掉(,右边也就是上面讨论的情况,左边翻转一下也就是上面讨论情况,ans = ansl*ansr%MOD;