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

无语,c++过了,g++wa?

Posted by wzpziyi1 at 2014-01-27 18:16:25 on Problem 1141
代码:#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[105][105],path[105][105],b[105];
char s[105];

void print(int left,int right)
{
    if(left>=right)
    {
        return;
    }

    if(path[left][right]==-1)
    {
       // b[left]=-1;
        print(left+1,right);
    }

    else
    {
        b[left]=1;
        b[path[left][right]]=1;
        //printf("%d %d\n",left,path[left][right]);
        print(left+1,path[left][right]-1);
        print(path[left][right]+1,right);
    }
}
int main()
{
    scanf("%s",s);
    {
        memset(dp,0,sizeof(dp));
        memset(path,-1,sizeof(path));
        memset(b,0,sizeof(b));
        int len=strlen(s);
        len--;
        for(int i=0;i<=len;i++)
        dp[i][i]=1;

        for(int i=len-1;i>=0;i--)
        {
            for(int j=i+1;j<=len;j++)
            {
                dp[i][j]=dp[i+1][j]+1;
                path[i][j]=-1;
                for(int k=i+1;k<=j;k++)
                {
                    if(s[i]=='('&&s[k]==')')
                    {
                        //dp[i][j]=min(dp[i][j],);
                        if(dp[i][j]>dp[i+1][k-1]+dp[k+1][j])
                        {
                            dp[i][j]=dp[i+1][k-1]+dp[k+1][j];
                            path[i][j]=k;
                        }

                    }
                    if(s[i]=='['&&s[k]==']')
                    {
                        if(dp[i][j]>dp[i+1][k-1]+dp[k+1][j])
                        {
                            dp[i][j]=dp[i+1][k-1]+dp[k+1][j];
                            path[i][j]=k;
                        }
                    }
                }
            }
        }
        //printf("%d\n",dp[0][len]);
        print(0,len);
        for(int i=0;i<=len;i++)
        {
            if(b[i]==0)
            {
                if(s[i]=='('||s[i]==')')
                printf("()");
                if(s[i]=='['||s[i]==']')
                printf("[]");
            }
            else
            printf("%c",s[i]);
        }
        //for(int i=0;i<=len;i++)
        //printf("%d ",b[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