比较有意思,因为要脱离掉原来的括号匹配的想法来做。 先建立括号,再建立W式 #include <cstdio> using namespace std; const int MAX = 200; int data[MAX]; char input[MAX]; int result[MAX]; int n,inputi,resulti; void change() { int inputn = 0 ; for( int i = 1 ; i <= n ; ++i ) { for( int j = data[i] - data[i-1] ; j >= 1 ; --j ) input[inputn++] = '('; input[inputn++] = ')'; } input[inputn++] = '/0'; } int find() { int mid = 1 ; inputi++; while( input[ inputi ] == '(' ) mid += find(); inputi++; result[ resulti++ ] = mid; return mid; } int main() { int n1; scanf("%d",&n1); while(n1--) { scanf("%d",&n); resulti = 1 ; inputi = 0; data[0] = 0 ; for( int i = 1 ; i <= n ; ++i ) scanf("%d",&data[i]); change(); while( input[ inputi ] != '/0') find(); printf("%d",result[1]); for( int i = 2 ; i <= n ; ++i ) printf(" %d",result[i]); printf("/n"); } return 0; }