简单区间dp 30pts求调 括号匹配 站外题
  • 板块题目总版
  • 楼主Wildchesse
  • 当前回复3
  • 已保存回复3
  • 发布时间2023/6/12 19:20
  • 上次更新2023/10/23 13:16:43
查看原帖
简单区间dp 30pts求调 括号匹配 站外题
362022
Wildchesse楼主2023/6/12 19:20

题目大意: 给定一个仅由()[]四种字符组成的字符串,你可以从中选出一个子序列,使其满足括号的合法性。问这样的合法括号序列的最长长度为多少? 假设A和B是合法的,那么A+B,B+A,(A),[B]都是合法的。(也就是说,([])我们认为是合法的)

字符串长度不超过一百

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
string s;
int f[105][105];
signed main(){
	cin.tie(0);
	cout.tie(0);
	cin>>s;
	int len=s.size();
	for(int l=2;l<=len;l++){
		for(int i=1,j=i+l-1;j<=len;i++,j++){
			for(int k=i;k<j;k++){
				if((s[i]=='(' and s[j]==')')or(s[i]=='[' and s[j]==']')){
					f[i][j]=max(f[i][j],2+f[i+1][j-1]);
				}
				f[i][j]=max(f[i][j],f[i][k]+f[k+1][j]);
			}
		}
	}
	cout<<f[1][len];
}

2023/6/12 19:20
加载中...