思路:模拟,根据序列P可以构造出S,然后再根据S,推出序列W。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
char str[50];
int w[25];
int main()
{
int t, n, num;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
int i ,j = 0, cnt = 0;
for (i = 0; i < n; ++i) {
scanf("%d", &num);
for (; j < num; ++j)
str[cnt++] = '(';
str[cnt++] = ')';
}
n *= 2;
int lf, rt;
cnt = 0;
for(i = 0; i < n; ++i) {
if (str[i] == '(')
continue;
lf = 0;
rt = 1;
j = i - 1;
while (1) {
if (str[j] == ')')
rt++;
else
lf++;
if(rt == lf) {
w[cnt++] = rt;
break;
}
j--;
}
}
for (i = 0; i < cnt-1; ++i)
printf("%d ", w[i]);
printf("%d\n", w[cnt-1]);
}
return 0;
}