2021-07-15(动态规划/递归)

7 篇文章 0 订阅
3 篇文章 0 订阅

Brackets Sequence
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 38784 Accepted: 11212 Special Judge

Description
Let us define a regular brackets sequence in the following way:

  1. Empty sequence is a regular sequence.
  2. If S is a regular sequence, then (S) and [S] are both regular sequences.
  3. If A and B are regular sequences, then AB is a regular sequence.

For example, all of the following sequences of characters are regular brackets sequences:

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

And all of the following character sequences are not:

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

Some sequence of characters ‘(’, ‘)’, ‘[’, and ‘]’ is given. You are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string a1 a2 … an is called a subsequence of the string b1 b2 … bm, if there exist such indices 1 = i1 < i2 < … < in = m, that aj = bij for all 1 = j = n.

Input
The input file contains at most 100 brackets (characters ‘(’, ‘)’, ‘[’ and ‘]’) that are situated on a single line without any other characters among them.

Output
Write to the output file a single line that contains some regular brackets sequence that has the minimal possible length and contains the given sequence as a subsequence.

Sample Input

([(]

Sample Output

()[()]

题目大意是:给一个初始序列,然后我们要用最少的次数将它变成一个符合规定的序列。
思想:动态规划.
用dp[i][j]表示把str中i位到j位补成标准形式所花费的次数。
那么对于一个最外围两侧的(,)dp[i][j]=dp[i+1][j-1];
对于这个来说这样也不一定是最小的。dp[i][j]=dp[i][k]+dp[k+1][j];
综合维护这两式子。选择其中小的。如果是第一种情况,那么path[i][j]就应在区间[i,j]之外。就让其为-1;否则就算有需要断点的,写成两个片段组合的形式。
正推倒求。
对于还原序列我们只要写递归式就可以,递归要细节,防止递归找不到出口。
正推时,优先级是先假设给的起点长度,控制断点,再控制长度移动起点。然后再是变化长度。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<string>
using namespace std;

/*

通过dp来帮忙记录最优状态下该序列从何而来,通过递归可以打印出这种序列; 

*/
const int N=105;
char str[N];
int  dp[N][N];//维护i-j的最小操作次数: 
int path[N][N];//记录最小时的断点: 
 
void print(int i,int j){//根据path[i][j]得出结果 ,递归 
	if(i>j)
		return;//这一步很有必要,当递归到序列只有两个长度时且是()这种时,下一步就会出现; 
	if(i==j){
		if(str[i]=='['||str[i]==']'){
			printf("[]");
		}
		else
			printf("()");
	}	
	else if(path[i][j]==-1){
		printf("%c",str[i]);
		print(i+1,j-1);
		printf("%c",str[j]);
	}
	else{
		print(i,path[i][j]);
		print(path[i][j]+1,j);
	}
	return;
} 
 

int main(){
	while(gets(str)){
		int n=strlen(str);//获取长度,开始枚举 
		if(n==0){
			cout<<endl;
			continue;
		}
		for(int i=0;i<n;i++)
			dp[i][i]=1;//初始值:0-1的突破,需要补充括号数 
		//分析该动态规划的优先级进行设计循环 
		for(int r=1;r<n;r++){//拓展长度 ,正推 
			for(int i=0;i<n-r;i++){//序列起点 
				int j=i+r;//获得头i和尾巴j,现在枚举所有状态的转移 
				dp[i][j]=0x3f3f3f3f;//对这个状态赋初值 
				if(str[i]=='['&&str[j]==']'||str[i]=='('&&str[j]==')'){
					dp[i][j]=dp[i+1][j-1];//我们可以很快看出,右边的长度更短,所以一定已经被赋过初值,这种情况不一定是最优解; 
					path[i][j]=-1;//不选择断点 
				} 
				for(int k=i;k<j;k++){//选择断点 
					 if(dp[i][j]>dp[i][k]+dp[k+1][j]){//不选择对结果非优的值进行记录 ,但实际上全都有赋值; 
					 	dp[i][j]=dp[i][k]+dp[k+1][j];
					 	path[i][j]=k; 
					 }
				}
			}
		}
		print(0,n-1); 
		printf("\n");
	}
	
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值