poj 1068 Parencodings

自己写的,感觉做的很一般,但是0ms,注意数组并不是所谓最大20,我开的100,否则会Runtime Error。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    int length,count;//count计数最后输出值
    int p_element[100];
    int r_position[100];//存储右括号在string中的位置

    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int s_element[100]={0};
        scanf("%d",&length);
        for(int j=0;j<length;j++)
        {
            scanf("%d",&p_element[j]);
            r_position[j]=j+p_element[j];
            s_element[r_position[j]]=1;
        }
        for(int p=0;p<length;p++)
        {
            count=1;
            for(int q=r_position[p]-1;q>=0;q--)
            {
                if(-1==s_element[q])
                {
                    count++;
                }
                else if(0==s_element[q])
                {
                    s_element[q]=-1;
                    printf("%d ",count);
                    break;
                }
            }
        }
        printf("\n");
    }

    return 0;
}

 

posted @ 2012-06-12 16:56  xiaowenchao  阅读(127)  评论(0编辑  收藏  举报