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
解题思路:
cnt 记录当前第几个字符 碰到 ‘-’ 会回退
dp[cnt] 表示当前 连续匹配的类似()()的数量
match[cnt] 表示与当前括号匹配的左括号的id
那么每一次的ans 就是 ans += dp[cnt] 秒啊!
那么当碰到左括号时dp[cnt] = 0 ; match[cnt] = 0; st.push(cnt);
当碰到右括号时就可以进行转移了 dp[cnt] = dp[st.top-1]+1;
代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
#define pii map<int,int>
#define mk make_pair
#define rtl rt<<1
#define rtr rt<<1|1
#define int long long
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
int ret = 0, sgn = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')
sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
ret = ret*10 + ch - '0';
ch = getchar();
}
return ret*sgn;
}
inline void Out(int a){if(a>9) Out(a/10);putchar(a%10+'0');}
int qpow(int m, int k, int mod){int res=1,t=m;while(k){if(k&1)res=res*t%mod;t=t*t%mod;k>>=1;}return res;}
ll gcd(ll a,ll b){return b==0?a : gcd(b,a%b);}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
ll inv(ll x,ll m){return qpow(x,m-2,m)%m;}
const int N = 2e6+10;
stack<int> st;
char s[N];
int dp[N];
int match[N];
signed main()
{
scanf("%s",s+1);
int len=strlen(s+1);
int cnt=0;
ll ans=0;
for(int i=1;i<=len;i++){
if(s[i]=='('){
cnt++;
dp[cnt]=0;
match[cnt]=0;
st.push(cnt);
}
else if(s[i]==')'){
cnt++;
if(!st.empty()){
match[cnt]=st.top();
dp[cnt]=dp[st.top()-1]+1;
st.pop();
}
else {
match[cnt]=0;
dp[cnt]=0;
}
ans+=dp[cnt];
}
else if(s[i]=='-'){
ans-=dp[cnt];
while(!st.empty()&&st.top()>=cnt)
st.pop();
// 这里是因为 如果当前cnt是和前面左括号匹配的话,那么之前这个左括号已经被弹出去了, 现在退回来了,那么还得把这个左括号给弄回来
if(match[cnt]!=0)
st.push(match[cnt]);
cnt--;
}
printf("%lld\n",ans);
}
}