2019 ICPC H-Parentheses Editor(dp+stack)

2019 ICPC H-Parentheses Editor(dp+stack)铜牌题

You are working with a strange text editor for texts consisting only of open and close parentheses. The editor accepts the following three keys as editing commands to modify the text kept in it.

‘(’ appends an open parenthesis (‘(’) to the end of the text.
‘)’ appends a close parenthesis (‘)’) to the end of the text.
‘-’ removes the last character of the text.
A balanced string is one of the following.

“()”
“(XX)” where XX is a balanced string
“XYXY” where both XX and YY are balanced strings
Initially, the editor keeps an empty text. You are interested in the number of balanced substrings in the text kept in the editor after each of your key command inputs. Note that, for the same balanced substring occurring twice or more, their occurrences should be counted separately. Also note that, when some balanced substrings are inside a balanced substring, both the inner and outer balanced substrings should be counted.

Input
The input consists of a single test case given in a line containing a number of characters, each of which is a command key to the editor, that is, either ‘(’, ‘)’, or ‘-’. The number of characters does not exceed 200 000. They represent a key input sequence to the editor.It is guaranteed that no ‘-’ command comes when the text is empty.

Output
Print the numbers of balanced substrings in the text kept in the editor after each of the key command inputs are applied, each in one line. Thus, the number of output lines should be the same as the number of characters in the input line.

样例输入1

(()())—)

样例输出1

0
0
1
1
3
4
3
1
1
2

样例输入2

()–()()----)(()()))

样例输出2

0
1
0
0
0
1
1
3
1
1
0
0
0
0
0
1
1
3
4

代码如下

#include<iostream>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std;

typedef long long LL; 
const int N=200010;
char s[N];
int f[N];//集合角度上看 分f【i】为以第i个结尾的连续平衡串数。
//状态转移方程为f[cnt]=f[st.top()-1]+1;
int q[N];//用来记录每个“)”已匹配到的左括号的下标。
LL res;//因为答案过大可能会爆int,我们要开long long

stack<int> st;//用栈来进行括号匹配 ,只不过这个栈只记录左括号‘(’。
int main(){
    scanf("%s",s+1);//我们从1开始 方便计算。
    int cnt =0//cnt为当前为第i个括号 ,
    for(int i=1;i<=strlen(s+1);i++)
          if(s[i]=='('){
            cnt++;
            f[cnt]=0;
            q[cnt]=0;
            st.push(cnt);
        }
        else if(s[i]==')'){
            cnt++;
            if(!st.empty()){//栈非空,就pop掉。
                q[cnt]=st.top();
                f[cnt]=f[st.top()-1]+1;
                st.pop();
            }
            else {//栈空说明不匹配,此cnt无连续平衡串
                q[cnt]=0;
                f[cnt]=0;
            }
            res+=f[cnt];//将集合相加。
        }
        else if(s[i]=='-'){
            res-=f[cnt];//减去已去掉的括号下标i所对应的连续平衡串数。
            while(!st.empty()&&st.top()>=cnt)
                st.pop();
                //如果当前cnt是和前面左括号匹配的话,那么之前这个左括号已经被弹出去了, 现在退回来了,那么还得把这个左括号给弄回来
            if(q[cnt]!=0)
                st.push(q[cnt]);
            cnt--;//cnt要回溯。
        }
        cout<<res<<endl;
    
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值