| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
区间dp~~#include<stdio.h>
#include<string.h>
#include<iostream>
#define N 120
#define INF 0x3f3f3f3f
using namespace std;
int dp[N][N];
char a[N];
int main()
{
int n,m,j,i,k,l;
while(~scanf("%s",a+1))
{
if(strcmp(a+1,"end")==0)
break;
memset(dp,0,sizeof(dp));
n=strlen(a+1);
for(l=2;l<=n;l++)
for(i=1;i+l-1<=n;i++)
{
j=i+l-1;
dp[i][j]=0;
if(a[i]=='('&&a[j]==')'||a[i]=='['&&a[j]==']')
dp[i][j]=dp[i+1][j-1]+2;
for(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];
}
printf("%d\n",dp[1][n]);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator