Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
同样的代码。C++提交就AC,G++提交就WA。非常恶心同样的代码。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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator