P1068
模擬根據第一數列推出括號序列
然后根據序列得出第二個數列
#include <iostream>
using namespace std;
const int MAXN=21;
int p[MAXN],w[MAXN];
char ch[MAXN*2];
int t;
int main()

{
cin>>t;
while(t--)
{
int n;
cin>>n;
for (int i=1;i<=n;++i)
cin>>p[i];
int top=0;
for (int i=1;i<=p[1];++i)
ch[++top]='(';
ch[++top]=')';
for (int i=2;i<=n;++i)
{
if (p[i]>p[i-1])
{
for (int j=1;j<=p[i]-p[i-1];++j)
ch[++top]='(';
ch[++top]=')';
}
else
ch[++top]=')';
}
top=0;
int count;
for (int i=1;i<=2*n;i++)
{
// cout<<ch[i];
if (ch[i]=='(') continue;
top++;
count=0;
int tmp=0;
for (int j=i-1;j>=1;--j)
{
if (ch[j]=='(')
{
if (tmp==0)
{
w[top]=count+1;
break;
}
else
{
tmp--;
}
}
else
{
tmp++;
count++;
}
}
}
for (int i=1;i<=n;i++)
cout<<w[i]<<" ";
cout<<endl;
// system("pause");
}
return 0;
}
posted on 2009-10-03 15:28 Vincent 閱讀(109) 評論(0) 編輯 收藏 引用 所屬分類: 數據結構與算法

