Codeforces Round #282 (Div. 1) A. Treasure

来源:互联网 发布:工兵铲推荐 知乎 编辑:程序博客网 时间:2024/05/21 11:09
乱搞题 细节略多,WA了好几发,故mark。代码一开始没有构思好,故比较冗长。
#include <cstdio>#include <cstring>#include <stack>using namespace std;#define mp make_pair#define pci pair<char, int>const int maxn=100005;int len, cntstar, cntbra;char temp[maxn];bool vis[maxn];pci p;stack<pci> q;stack<char> q2;stack<int> res;void print() {    for (int i=0; i<len; i++)        printf("%d%c", vis[i], i==len-1?'\n':' ');}void solve() {    len=strlen(temp);    for (int i=0; i<len; i++)        if (temp[i]=='(')            q.push(mp(temp[i], i));        else if (temp[i]==')') {            if (q.empty()) {                puts("-1");                return;            }            vis[q.top().second]=true;            q.pop();        }    for (int i=0; i<len; i++)        if (vis[i]==false) {            if (temp[i]=='('||temp[i]=='#')                q2.push(temp[i]);            if (temp[i]=='(')                cntbra++;            else if (temp[i]=='#')                cntstar++;        }    if (cntstar>cntbra) {        puts("-1");        return;    }    if (q2.top()=='(') {        puts("-1");        return;    }    int det=0;    while (!q2.empty()) {        char temp=q2.top();        q2.pop();        if (temp=='#') {            int cnt=0;            while (!q2.empty()&&q2.top()=='(') {                q2.pop();                cnt++;            }            if (cnt+det<=0) {                det--;                det+=cnt;                res.push(1);            } else {                res.push(cnt+det);                det=0;            }        }    }    if (det<0) {        puts("-1");        return;    }    while (!res.empty()) {        int temp=res.top();        res.pop();        printf("%d\n", temp);    }}int main(){    //freopen("in.txt", "r", stdin);    scanf("%s", temp);    solve();    return 0;}

0 0
原创粉丝点击