题目描述:
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 1e9 + 7
(n,m <= 1000)
题意:
求有多少种长度为2*(n+m)的字符串使得可以凑出n个‘AB’ 和 m个’BA’,比如当n = 1, m = 1的时候,有ABAB,BAAB,BABA,ABBA四种满足的字符串,答案对1e9+7取模。
题解:
思考这样一个问题:给你一个长为2*(n+m)的字符串,判断它是否能分解出n个’AB’和m个’BA‘。
贪心的思想,我们从前往后扫一遍,遇到A,如果还有’AB’没分到’A’,就把这个’A’分给’AB‘,否则看看’BA’中是否有分到了’B’但还没有分到’A’的,如果有,就把这个’A’分给他,如果没有,那么这个’A’就无法和任何东西配,这个字符串就不满足条件。
有了分配’A’和’B’的原则之后,我们就可以开始dp了。dp[i][j]表示放了i个’A’,j个’B’后依然有可能满足条件的方案数。
dp[i][j]的转移:

最低0.47元/天 解锁文章





