【Codeforces 494A】Treasure

【链接】 我是链接,点我呀:)
【题意】

让你把"#"用至少一个右括号代替 使得整个括号序列合法

【题解】

首先我们不要考虑井号 考虑最简单的括号序列 并且把左括号看成1,右括号看成是-1 然后用a[]表示它的一个前缀和(a[0]=0) 这样 这个括号序列合法的充要条件就是 ①a[i]>=0 (1<=i<=len(s)) ②a[len(s)] = 0

这样我们就把括号问题转换成一个仅仅为不等式和一个等式的问题了
而井号仅仅会增加-1,即让某一段往后的a[i]都变小
那么我们先这样,让所有的井号都先用1个右括号代替
这样之后,再看看形成的括号序列是否满足①
如果不满足,则输出-1
如果满足
那么我们接下来就是要想办法把a[len(s)]变成0了
办法肯定就是在某些井号的位置加上右括号
怎么加呢?
肯定是加在最末尾的那个井号位置。
因为这样的话,这a[leng(s)]个需要的右括号才会对最少的a[]造成影响。
也就使得尽可能少的a[]有变成小于0的可能性
最后再检查一下最后一个井号以及它后面的a[]是否大于等于0就好。
(此时a[len(s)]肯定已经变成0了,因为加入了所需的右括号)

【代码】

import java.io.*;
import java.util.*;

public class Main {
    
    
    static InputReader in;
    static PrintWriter out;
        
    public static void main(String[] args) throws IOException{
        //InputStream ins = new FileInputStream("E:\rush.txt");
        InputStream ins = System.in;
        in = new InputReader(ins);
        out = new PrintWriter(System.out);
        //code start from here
        new Task().solve(in, out);
        out.close();
    }
    
    static int N = (int)1e5;
    static class Task{
        String s;
        int a[] = new int[N+10],n;
        int cnt = 0;
        
        public void solve(InputReader in,PrintWriter out) {
        	s = in.next();
        	n = s.length();
        	int idx = 0;
        	for (int i = 0;i < n;i++) {
        		if (s.charAt(i)=='(') {
        			a[i+1] = 1;
        		}else if (s.charAt(i)==')') {
        			a[i+1] = -1;
        		}else {
        			cnt++;
        			idx = i+1;
        			a[i+1] = -1;
        		}
        	}
        	for (int i = 1;i <= n;i++) {
        		a[i]+=a[i-1];
        	}
        	for (int i = 1;i <= n;i++) {
        		if (a[i]<0) {
        			System.out.println(-1);
        			return;
        		}
        	}
        	int temp = a[n];
        	for (int i = idx;i <= n;i++) {
        		a[i]-=a[n];
        		if (a[i]<0) {
        			System.out.println(-1);
        			return;
        		}
        	}
        	for (int i = 1;i <= cnt-1;i++) {
        		out.println(1);
        	}
        	out.println(temp+1);
        }
    }

    

    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream ins) {
            br = new BufferedReader(new InputStreamReader(ins));
            tokenizer = null;
        }
        
        public String next(){
            while (tokenizer==null || !tokenizer.hasMoreTokens()) {
                try {
                tokenizer = new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        
        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}