1 solutions

  • 0
    @ 2024-11-22 22:33:15

    image

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef double dou;
    typedef pair<int,int> pii;
    #define fi first
    #define se second
    #define mapa make_pair
    typedef long double ld;
    typedef unsigned long long ull;
    template <typename T>inline void read(T &x){
    	x=0;char c=getchar();bool f=0;
    	for(;c<'0'||c>'9';c=getchar()) f|=(c=='-');
    	for(;c>='0'&&c<='9';c=getchar())
    	x=(x<<1)+(x<<3)+(c^48);
    	x=(f?-x:x);
    }
    const int N=1e5+5, mod=998244353;
    int T, n, m;
    char s[N];
    int s1[N], s2[N], t1, t2;
    int main(){
    	//freopen("ex_ciphertext3.in","r",stdin);
    	//freopen("ex_ciphertext3.out","w",stdout);
    	read(T);
    	while(T--){
    		read(n);
    		scanf("%s", s+1);
    		t1=t2=0;
    		int ans=0;
    		for(int i=1; i<=n; ++i){
    			if(s[i]=='('){
    				s1[++t1]=i;
    			}
    			else if(s[i]==')'){
    				if(t1) --t1;
    				else if(t2){
    					s[s2[t2]]='(';
    					--t2;
    				}
    				else{
    					++ans;
    				}
    			}
    			else{
    				s2[++t2]=i;
    			}
    		}
    		while(t1){
    			if(t2&&s2[t2]>s1[t1]){
    				s[s2[t2]]=')'; --t2;
    				--t1;
    			}
    			else --t1, ++ans;
    		}
    		ans+=t2&1;
    		for(int i=1; i<=t2; ++i){
    			if(i&1) s[s2[i]]='(';
    			else s[s2[i]]=')';
    		}
    		printf("%d\n", ans);
    		for(int i=1; i<=n; ++i) putchar(s[i]);
    		putchar('\n');
    	}
    	
    	return 0;
    }
    
    • 1

    Information

    ID
    686
    Time
    1000ms
    Memory
    512MiB
    Difficulty
    8
    Tags
    (None)
    # Submissions
    11
    Accepted
    7
    Uploaded By