本题用简单的模拟法就可以解出来,大致题意:一个括号序列有两种序列数字对应:
第一个是相应的右括号前面有多少左括号
第二个是相应右括号与匹配左括号之间有多少右括号(包括本身)
根据p求w
S (((()()())))
P-sequence 4 5 6666
W-sequence 1 1 1456
基本思路:基本思路不复杂,先根据p把括号模拟出来,用0,1也可以用’(‘,’)’.
然后根据括号序列检测就可以了.非常简单.匹配的规则是,从右括号的位置往左遍历,当经过的左右括号数目一样时,括号匹配.
#include "stdafx.h"
#include "iostream"
using namespace std;
int main()
{
int t = 0,top;
char S[100];
cin>>t;
while(t--)
{
memset(S,0,sizeof(S));
top = 0;
int n = 0,x = 0;
int index = 1;
cin>>n;
while(n--)
{
cin>>x;
for(;index<x;index++)
S[top++]='(';
S[top++]=')';
}
for(int i = 0;i<=top-1;i++)
{
if(S[i] == ')')
{
int Leftnum = 0,Rightnum = 1,j;
for( j = i-1;Leftnum!=Rightnum;j--)
{
if(S[j]==')')
Rightnum++;
else
Leftnum++;
}
cout<<Rightnum<<' ';
}
}
cout<<endl;
}
return 0;
}