本文共 4285 字,大约阅读时间需要 14 分钟。
The development of a text editor is a hard problem. You need to implement an extra module for brackets coloring in text.
Your editor consists of a line with infinite length and cursor, which points to the current character. Please note that it points to only one of the characters (and not between a pair of characters). Thus, it points to an index character. The user can move the cursor left or right one position. If the cursor is already at the first (leftmost) position, then it does not move left.
Initially, the cursor is in the first (leftmost) character.
Also, the user can write a letter or brackets (either (, or )) to the position that the cursor is currently pointing at. A new character always overwrites the old value at that position.
Your editor must check, whether the current line is the correct text. Text is correct if the brackets in them form the correct bracket sequence.
Formally, correct text (CT) must satisfy the following rules:
any line without brackets is CT (the line can contain whitespaces);
If the first character of the string — is (, the last — is ), and all the rest form a CT, then the whole line is a CT; two consecutively written CT is also CT. Examples of correct texts: hello(codeforces), round, ((i)(write))edi(tor)s, ( me). Examples of incorrect texts: hello)oops(, round), ((me).The user uses special commands to work with your editor. Each command has its symbol, which must be written to execute this command.
The correspondence of commands and characters is as follows:
any lowercase Latin letter or bracket (( or )) — write the entered character to the position where the cursor is now.
For a complete understanding, take a look at the first example and its illustrations in the note below.You are given a string containing the characters that the user entered. For the brackets coloring module’s work, after each command you need to:
If two pairs of brackets are nested (the first in the second or vice versa), then these pairs of brackets
should be painted in different colors. If two pairs of brackets are not nested, then they can be painted in different or the same colors. For example, for the bracket sequence ()(())()() the least number of colors is 2, and for the bracket sequence (()(()())())(()) — is 3.
Write a program that prints the minimal number of colors after processing each command.
Input
The first line contains an integer n (1≤n≤106) — the number of commands.
The second line contains s — a sequence of commands. The string s consists of n characters. It is guaranteed that all characters in a string are valid commands.
Output
In a single line print n integers, where the i-th number is:
Sample Input
11
(RaRbR)L)L(Sample Output
-1 -1 -1 -1 -1 -1 1 1 -1 -1 2
解题思路:
通过维护区间最小值和左右括号是否相等来判断该文本是否符合规定。通过区间最大值来得到匹配的括号层数。
AC代码:
#includeusing namespace std;#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)#define endl '\n'#define lson root<<1#define rson root<<1|1const int MAXN = 1e6+5;int add[MAXN<<2],sl[MAXN<<2],sr[MAXN<<2];void push_up(int root){ sl[root]=min(sl[lson],sl[rson]); sr[root]=max(sr[lson],sr[rson]);}void push_down(int root){ if(add[root]) { sl[lson]+=add[root]; sl[rson]+=add[root]; sr[lson]+=add[root]; sr[rson]+=add[root]; add[lson]+=add[root]; add[rson]+=add[root]; add[root]=0; }}void update(int root,int l,int r,int L,int R,int x){ if(l>=L && r<=R) { add[root]+=x; sl[root]+=x; sr[root]+=x; return; } push_down(root); int mid=(l+r)>>1; if(L<=mid) update(lson,l,mid,L,R,x); if(R>mid) update(rson,mid+1,r,L,R,x); push_up(root);}int main(){ SIS; int n,pos=1,cnt=0; cin >> n; string s,t(n+1,' '); cin >> s; for(int i=0;i
转载地址:http://siyof.baihongyu.com/