poj 2955 Brackets 区间DP
来源:互联网 发布:ntfs foe mac教程 编辑:程序博客网 时间:2024/05/21 07:47
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
66406
Source
#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<fstream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<cctype>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI (4.0*atan(1.0))#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)#define lson ind<<1,le,mid#define rson ind<<1|1,mid+1,ri#define MID int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)#define mk make_pair#define _f first#define _s second#define ysk(x) (1<<(x))using namespace std;typedef long long ll;//const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);const int INF =0x3f3f3f3f;const int maxn= 100+5 ;//const int maxV=12 ;char s[maxn];int dp[maxn][maxn];int n;bool match(int le,int ri){ if(s[le]=='('&&s[ri]==')') return true; if(s[le]=='['&&s[ri]==']') return true; return false;}int DP(int le,int ri)//起点,增量{ if(~dp[le][ri]) return dp[le][ri]; if(le>=ri) return dp[le][ri]=0; dp[le][ri]=DP(le,ri-1); for(int i=le;i<ri;i++) if( match(i,ri) )//匹配判断 { dp[le][ri]=max(dp[le][ri],2+DP(le,i-1)+DP(i+1,ri-1) );//+2,不是+1 } return dp[le][ri];}int main(){ while(~scanf("%s",s)&&strcmp(s,"end")) { n=strlen(s)-1; memset(dp,-1,sizeof dp); printf("%d\n",DP(0,n));//已经 n=strlen(s)-1了,又写成n-1,屡犯小错误 } return 0;}
- poj 2955 Brackets(区间DP)
- poj 2955 Brackets 【区间DP】
- POJ 2955 Brackets(区间DP)
- poj-2955 Brackets 区间dp
- POJ 2955 Brackets (区间dp)
- POJ 2955 Brackets 区间DP
- poj 2955 Brackets(区间dp)
- POJ 2955 - Brackets(区间DP)
- poj 2955 Brackets 区间dp
- poj 2955 Brackets(区间DP)
- poj 2955 Brackets【区间DP】
- poj 2955 Brackets(区间dp)
- poj 2955 Brackets 区间DP
- poj 2955 Brackets 区间dp
- poj 2955 Brackets(区间dp)
- POJ 2955 Brackets 【区间DP】
- [区间dp] poj 2955 Brackets
- POJ 2955 Brackets(区间dp)
- LayoutParams 参数含义及转换dp
- 安全多线程例子
- 修练8年C++面向对象程序设计之体会之 命名约定及assert
- DBConnection公共类的使用-------java操作数据库(3)
- linux上几款必须装的软件推荐
- poj 2955 Brackets 区间DP
- extjs中的表格编辑
- 设置navigationController的背景颜色和背景图片
- iOS 异常处理@try,catch
- STM32F207(3) SetSysClock
- Windows 环境里下载及安装 GO
- 根据excel中的列表值,导入注解单元写入到指定包下所有java类方法中
- vi命令详解
- Android JNI通过C++调用JAVA