【codechef】Chef and Bracket-Pairs (分层dp)
2015-06-19 15:19
357 查看
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[101]; ll dp[101][101]; //dp[j][i]表示j到i有多少个平衡子序列 int main(){ int n; cin>>n; for(int i=1;i<=n;++i){ cin>>x[i]; } for(int i=0;i<101;++i) for(int j=0;j<101;++j) //初始化(1代表空串) dp[i][j]=1; for(int i=1;i<=n;++i){ for(int j=1;j<=i;++j) dp[j][i]=dp[j][i-1]; //先继承 if(x[i]>0){ //为了不算多,只取右括号的情况 for(int j=i-1;j>=1;--j){ //j为k到i的中间位置 if(x[j]+x[i]==0){ //匹配 for(int k=j;k>=1;--k) dp[k][i]=(dp[k][i]+(dp[k][j-1]*dp[j+1][i-1])%mod)%mod; //把k到i去除掉j位置和i位置后剩余的两段平衡序列个数相乘 } } } } cout<<dp[1] <<endl; return 0; }
相关文章推荐
- 十大经典数据挖掘算法(9) 朴素贝叶斯分类器 Naive Bayes
- You possibly will not often be capable of find the money for Obtain Diesel engine Designer watches
- 使用AIDL实现进程间的通信
- project继承domain管理权限
- ajax和grails后台的搭配
- AIX系统的日常监控维护
- LeetCode: Contains Duplicate II
- 使用 SailingEase WinForm 框架构建复合式应用程序(插件式应用程序)
- Containers In Memory: How Big Is Big?
- ibaits---remapResults属性
- Contains Duplicate II
- 运行到自定义的AVPlayer时,程序崩在main函数里
- hdu 2476 String painter(区间dp)
- LightOJ 1236 Pairs Forming LCM(唯一分解定理+素数刷选)
- Thunderbird Mail 支持 Microsoft Exchange
- Thunderbird Mail 支持 Microsoft Exchange
- CF 8A Train and Peter
- [leetCode] Contains Duplicate II
- [leetCode] Contains Duplicate
- Contains Duplicate II