~~~~
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
char seq[125];
int q[125];
int w[125];
int book[125];
int main()
{
int t;
cin >> t;
int n;
int i, j;
int top;
int topw;
while(t--){
memset(book,0,sizeof(book));
memset(seq,0,sizeof(seq));
memset(q,0,sizeof(q));
memset(w,0,sizeof(w));
top = -1;//seq数组的光标
topw = -1;//w数组的光标
cin >> n;
for(i = 0;i < n;i++){
cin >> q[i];
}
for(i = 0;i < q[0];i++){
seq[++top] = '(';
}
seq[++top] = ')';
for(i = 1;i < n;i++){
for(j = 0;j < q[i]-q[i-1];j++){
seq[++top] = '(';
}
seq[++top] = ')';
}
for(i = 0;i <= top;i++){
if(seq[i] == ')'){
for(j = i-1;j >= 0; j--){
if(book[j]==0&&seq[j]=='('){
w[++topw] = (i-j+1)/2;
book[j] = 1;
break;
}
}
}
}
for(i = 0;i <= topw;i++){
printf("%d%c", w[i],i==topw? '\n' : ' ');
}
}
return 0;
}
数组一定要开大,找了一晚上bug,第二天灵感一来,把数组开大,然后就a了~~~GG
算法核心内容 先把p序列转化为括号序列,找到一个右括号(下标为i),然后往前找第一个没有用到的做括号(下标为i),然后(i-j+1)/2即为包涵的括号数量。