题解报告:poj 2955 Brackets(括号匹配)

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 ≤ i1 < i2 < … < 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
解题思路:经典区间dp,要求找出能配对的括号的最大个数。定义dp[i][j]表示第i到第j个括号的最大匹配数目,那么当第i个括号与第j个括号配对时,dp[i][j]为小的区间最大匹配数加上2即dp[i][j]=dp[i+1][j-1]+2,然后枚举区间[i,j]之间的断点k,合并更新区间[i,j]的最值,最终的答案就是dp[1][length]。时间复杂度为O(n^3)。
AC代码(32ms):
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
using namespace std;
const int maxn=;
char str[maxn];int length,dp[maxn][maxn];
bool check(char ch1,char ch2){
return (ch1=='('&&ch2==')')||(ch1=='['&&ch2==']');
}
int main(){
while(~scanf("%s",str+)&&strcmp(str+,"end")){
memset(dp,,sizeof(dp));length=strlen(str+);
for(int len=;len<=length;++len){//枚举区间长度1~length
for(int i=;i<=length-len;++i){//区间起点i
int j=i+len;//区间终点j
if(check(str[i],str[j]))dp[i][j]=dp[i+][j-]+;
for(int k=i;k<j;++k)//区间最值合并
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+][j]);
}
}
printf("%d\n",dp[][length]);
}
return ;
}

题解报告:NYOJ #15 括号匹配(二)

描述

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

输入

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

输出

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

样例输入

4
[]
([])[]
((]
([)]

样例输出

0
0
3
2
解题思路:上一道题的变形,同样求出给定括号的最大匹配数,那么最少还需要添加的括号数为length-dp[1][length]。
AC代码一(16ms):
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
using namespace std;
const int maxn=;
char str[maxn];int t,length,dp[maxn][maxn];
bool check(char ch1,char ch2){
return (ch1=='('&&ch2==')')||(ch1=='['&&ch2==']');
}
int main(){
while(~scanf("%d",&t)){
while(t--){
scanf("%s",str+);
memset(dp,,sizeof(dp));length=strlen(str+);
for(int len=;len<=length;++len){//区间长度
for(int i=;i<=length-len;++i){//区间起点i
int j=i+len;//区间终点j
if(check(str[i],str[j]))dp[i][j]=dp[i+][j-]+;
for(int k=i;k<j;++k)//更新区间最值
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+][j]);
}
}
printf("%d\n",length-dp[][length]);
}
}
return ;
}

AC代码二(16ms):记忆化搜索。定义dp[i][j]为第i个到第j个括号间至少需要增加的括号数,那么当区间[i,j]为空(i>j)即不含括号时,dp[i][j]=0;当区间只含一个字符即i==j时,dp[i][j]=1,表示至少需要增加1个括号;当第i个括号与第j个括号配对时,dp[i][j]为上一个状态子区间[i+1,j-1]中至少需要增加的括号数即dp[i+1][j-1];然后枚举[i,j]中的断点k,依次合并更新区间[i,j]值dp[i][j],表示从第i个字符到第k个字符至少需要增加的括号数加上从第k+1个字符到第j个字符至少需要增加的括号数,在这所有的k中取最小值作为dp[i][j]即可。

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
using namespace std;
const int maxn=;
const int inf=0x7fffffff;
char str[maxn];int t,length,dp[maxn][maxn];
bool check(char ch1,char ch2){
return (ch1=='('&&ch2==')')||(ch1=='['&&ch2==']');
}
int dfs(int x,int y){
if(x>y)return ;
else if(dp[x][y]>=)return dp[x][y];//表示已搜索过了,此时直接返回当前区间[x,y]的值
else if(x==y)return dp[x][y]=;//单个字符时,需要增加的括号数为1
else{
int var=inf;//先初始化为无穷大
if(check(str[x],str[y]))var=dfs(x+,y-);//如果能匹配,至少需要增加的括号数为上一个状态的值dp[x+1][y-1]
for(int k=x;k<y;++k)
var=min(var,dfs(x,k)+dfs(k+,y));//再更新当前区间[x,y]最少需要增加的括号数,由其子状态得来
return dp[x][y]=var;//返回并且赋值
}
}
int main(){
while(~scanf("%d",&t)){
while(t--){
scanf("%s",str+);memset(dp,-,sizeof(dp));
length=strlen(str+);
printf("%d\n",dfs(,length));
}
}
return ;
}

