q789321 发表于 2015-11-26 07:15:01

【codechef】Chef and Bracket-Pairs (分层dp)




  

Input:
11
-1 -2 9 2 -3 -4 3 4 8 8 1
Output:
12
http://www.codechef.com/problems/CHEFBR/
  
  

#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<vector>
#include<queue>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define ll long long
#define mod 1000000007
using namespace std;
int x;
ll dp;//dp表示j到i有多少个平衡子序列
int main(){
int n;
cin>>n;
for(int i=1;i<=n;++i){
cin>>x;
}
for(int i=0;i<101;++i)
for(int j=0;j<101;++j) //初始化(因为乘法所以设成1)
dp=1;
for(int i=1;i<=n;++i){
for(int j=1;j<=i;++j)   //未算上i点时的子序列个数
dp=dp;
if(x>0){//算上i点时的子序列个数(为了不算多,只取右括号的情况 )
for(int j=i-1;j>=1;--j){//j为k到i的中间位置
if(x+x==0){   //匹配
for(int k=1;k<=j;++k)//这里要注意不是j-1(因为也要被算上)
dp=(dp+(dp*dp)%mod)%mod;
//把k到i去除掉j位置和i位置后剩余的两段平衡序列个数相乘
}
}
}
}
cout<<dp<<endl;
return 0;   
}   


  
页: [1]
查看完整版本: 【codechef】Chef and Bracket-Pairs (分层dp)