2 条题解

  • 1
    #include <iostream>
    using namespace std;
    const int N=1e2+10;
    string s;
    int dp[N][N];
    int main(){
    	cin>>s;
    	int n=s.length();
    	for(int i=n-2;i>=0;i--){
    		for(int j=0;j<n;j++){
    			if((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
    				dp[i][j]=dp[i+1][j-1]+2;
    			for(int k=i;k<j;k++){
    				dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
    			} 
    		}
    	}
    	cout<<n-dp[0][n-1]<<endl;
    }
    
    • 0
      @ 2023-7-18 8:30:47
      #include <bits/stdc++.h>
      using namespace std;
      const int N = 1e3 + 5;
      int n;
      int dp[N][N], root[N][N];
      int a[N];
      string s;
      
      bool check(int l, int r)
      {
      	return ((s[l] == '(' && s[r] == ')') || (s[l] == '[' && s[r] == ']')); 
      }
      
      int main()
      {
      	memset(dp, 0x3f, sizeof(dp));
      	cin >> s;
      	int len = s.size();
      	s = " " + s;
      	for(int i = 1; i <= len; i++)
      		dp[i][i] = 1;
      	for(int i = 2; i <= len; i++)
      	{
      		for(int l = 1; l + i - 1 <= len; l++)
      		{
      			int r = i + l - 1;
      			if(l + 1 == r && check(l, r))
      				dp[l][r] = 0; 
      			if(l + 1 < r && check(l, r)) 
      				dp[l][r] = dp[l + 1][r - 1];
      			for(int k = l; k < r; k++)
      			{
      				dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]);	
      			}
      		}
      	}
      	cout << dp[1][len] << endl;
      	return 0; 
      }
      
      • 1

      信息

      ID
      469
      时间
      1000ms
      内存
      512MiB
      难度
      6
      标签
      递交数
      35
      已通过
      11
      上传者