Location>code7788 >text

On finding the number of legal parenthesis subsequences

Popularity:0 ℃/2024-09-08 17:00:00

Find the number of legal parenthesized subsequences

meaning of a title

contexts

legal bracket stringThe definitions are as follows:

  1. () is a legal bracket string.
  2. in the event thatA is a legal bracket string, then(A) is a legal bracket string.
  3. in the event thatAB is a legal bracket string, thenAB is a legal bracket string.

substringtogether withdistinct substringsThe definitions are as follows:

  1. string (computer science)S The substring ofS centerprogressionA character string consisting of any of the characters of theS The available starting positions for substrings of\(l\) with termination position\(r\) is denoted by\(S (l, r)\)\(1 \leq l \leq r \leq |S |\)\(|S |\) (denotes the length of S).
  2. S The two substrings are treated as differentif and only ifThey are inS A different position in the\(l\) different or\(r\) Different.

title

Given a string of parentheses\(s\)(which is not necessarily a legal bracket string), ask\(s\) How many of themutually exclusive substringsbelegal bracket string

example

Sample Input

(()()

Sample Output

2

analyze

Observe sequence 1:

()()()

\(pos = 2\) The value of the contribution to the answer at the time of\(1\)

\(pos = 4\) The time itself\([3, 4]\) There is then a sequence of parentheses that satisfies the requirement, which is then merged with the previous one to become\([1, 4]\) , so the value of the contribution to the answer is\(2\) and add to that the previous\([1, 2]\) itself has a sequence of parentheses totaling\(3\)

\(pos = 6\) At that time, the total contribution value is\(3\) Plus the front has\(3 + 3 = 6\) Species. None of the other positions contribute (left bracket has no contribution value).

In conclusion.\(pos\) because of\(1 \sim 6\) The contribution to the answer at the time is respectively\(0, 1, 0, 2, 0, 3\) , the combined total answer is\(0, 1, 1, 3, 3, 6\)


Observe sequence 2:

())()

\(pos = 2\) When the contribution to the answer is\(1\)

\(pos = 3\) when there is no contribution because it is not satisfied into a matching sequence of parentheses (we only look at the contribution value of the right parentheses).

\(pos = 5\) When, as a result of\(pos = 3\) There is an extra back bracket, so\([1, 3]\) Mismatches that result in\([1, 5]\) does not become a matching sequence of parentheses, so the contribution to the answer is still\(1\)

\(pos\) because of\(1 \sim 5\) The contribution to the answer at the time is respectively\(0, 1, 0, 0, 1\) , the combined total answer is\(0, 1, 1, 1, 2\)


Observe sequence 3:

()(())

\(pos = 2\) When the contribution is\(1\)

\(pos = 5\) When, as a result of\(pos = 3\) is broken in the middle, so\([1, 5]\) cannot be matched, so the contribution remains\(1\)

\(pos = 6\) When we found\([1, 2]\) is matched. Therefore\([1, 2], [3, 6]\) can synthesize a matching sequence, so the contribution to the answer is\(2\)

\(pos\) because of\(1 \sim 6\) The contribution to the answer at the time is respectively\(0, 1, 0, 0, 1, 2\) , the combined total answer is\(0, 1, 1, 1, 2, 4\)


It can be found thatA backbrace that matches a front brace, assuming that the first 1 position of this front brace also has a backbrace that has already been matched, allows us to merge the current sequence of matching braces with the previous sequence of matching braces. The contribution of the current back parenthesis is in fact equal to the contribution of the previous back parenthesis + 1.

Elaina's Code

int sum=0,tot[N];
string s;
stack<int> sta;
signed main(){
	cin>>s;
	for(int i=0;i<();i++){
		if(s[i]=='(') (i);
		if(s[i]==')'){
			if(!()){
				int l=();
				();
				tot[i]=tot[l-1]+1;
			}
		}
		sum+=tot[i];
	}
	cout<<sum;
	return Elaina;
}