POJ

http://poj.org/problem?id=1068

定义一个合法括号串的P序列和W序列,P序列表示每个右括号前面的左括号的数量,W序列(貌似)表示每个右括号和前面的左括号的距离的一半的上整。

按题意模拟。

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;

char s[1005];
int top;
int st[1005];
int stop;

int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
#endif // Yinku
    int t;
    scanf("%d", &t);
    while(t--) {
        top = 0;
        int n;
        scanf("%d", &n);
        int px = 0, x;
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &x);
            int tmp = x - px;
            while(tmp--) {
                s[++top] = '(';
            }
            s[++top] = ')';
            px = x;
        }
        /*for(int i = 1; i <= top; ++i)
            printf("%c", s[i]);
        printf("
");*/
        int out = 0;
        stop = 0;
        for(int i = 1; i <= top; ++i) {
            if(s[i] == '(') {
                st[++stop] = i;
            } else {
                if(out) {
                    printf(" ");
                } else {
                    out = 1;
                }
                printf("%d", (i - st[stop--] + 1) >> 1);
            }
        }
        printf("
");
    }
}
原文地址:https://www.cnblogs.com/Inko/p/11719084.html