Codeforces VK Cup A.and yet another bracket Sequence (suffix array + balance tree + string)

Source: Internet
Author: User

The problem is quite complicated. There should be a better way.

Main topic:

There is a sequence of parentheses that you can do two things:

· Add a parenthesis to the inside, at the beginning, at the end, and between two brackets.

· Loops through the current bracket sequence, taking the last parenthesis to the beginning.

The above two operations can be done any number of times, requiring the addition of a minimum of parentheses to make the original sequence into a valid bracket sequence. If there are multiple possibilities, the output dictionary order is the smallest one. "(" < ")".

Exercises

The number of opening and closing parentheses is calculated first, so you can assume that the number of opening parentheses is greater than the right parenthesis

The least option is to add a closing parenthesis to the right of the string, so that the number of opening brackets equals the number of closing parentheses.

However, if a scheme is feasible, the number of open parentheses that satisfy the preceding prefix will be greater than the right parenthesis

If it is not satisfied, it can be moved in a circular way, and it will find a solution by looping.

To output a scheme with the smallest dictionary order, a suffix array is required

Repeat the string, do the suffix array, then you know the ranking of each scenario

Find the minimum and feasible solution output.

Another case is that the number of opening parentheses is less than the closing parenthesis, and the same is true.

about how to judge if it works, here's the balance tree

The conditions for each position are written, each time it is moved, the effect is the same for all conditions, so maintaining these conditions with the balance tree

#include <iostream>#include<cstring>#include<cstdio>#include<vector>#include<map>#include<Set>using namespacestd;Const intMAXN = 2e6 + +;intWA[MAXN], WB[MAXN], WV[MAXN], WS[MAXN], SA[MAXN];intRANK[MAXN];intHEIGHT[MAXN];Set<int>S;map<int,int>M;vector<int>V;intA[MAXN];intcmpint*r,intAintBintl) {    returnR[A]==R[B] && r[a+l]==r[b+l];}voidGet_sa (int*r,int*sa,intNintm) {    inti,j,p,*x=wa,*y=wb,*T;  for(i=0; i<m; i++) ws[i]=0;  for(i=0; i<n; i++) ws[x[i]=r[i]]++;  for(i=1; i<m; i++) ws[i]+=ws[i-1];  for(i=n-1; i>=0; i--) sa[--ws[x[i]]]=i;  for(p=1, j=1; p<n; j*=2, m=p) { for(p=0, I=n-j; i<n; i++) y[p++]=i;  for(i=0; i<n; i++)if(SA[I]&GT;=J) y[p++]=sa[i]-J;  for(i=0; i<n; i++) wv[i]=X[y[i]];  for(i=0; i<m; i++) ws[i]=0;  for(i=0; i<n; i++) ws[wv[i]]++;  for(i=1; i<m; i++) ws[i]+=ws[i-1];  for(i=n-1; i>=0; i--) sa[--ws[wv[i]]]=Y[i];  for(t=x,x=y,y=t,p=1, x[sa[0]]=0, i=1; i<n; i++) X[sa[i]]=CMP (y,sa[i-1],sa[i],j)? p1:p + +; }}voidGet_height (int*r,int*sa,intN) {    intI, J, k=0;  for(i=1; i<=n; i++) rank[sa[i]]=i;  for(i=0; i<n; height[rank[i++]]=k) for(k?k--:0, j=sa[rank[i]-1]; R[I+K]==R[J+K]; k++);}voidHinsert (intx) {    if(M[x] = =0) S.insert (x); M[X]++;}voidHerase (intx) {    if(M[x] = =1) s.erase (x); M[X]--;}CharSTR[MAXN];intTR, tl;intMain () {CIN>>str; intn = strlen (str), NL =0, nr =0;  for(inti =0; I < n; i++){        if(Str[i] = ='(') nl++, str[i] =1; Elsenr++, str[i] =2; }    if(NL <nr) {TL=0;  for(inti = n1; I >=0; i--){            if(Str[i] = =1) tl++; A[i]=2*tl-(ni); Hinsert (-A[i]); } TL=0;  for(inti = n1; I >=0; i--){            if(-(*s.begin ()) <=-(n-i-1)+2*TL)            V.push_back (i); Herase (-A[i]); if(Str[i] = =1) tl++; Hinsert (-(2*nl-n-(n-i) +2*tl)); }        intN =2*n-1;  for(inti =0; I < n; i++) A[i] =Str[i];  for(inti = n; i < N; i++) A[i] = str[i-N]; Get_sa (A, SA, N+1,4);  for(inti =1; I <= N; i++) Rank[sa[i] =i; intMAXR = n+ -, Kr =0;  for(Auto i:v) {if(i+1>= N) Break; if(rank[i+1] <MAXR) {MAXR= rank[i+1]; Kr= i+1; }} A[n]= a[n-N];  for(inti =0; i < NR-NL; i++) printf ("(");  for(inti = Kr; i < kr+n; i++) printf ("%c", a[i] = =2?')':'('); } Else{TR=0;  for(inti =0; I < n; i++){            if(Str[i] = =2) tr++; A[i]=2*tr-i-1; Hinsert (-A[i]); } TR=0;  for(inti =0; I < n; i++){            if(-(*s.begin ()) <=-i+2*TR)            V.push_back (i); Herase (-A[i]); if(Str[i] = =2) tr++; Hinsert (-(2*nr-n-i-1+2*tr)); }        intN =2*n-1;  for(inti =0; I < n; i++) A[i] =Str[i];  for(inti = n; i < N; i++) A[i] = str[i-N]; Get_sa (A, SA, N+1,4);  for(inti =1; I <= N; i++) Rank[sa[i] =i; intMAXR = n+ -, Kr =0;  for(Auto i:v) {if(Rank[i] <MAXR) {MAXR=Rank[i]; Kr=i; }        }         for(inti = Kr; i < kr+n; i++) printf ("%c", a[i] = =2?')':'(');  for(inti =0; i < NL-NR; i++) printf (")"); }}

Codeforces VK Cup A.and yet another bracket Sequence (suffix array + balance tree + string)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.