LOJ P10150 括号配对 题解

Analysis

区间dp裸题

初始化有点麻烦

i,j能匹配时要特判

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define maxn 110
 6 #define INF 2139062143
 7 using namespace std;
 8 int n,dp[maxn][maxn];
 9 char ch[maxn];
10 int main()
11 {
12         memset(dp,127,sizeof(dp));
13     cin>>ch+1;
14     n=strlen(ch+1);
15     for(int i=1;i<=n;i++) 
16     {
17         if((ch[i]=='('&&ch[i+1]==')')||(ch[i]=='['&&ch[i+1]==']')) dp[i][i+1]=0;
18         else dp[i][i+1]=2;
19         dp[i][i]=1;
20     }
21     for(int len=3;len<=n;len++)
22     {
23         for(int i=1;i<=n;i++)
24         {
25             int j=i+len-1;
26             if(j>n) break;
27             if((ch[i]=='('&&ch[j]==')')||(ch[i]=='['&&ch[j]==']')) dp[i][j]=dp[i+1][j-1];
28             for(int k=i;k<j;k++)
29             {
30                 dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
31             }
32         }
33     }
34     printf("%d",dp[1][n]);
35     return 0;
36 } 
37 /*
38 4
39 4 5 9 4
40 */

请各位大佬斧正(反正我不认识斧正是什么意思)

原文地址:https://www.cnblogs.com/handsome-zyc/p/11309321.html