Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

给runtime error的同学们一点参考

Posted by rpbear at 2010-01-25 14:41:25 on Problem 1068
我的算法应该很一般
我之所以会RE是因为我每个malloc之前都没有+1,加上就AC了
/*思路是这样的,先根据第一种形式得到括号序列的表示,0是
左括号,1是右括号。然后从序列最右端开始检测,遇到右括号countR就
增加,遇到左括号countL就增加并且递减一次countR,知道countR等于
0的时候意味着找到匹配的左括号,输出*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int t,n;
    scanf("%d",&t);

    while(t--){
        scanf("%d",&n);
        int *p = (int *)malloc(sizeof(int)*(n+1));
        int *w = (int *)malloc(sizeof(int)*(n+1));

        for(int i=0;i<n;i++)
            scanf("%d",&p[i]);
        int *map = (int *)malloc(sizeof(int)*(p[n-1]*2+1));
        for(int i=0;i<2*p[n-1];i++){
            map[i] = 3;
            w[i] = 0;
        }

        for(int i=0;i<n;i++){
            for(int j=0;j<2*p[i];j++){
                if(map[j] == 3){
                    map[j] = 0;
                }
            }
            map[p[i]+i] = 1;
        }

        int mark = 2*p[n-1]-1;

        for(int i=n-1;i>=0;i--){
            int countL=0,countR = 0;
            for(int j=mark;j>=0;){
                if(map[j]) countR++;
                else{
                    countL++;
                    countR--;
                }
                j--;
                if(!countR){
                    w[i] = countL;
                    for(int k=mark-1;k>=0;k--)
                        if(map[k]){
                            mark = k;
                            break;
                        }
                    break;
                }
            }

        }
        for(int i=0;i<n;i++)
            printf("%d ",w[i]);
        printf("\n");

    }
        return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator