Codeforces Round #343 (Div. 2) C. Famil Door and Brackets(简单dp)
来源:互联网 发布:中小学图书馆软件下载 编辑:程序博客网 时间:2024/06/05 10:29
题目链接:点这里!!!!
题意:
给你一长度为m仅包含'('和')'的字符串s,叫你组成一个长度为n的字符串。
使得满足下列两个条件:
1、字符串中'('和')'的数量是相等的。
2、字符串前缀中'('的数量大于等于')'。
问你p+s+q得到符合条件长度为n的字符串的(p,q)组合有多少种?答案对1e9+7取模。
数据范围1<=m<=n<=100000,n-m<=2000
题解:
我设dp[i][j]为前i位中'('比')'多j个的方案数为多少。
转移方程为:dp[i][j]=dp[i-1][j-1]+dp[i-1][j+1]。
然后我们去枚举p符合条件组成方式,再判断q是否可行,就ok了,具体看代码吧。
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<sstream>#include<algorithm>#include<vector>#include<bitset>#include<set>#include<queue>#include<stack>#include<map>#include<cstdlib>#include<cmath>#define LL long long#define pb push_back#define pa pair<int,int>#define clr(a,b) memset(a,b,sizeof(a))#define lson lr<<1,l,mid#define rson lr<<1|1,mid+1,r#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)#define key_value ch[ch[root][1]][0]#pragma comment(linker, "/STACK:102400000000,102400000000")const LL MOD = 1000000007;const int N = 2000+15;const int maxn = 1e5+15;const int letter = 130;const LL INF = 1e7;const double pi=acos(-1.0);const double eps=1e-10;using namespace std;inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}LL dp[N][N];char s[maxn];int n,m;int main(){ scanf("%d%d",&n,&m); scanf("%s",s); dp[0][0]=1; for(int i=1;i<=n-m;i++){ dp[i][0]=dp[i-1][1]; for(int j=1;j<=i;j++){ dp[i][j]=((dp[i-1][j-1]+dp[i-1][j+1])%MOD+MOD)%MOD; } } int ll=-1000000; int sum=0; for(int i=0;i<m;i++){ if(s[i]==')') sum++; else sum--; ll=max(ll,sum); } sum=-sum; LL ans=0; for(int i=0;i<=n-m;i++) for(int j=0;j<=i;j++){ if(j>=ll&&j+sum<=n-m-i){ ans=(ans+dp[i][j]*dp[n-m-i][j+sum]%MOD)%MOD; } } printf("%I64d\n",ans); return 0;}
0 0
- Codeforces Round #343 (Div. 2) C. Famil Door and Brackets(简单dp)
- Codeforces Round #343 (Div. 2)-C. Famil Door and Brackets
- Codeforces Round #343 (Div. 2) (C. Famil Door and Brackets(DP))
- Codeforces Round #343 (Div. 2) C. Famil Door and Brackets(dp)
- Codeforces Round #343 (Div. 2)C. Famil Door and Brackets DP
- Codeforces Round #343 (Div. 2) C. Famil Door and Brackets(DP)
- Codeforces Round #343 (Div. 2) Problem C——Famil Door and Brackets
- codeforces C. Famil Door and Brackets dp
- codeforces #343 C. Famil Door and Brackets (dp)
- Codeforces 629C - Famil Door and Brackets dp
- codeforces 629C Famil Door and Brackets(dp)
- Codeforces 629C Famil Door and Brackets 【dp】
- Codeforces 629 C Famil Door and Brackets(dp)
- Codeforces 629C Famil Door and Brackets (DP)
- Codeforces 629C Famil Door and Brackets【dp】
- Codeforces 629C Famil Door and Brackets(dp)
- 【Codeforces 629 C Famil Door and Brackets 】+ dp
- Famil Door and Brackets codeforces dp
- iOS开发从入门到精通--基础组件UILabel
- 指数型母函数及模板(HDOJ 1521)
- Linux文章汇总
- eclipse代码颜色设置
- 【TJOI & HEOI 2016】【BZOJ 4556】【JZOJ 4614】 游戏
- Codeforces Round #343 (Div. 2) C. Famil Door and Brackets(简单dp)
- (2) 配置日志log
- GET 与POST的请求使用
- 268. Missing Number(重要!)
- error C2664: “Student1::Student1(int,char [],int)”: 不能将参数 2 从“std::string”转换为“char []”
- 初识java程序的垃圾回收机制及对象在内存中的状态-----自学JAVA(第二节)
- 【MySQL】性能优化之 Index Condition Pushdown
- mysql获取第n条记录
- uc/os-iii学习笔记-任务管理