poj 1086 Parencodings
题目意思:P-sequence->代表右括号左边有几个左括号;
W-sequence->代表这对括号(包括这对括号)里面有几个左括号;
思路:首先对数字进行括号还原;
在进行括号匹配;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<set> #include<map> #include<vector> using namespace std; class Node { public: char str[40]; int top; Node() {top =-1;} }; void Res( Node &node ) { int top = node.top,cnt=0,sum,flag=0; for( int i = 0; i <=top ; i ++ ) { cnt=0; if( node.str[i]==')' ) { sum=0; for( int j = i-1 ; j>=0 ; j-- ) { if( node.str[j]==')' ) cnt++; else { if( cnt==0 ) { printf( flag==0?"%d":" %d",sum +1); flag++; break; } else cnt--; sum++; } } } } puts(""); } int main( ) { int Case; while( scanf( "%d",&Case )==1 ) { while( Case-- ) { Node node; int n,num,front=0; scanf( "%d",&n ); for(int i =0 ; i < n ; i ++ ) { scanf( "%d",&num ); int t = num - front; while( t-- ) { node.str[++node.top]='('; } front = num; node.str[++node.top]=')'; } Res( node ); } } //system( "pause" ); return 0; }