http://acm.pku.edu.cn/JudgeOnline/problem?id=1068
分析:
代码:
分析:
(1) The first element of W-sequence must be 1.
(2) The matched left parenthesis is the closest unmatched left parenthesis.
(3) The left must have left parenthesis and the first right parenthiesis matches with the left parenthesis nearest to it.
(4) If , ,else w[i]=i+1; the subscript must be from 0.
代码:
#include
"
iostream
"
#include " stdio.h "
using namespace std;
int p[ 21 ],w[ 21 ],t,n,k;
bool flag;
int main()
{
scanf("%d",&t);
for(int i=0;i<t;i++)
{
scanf("%d",&n);
for(int j=0;j<n;j++)
scanf("%d",&p[j]);
w[0]=1;
for(int j=1;j<n;j++)
{
flag=false;
for(k=j-1;k>=0;k--)
{
if(p[j]-p[k]>=j-k)
{
w[j]=j-k;
flag=true;
break;
}
}
if(flag==true)
{
continue;
}
else
w[j]=j+1;
}
for(int i=0;i<n;i++)
printf("%d ",w[i]);
printf("\n");
}
return 0;
}
#include " stdio.h "
using namespace std;
int p[ 21 ],w[ 21 ],t,n,k;
bool flag;
int main()
{
scanf("%d",&t);
for(int i=0;i<t;i++)
{
scanf("%d",&n);
for(int j=0;j<n;j++)
scanf("%d",&p[j]);
w[0]=1;
for(int j=1;j<n;j++)
{
flag=false;
for(k=j-1;k>=0;k--)
{
if(p[j]-p[k]>=j-k)
{
w[j]=j-k;
flag=true;
break;
}
}
if(flag==true)
{
continue;
}
else
w[j]=j+1;
}
for(int i=0;i<n;i++)
printf("%d ",w[i]);
printf("\n");
}
return 0;
}