2019牛客暑假多校训练赛第一场E题

针对给定的整数n和m,本博客介绍了一种算法来计算可能的字符串数量,这些字符串由字符A和B组成,并且可以分解为n个AB子序列和m个BA子序列。通过动态规划的方法实现了这一计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:https://ac.nowcoder.com/acm/contest/881/E
来源:牛客网
 

题目描述

Bobo has a string of length 2(n + m) which consists of characters `A` and `B`. The string also has a fascinating property: it can be decomposed into (n + m) subsequences of length 2, and among the (n + m) subsequences n of them are `AB` while other m of them are `BA`.

Given n and m, find the number of possible strings modulo (109+7)

输入描述:

The input consists of several test cases and is terminated by end-of-file.

Each test case contains two integers n and m.

* 0≤n,m≤103

输出描述:

For each test case, print an integer which denotes the result.

输入

1 2
1000 1000
0 0

输出

13
436240410
1

给出n,m,构造字符串可以分成n个AB,m个BA,求方案数

每个A一定是先构成AB,后构成BA

从前到后判断每个位置放A或放B

放A,如果有AB还没A,就把这个A分给AB

          接下来看BA是否有  分到了B但还没有分到A的,如果有,就把这个A分给他

dp[i][j]表示放了i个A,j个B满足条件的方案数

A:不能直接放的是 i-n>=j 即现在A用于组成BA的数量大于B的数量,无法构成m个BA

所以,条件i-n<j,dp[i+1][j]+=dp[i][j]

B同理

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
ll num[2005][2005];
int main()
{
    ll n,m;
    while(scanf("%lld%lld",&n,&m)!=EOF){
        for(ll i=0; i<=n+m; i++){
            for(ll j=0; j<=n+m; j++){
                num[i][j]=0;
            }
        }
        num[0][0]=1;
        for(ll i=0; i<=n+m; i++){
            for(ll j=0; j<=n+m; j++){
                if(i-n<j)
                    num[i+1][j]=(num[i][j]+num[i+1][j])%mod;
                if(j-m<i)
                    num[i][j+1]=(num[i][j]+num[i][j+1])%mod;
            }
        }
        printf("%lld\n",num[n+m][n+m]);
    }
    return 0;
}

 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值