POJ 2955 Brackets
2016-04-22 15:25
155 查看
K - Brackets
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
SubmitStatusPracticePOJ
2955
Appoint description:
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
i1, i2, …, im where 1 ≤
i1 < i2 < … < im ≤
n, ai1ai2 …
aim is a regular brackets sequence.
Given the initial sequence
Input
The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters
Output
For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.
Sample Input
Sample Output
这几天都在做区间dp的题
做到这题的时候思路很清晰很快就ac了
ACcode:
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define maxn 110
using namespace std;
int dp[maxn][maxn];
char str[maxn];
int main(){
while(scanf("%s",str+1)&&strcmp(str+1,"end")){
memset(dp,0,sizeof(dp));
int n=strlen(str+1);
for(int l=2;l<=n;++l)
for(int i=1;i+l-1<=n;++i){
int j=i+l-1;
for(int k=i;k<=j;++k)dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
if(str[i]=='('&&str[j]==')'||str[i]=='['&&str[j]==']')dp[i][j]=max(dp[i][j],dp[i+1][j-1]+2);
}
/* for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
cout<<dp[i][j]<<' ';
}
cout<<'\12';
}*/
int ans=0;
for(int i=1;i<=n;++i){
ans=max(ans,dp[1][i]+dp[i+1]
);
// cout<<dp[1][i]<<" "<<dp[i+1]
<<'\12';
}
printf("%d\n",ans);
}
return 0;
}
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
SubmitStatusPracticePOJ
2955
Appoint description:
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
i1, i2, …, im where 1 ≤
i1 < i2 < … < im ≤
n, ai1ai2 …
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的题
做到这题的时候思路很清晰很快就ac了
ACcode:
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define maxn 110
using namespace std;
int dp[maxn][maxn];
char str[maxn];
int main(){
while(scanf("%s",str+1)&&strcmp(str+1,"end")){
memset(dp,0,sizeof(dp));
int n=strlen(str+1);
for(int l=2;l<=n;++l)
for(int i=1;i+l-1<=n;++i){
int j=i+l-1;
for(int k=i;k<=j;++k)dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
if(str[i]=='('&&str[j]==')'||str[i]=='['&&str[j]==']')dp[i][j]=max(dp[i][j],dp[i+1][j-1]+2);
}
/* for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
cout<<dp[i][j]<<' ';
}
cout<<'\12';
}*/
int ans=0;
for(int i=1;i<=n;++i){
ans=max(ans,dp[1][i]+dp[i+1]
);
// cout<<dp[1][i]<<" "<<dp[i+1]
<<'\12';
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- DataTable案例
- 浏览器兼容性问题
- POJ 1930
- Why I am Here
- 多个域名使用微信获取用户信息的解决办法
- 集成QQ语音通话
- 双线性插值算法的详细总结
- Sublime Text插件:HTML+CSS+JAVASCRIPT+JSON快速格式化
- js数组最大值max和最小值min
- 8、控制文件、日志文件
- spring对事务的控制 AOP
- 树形结构
- wcf 快速创建
- 数据排序方法的比较
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- 利用spring的profile切换不同的环境
- CSS display:block inline
- 关于百度地图申请key和混淆说明
- 个人工作总结04
- 数据结构与算法分析java——树2(二叉树类型)