括号匹配(区间dp)


Language:Default
Brackets
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11260 Accepted: 5968

Description

We give the following inductive definition of a “regular brackets” sequence:

  • the empty sequence is a regular brackets sequence,
  • if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
  • if a and b are regular brackets sequences, then ab is a regular brackets sequence.
  • no other sequence is a regular brackets sequence

For instance, all of the following character sequences are regular brackets sequences:

(), [], (()), ()[], ()[()]

while the following character sequences are not:

(, ], )(, ([)], ([(]

Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1i2, …, im where 1 ≤ i1i2 < … < im ≤ nai1ai2 … aim is a regular brackets sequence.

Given the initial sequence ([([]])], the longest regular brackets subsequence is [([])].

Input

The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters ()[, and ]; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.

Output

For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.

Sample Input

((()))
()()()
([]])
)[)(
([][][)
end

Sample Output

6
6
4
0
6

Source

                      具体细节在代码中详细讲解~~

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int dp[105][105];//dp[w][s]表示从w到s的字串中,最大的匹配个数是多少(因为是匹配个数所以后面*2输出)
bool match(char a, char b)//看看能否匹配
{
	if ((a == '('&&b == ')') || (a == '['&&b == ']'))
	{
		return 1;
	}
	return 0;
}
int main()
{
	string m;
	while (cin >> m&&m[0]!='e')
	{
		memset(dp, 0, sizeof(dp));//初始化
		int len = m.length();
		for (int s = 1; s < len; s++)//枚举终点
		{
			for (int w = s - 1; w >= 0; w--)//枚举起点,,这里注意要从小范围到大范围的枚举,保证dp的顺序性
			{
				if (match(m[w], m[s]))//如果最两边的可匹配,那就将dp[w][s]初始化为dp[w+1][s-1]+1;
				{
		                     dp[w][s] = max(dp[w][s],dp[w+1][s-1]+1);
				}
				for(int q=w;q<=s;q++)
                                {
                                     dp[w][s]=max(dp[w][s],dp[w][q]+dp[q][s]);//之后枚举断点,看看整个的最大值和分成部分的相加值的比较更新
                                }
			}
		}
		cout << 2*dp[0][len - 1] << endl;//双倍输出
	}
	return 0;
}

括号匹配

时间限制:1000 ms  |  内存限制:65535 KB

难度:6

描述

给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来。
如:
[]是匹配的
([])[]是匹配的
((]是不匹配的
([)]是不匹配的

输入

第一行输入一个正整数N,表示测试数据组数(N<=10)
每组测试数据都只有一行,是一个字符串S,S中只包含以上所说的四种字符,S的长度不超过100

输出

对于每组测试数据都输出一个正整数,表示最少需要添加的括号的数量。每组测试输出占一行

样例输入

4

[]

([])[]

((]

([)]

样例输出

0

0

3

2


        如果想要之后填的越少,那么只要求出原来的字符串中最大的匹配数就好,然后减掉就是答案,

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int dp[105][105];
bool match(char a, char b)
{
	if ((a == '('&&b == ')') || (a == '['&&b == ']'))
	{
		return 1;
	}
	return 0;
}
int main()
{
	string m;
	int test;
	scanf("%d",&test);
	while (test--)
	{
	    cin>>m;
		memset(dp, 0, sizeof(dp));
		int len = m.length();
		for (int s = 1; s < len; s++)
		{
			for (int w = s - 1; w >= 0; w--)
			{
				if (match(m[w], m[s]))
				{
					dp[w][s] = max(dp[w][s],dp[w+1][s-1]+1);
				}
				for(int q=w;q<=s;q++)
                                {
                                        dp[w][s]=max(dp[w][s],dp[w][q]+dp[q][s]);
                                }
			}
		}
		cout << len-2*dp[0][len - 1] << endl;
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
区间DP是一种动态规划的方法,用于解决区间范围内的问题。在Codeforces竞赛中,区间DP经常被用于解决一些复杂的字符串或序列相关的问题。 在区间DP中,dp[i][j]表示第一个序列前i个元素和第二个序列前j个元素的最优解。具体的转移方程会根据具体的问题而变化,但是通常会涉及到比较两个序列的元素是否相等,然后根据不同的情况进行状态转移。 对于区间长度为1的情况,可以先进行初始化,然后再通过枚举区间长度和区间左端点,计算出dp[i][j]的值。 以下是一个示例代码,展示了如何使用区间DP来解决一个字符串匹配的问题: #include <cstdio> #include <cstring> #include <string> #include <iostream> #include <algorithm> using namespace std; const int maxn=510; const int inf=0x3f3f3f3f; int n,dp[maxn][maxn]; char s[maxn]; int main() { scanf("%d", &n); scanf("%s", s + 1); for(int i = 1; i <= n; i++) dp[i][i] = 1; for(int i = 1; i <= n; i++) { if(s[i] == s[i - 1]) dp[i][i - 1] = 1; else dp[i][i - 1] = 2; } for(int len = 3; len <= n; len++) { int r; for(int l = 1; l + len - 1 <= n; l++) { r = l + len - 1; dp[l][r] = inf; if(s[l] == s[r]) dp[l][r] = min(dp[l + 1][r], dp[l][r - 1]); else { for(int k = l; k <= r; k++) { dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]); } } } } printf("%d\n", dp[n]); return 0; } 希望这个例子能帮助你理解区间DP的基本思想和应用方法。如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值