poj 2955 Brackets 区间DP

来源:互联网 发布:ntfs foe mac教程 编辑:程序博客网 时间:2024/05/21 07:47

 

Brackets
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4838 Accepted: 2577

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

66406

Source

Stanford Local 2004

这份代码屡犯小错了,虽然是1A,但是写的过程中烦了快十几个错误。
1. n=strlen(s)-1;
printf("%d\n",DP(0,n-1));

已经 n=strlen(s)-1了,又写成n-1!!!

2.
 if( match(i,ri) )//匹配判断
直接写成了if(s[i]==s[ri]);

3.
dp[le][ri]=max(dp[le][ri],2+DP(le,i-1)+DP(i+1,ri-1) );//一开始没有写+,结果后来又改成+1


动态规划状态转移方程中有个重要的规律:
那就是正确的状态转移方程,一定有起点(就是递归边界的处理)

,一定有增量(转移时要有进步!!!,不管是+ - * / 还是max() min() ,否则是原地踏步)。


#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;}


0 0
原创粉丝点击