POJ1068Parencodings

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

这个题的话就是先把给出来的一串数字转化成括号,再把括号转化成要求的,最后输出就行了

#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std ;
int main()
{
    stack<char>Q;
    queue<char>p,q;
    int n;
    cin>>n;
    for(int i = 1 ; i <= n ; i++)
    {
        int m;
        cin>>m;
        int a[66];
        for(int j = 1 ; j <= m ; j++)
        {
            cin>>a[j] ;
            if(j == 1)
            {
                for(int k = 1 ; k <= a[j] ; k++)
                    p.push('(');
                p.push(')');
            }
            else
            {
                for(int k = a[j-1] ; k < a[j] ; k++)
                    p.push('(');
                p.push(')');
            }
        }
        int sum ;
        while(!p.empty())
        {
            if(p.front() == '(')
            {
                Q.push(p.front());
            }
            else
            {
                if(Q.top() == '(')
                {
                    Q.pop();
                    Q.push(1);
                    q.push(1);
                }
                else
                {
                    sum = 1;
                    while(Q.top() != '(')
                    {
                        sum += Q.top() ;
                        Q.pop() ;
                    }
                    Q.pop() ;
                    q.push(sum) ;
                    Q.push(sum) ;
                }
            }
            p.pop() ;
        }
        printf("%d",q.front());
        q.pop() ;
        while(!q.empty())
        {
            printf(" %d",q.front());
            q.pop();
        }
        printf("
");
    }
    return 0 ;
}
View Code
原文地址:https://www.cnblogs.com/luyingfeng/p/3241943.html