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

Re:同样的代码。C++提交就AC,G++提交就WA。非常恶心

Posted by CrystBaL at 2021-02-05 18:45:17 on Problem 1141
In Reply To:同样的代码。C++提交就AC,G++提交就WA。非常恶心 Posted by:edwardyhy1 at 2018-12-23 07:07:15
> 同样的代码。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");
> }
我的代码C++没过,改成G++过了。。
#include <stdio.h>
#include <cstring>
#include <map>
#include <string>

const int INF=0x3f3f3f3f;

char ans[105][105][205];

int main()
{
	std::map<int,int> mp;
	mp['(']=1,mp[')']=4,mp['[']=0,mp[']']=3;
	int dp[105][105]={0};
	char s[105];
	scanf("%s",s+1);
	int n=strlen(s+1);
	for(int i=1;i <= n;i++)
	{
		dp[i][i]=1;
		if(s[i] == '(' || s[i] == ')')
			strcpy(ans[i][i],"()");
		else
			strcpy(ans[i][i],"[]");
	}
	for(int i=1;i<n;i++)
		for(int j=1;i+j <= n;j++)
		{
			if(mp[s[i+j]]-mp[s[j]] == 3)
			{
				dp[j][i+j]=dp[j+1][i+j-1];
				if(mp[s[j]] == 1)
				{
					ans[j][i+j][0]='(';
					strcat(ans[j][i+j],ans[j+1][i+j-1]);
					strcat(ans[j][i+j],")");
				}
				else
				{
					ans[j][i+j][0]='[';
					strcat(ans[j][i+j],ans[j+1][i+j-1]);
					strcat(ans[j][i+j],"]");
				}
			}
			else 
				dp[j][i+j]=INF;
			for(int k=j;k<i+j;k++)
				if(dp[j][k]+dp[k+1][i+j]<dp[j][i+j])
				{
					dp[j][i+j]=dp[j][k]+dp[k+1][i+j];
					strcpy(ans[j][i+j],ans[j][k]);
					strcat(ans[j][i+j],ans[k+1][i+j]);
				}
		}
	printf("%s\n",ans[1][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