题解报告:NYOJ #746 整数划分(四)

描述

暑假来了,hrdv 又要留学校在参加ACM集训了,集训的生活非常Happy(ps:你懂得),可是他最近遇到了一个难题,让他百思不得其解,他非常郁闷。。亲爱的你能帮帮他吗?

问题是我们经常见到的整数划分,给出两个整数 n , m ,要求在 n 中加入m - 1 个乘号,将n分成m段,求出这m段的最大乘积

输入

第一行是一个整数T,表示有T组测试数据
接下来T行,每行有两个正整数 n,m ( 1<= n < 10^19, 0 < m <= n的位数);

输出

输出每组测试样例结果为一个整数占一行

样例输入

2
111 2
1111 2

样例输出

11
121
解题思路:定义dp[i][j]表示前i位插入j个乘号(i>j)能得到的最大乘积。根据区间dp思想我们可以从插入较少乘号的结果算出插入较多乘号的结果,即由子问题计算推出大问题。状态转移的关键点是,当插入第j个乘号时,需要枚举其放的位置j~i-1中哪个位置能产生最大的乘积值,则方程可表示为dp[i][j]=max(dp[i][j],dp[k][j-1]*num[k+1][i]),表示前k位中插入j-1(k>j-1)个乘号的最大值乘以从第k+1位到i位组成的数,取所有k中得到的最大乘积值作为dp[i][j],最终的答案就是dp[len][m-1]。
AC代码(4ms):
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
using namespace std;
typedef long long LL;
const int maxn=;
int t,len;LL m;char str[maxn];LL dp[maxn][maxn],num[maxn][maxn];
int main(){
while(cin>>t){
while(t--){
cin>>(str+)>>m;len=strlen(str+);memset(dp,,sizeof(dp));memset(num,,sizeof(num));
for(int i=;i<=len;++i){
num[i][i]=str[i]-'';//num[i,i]的值为其本身
for(int j=i+;j<=len;++j)//num[i][j]表示区间[i,j]组成对应的数
num[i][j]=num[i][j-]*+(str[j]-'');
dp[i][]=num[][i];//前i位插入0个乘号,其值为1~i对应的数即num[1][i]
}
for(int j=;j<m;++j)//插入j个乘号
for(int i=j+;i<=len;++i)//则最少有j+1位,枚举j+1~len位,此时插入j个乘号
for(int k=j;k<i;++k)//枚举j~i-1中的分断点k,找出最大的乘积作为dp[i][j]的值,即前i位加入j个乘号所能得到的最大乘积
dp[i][j]=max(dp[i][j],dp[k][j-]*num[k+][i]);
cout<<dp[len][m-]<<endl;//表示前len位插入m-1个乘号
}
}
return ;
}

最新文章

  1. 提取刷机包内system.new.dat文件
  2. cassandra.yaml介绍
  3. 用python+selenium从百度获取本地明日的天气信息并根据温度情况设置提醒
  4. 实战:ajax带参数请求slim API
  5. seajs 2.3.0 加入jquery
  6. nginx install in centos
  7. for循环++i效率
  8. 武汉科技大学ACM :1003: 零起点学算法67——统计字母数字等个数
  9. DVWA-命令执行学习笔记
  10. Java 平时作业三
  11. loj#2049. 「HNOI2016」网络(set 树剖 暴力)
  12. JavaScript(JS)之简单介绍
  13. P2473 [SCOI2008]奖励关(期望)
  14. SQL Server 2008设置sa用户并开启远程连接
  15. tomcat:8080/返回404;/etc/hosts(identifier-Namespace-scope)
  16. GitHub----初学习(一)
  17. hdu 4952 暴力
  18. HTTP 请求/响应报文结构
  19. pandas的drop函数
  20. JS如何获取url查询字符串的键和值?

热门文章

  1. js编程精解--笔记
  2. DTD复习笔记(复习资料为菜鸟教程里的DTD教程)
  3. armel、armhf和arm64
  4. C# 软件实现远程桌面调用
  5. JAVA MAIL基本功能
  6. UVA-11462 (计数排序)
  7. Watir: Watir-WebDriver对打开的浏览器attach操作
  8. Java必知必会:异常机制详解
  9. JAVA GUI THREAD---***
  10. http://www.cnblogs.com/Javame/p/3632473.html