题解 CF494A 【Treasure】

2018-12-16 15:44:38


前言:感谢各位大佬的支持

@Guoyh

二话不说,放代码:

# include <queue>
# include <stack>
# include <cstdio>
# include <vector>
# include <cstring>
# include <algorithm>
# define F(i, a, b) for (int i = a; i < b; i++)//宏定义不说了
using namespace std;
char s[10000050];//字符串
int main(){

        scanf("%s", &s);
        int l = strlen(s), now = 0, num = 0, t = 0, n = 0;//初步定义
        F(i, 0, l){
                if (s[i] == '(') now++;//以now来表示括号数匹配
                if (s[i] == ')') now--;
                if (s[i] == '#'){
                        now--;
                        num++;//抵消,#number++;
                }
                if (now < 0){
                        printf("-1\n");
                        return 0;//不符合要求直接输出-1
                }
        }
        F(i, 0, l){
                if (s[i] == '(') n++;
                if (s[i] == ')') n--;
                if (s[i] == '#'){
                        t++;
                        if (t == num) n -= now;
                        n--;//计算过程
               }
                if (n < 0){
                        printf("-1\n");//不符合要求直接输出-1
                        return 0;
                }
        }
        F(i, 0, num - 1) printf("1\n");输出括号字符串匹配
        printf("%d\n", now + 1);//最后在输出数量
        return 0;
}

拒绝抄袭,从我做起

推销团队