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++提交就AC,G++提交就WA。非常恶心

Posted by edwardyhy1 at 2018-12-23 07:07:15 on Problem 1141
同样的代码。C++提交就AC,G++提交就WA

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cstring>
using namespace std;

const int MaxN = 101;

void print_brackets(char s[], int f[][MaxN], int i, int j) {
    if (i>j) return;
    if (i==j)
    {
        if (s[j]=='(' || s[j]==')')
            printf("()");
        else
            printf("[]");
    }
    else
    {   // i<j
        if (f[i][j]==f[i+1][j-1] &&
            ((s[i]=='(' && s[j]==')') || (s[i]=='[' && s[j]==']')))
        {
            printf("%c", s[i]);
            print_brackets(s, f, i+1, j-1);
            printf("%c", s[j]);
        }
        else
        {
            for (int k=i; k<j; ++k)
                if (f[i][k] + f[k+1][j] == f[i][j])
                {
                    print_brackets(s, f, i, k);
                    print_brackets(s, f, k+1, j);
                    break;
                }
        }
    }
}

int main() {
    char s[MaxN];
    int f[MaxN][MaxN];
    int i,j,k,d;
        scanf("%s", s);
        memset(f, 0x7fff, sizeof(f));
        int n = strlen(s);
        if(n == 0)
        {
            printf("\n");
            return 0;
        }
        for (i=0; i<n; ++i) {
            f[i][i] = 1;    // for every character, we need one more to match it
            f[i+1][i] = 0;
        }
        for (d=1; d<n; ++d) // match s[i]...s[j], d=j-i
        {
            for (i=0; i<n-d; ++i)
            {
                j = i+d;
                f[i][j] = f[i][j-1] + 1;
                if ((s[i]=='(' && s[j]==')') || (s[i]=='[' && s[j]==']'))
                    if (f[i+1][j-1] < f[i][j])
                        f[i][j] = f[i+1][j-1];
                for (k=i; k<j; ++k)
                    if (f[i][k] + f[k+1][j] < f[i][j])
                        f[i][j] = f[i][k] + f[k+1][j];
            }
        }
        print_brackets(s, f, 0, n-1);
        printf("\n");
}

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