题目描述:

我们给出 S,一个源于 {'D', 'I'} 的长度为 n 的字符串 。(这些字母代表 “减少” 和 “增加”。)
有效排列 是对整数 {0, 1, ..., n} 的一个排列 P[0], P[1], ..., P[n],使得对所有的 i:

如果 S[i] == 'D',那么 P[i] > P[i+1],以及;
如果 S[i] == 'I',那么 P[i] < P[i+1]。
有多少个有效排列?因为答案可能很大,所以请返回你的答案模 10^9 + 7.

示例:

输入:"DID"
输出:5
解释:
(0, 1, 2, 3) 的五个有效排列是:
(1, 0, 3, 2)
(2, 0, 3, 1)
(2, 1, 3, 0)
(3, 0, 2, 1)
(3, 1, 2, 0)

提示:

  1. 1 <= S.length <= 200
  2. S 仅由集合 {'D', 'I'} 中的字符组成。

思路分析:

我们用 dp(i, j) 表示确定了排列中到 P[i] 为止的前 i + 1 个元素,并且 P[i] 和未选择元素的相对大小为 j 的方案数(即未选择的元素中,有 j 个元素比 P[i] 小)。在状态转移时,dp(i, j) 会从 dp(i - 1, k) 转移而来,其中 k 代表了 P[i - 1] 的相对大小。如果 S[i - 1] 为 D,那么 k 不比 j 小;如果 S[i - 1] 为 I,那么 k 必须比 j 小。

代码:

 class Solution {
public:
int mod = 1e9+;
int numPermsDISequence(string S) {
int n = S.size();
if(n==)
return ;
vector<vector<int>> dp(n+, vector<int>(n+, ));
for(int i=; i<n+; i++)
dp[][i]=;
for(int i=; i<=n; i++)
{
for(int j=; j<=i; j++)
{
if(S[i-]=='D')
{
for(int k=j; k<i; k++)
{
dp[i][j] += dp[i-][k];
dp[i][j] %= mod;
}
}
else
{
for(int k=; k<j; k++)
{
dp[i][j] += dp[i-][k];
dp[i][j] %= mod;
}
}
}
}
int ans = ;
for(int i=; i<=n; i++)
{
ans += dp[n][i];
ans %= mod;
}
return ans;
}
};

leetcode 903. DI序列的有效排列的更多相关文章

  1. [LeetCode] 903. Valid Permutations for DI Sequence DI序列的有效排列

    We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for &q ...

  2. [Swift]LeetCode903. DI 序列的有效排列 | Valid Permutations for DI Sequence

    We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for &q ...

  3. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  4. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  5. [leetcode](4.21)2. 按字典序排列最小的等效字符串

    给出长度相同的两个字符串:A 和 B,其中 A[i] 和 B[i] 是一组等价字符.举个例子,如果 A = "abc" 且 B = "cde",那么就有 'a' ...

  6. Leetcode题库——31.下一个排列

    @author: ZZQ @software: PyCharm @file: nextPermutation.py @time: 2018/11/12 15:32 要求: 实现获取下一个排列的函数,算 ...

  7. #leetcode刷题之路47-全排列 II

    给定一个可包含重复数字的序列,返回所有不重复的全排列.示例:输入: [1,1,2]输出:[ [1,1,2], [1,2,1], [2,1,1]] 之前的https://www.cnblogs.com/ ...

  8. [LeetCode] 31. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. LeetCode——376.摆动序列

    如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列.第一个差(如果存在的话)可能是正数或负数.少于两个元素的序列也是摆动序列. 例如, [1,7,4,9,2,5] 是一个摆动序列, ...

随机推荐

  1. 【译】.NET Core 是 .NET 的未来

    为什么要翻译咧,.NET 5 都宣布在 .NET Core 之后发布咯,何不再给 .NET Core 打打鸡血,我这个 .NET Core 的死忠粉. 原文:<.NET Core is the ...

  2. 统一批处理流处理——Flink批流一体实现原理

    实现批处理的技术许许多多,从各种关系型数据库的sql处理,到大数据领域的MapReduce,Hive,Spark等等.这些都是处理有限数据流的经典方式.而Flink专注的是无限流处理,那么他是怎么做到 ...

  3. 『月之谜 数位dp』

    月之谜 Description 打败了Lord lsp 之后,由 于lqr 是一个心地善良的女孩 子,她想净化Lord lsp 黑化的 心,使他变回到原来那个天然 呆的lsp--在倒霉的光之英 雄ap ...

  4. WPF DataGrid row background converter datagrid 行背景随绑定数据变化,转换器

    <DataGrid Grid.Row=" ItemsSource="{Binding SalesList,UpdateSourceTrigger=PropertyChange ...

  5. C# CSV Generic T

    This artice will write the main step to export generic data via csv with complete code and step by s ...

  6. git和小乌龟在windows下安装

    一:所需软件 (1):git 下载地址:https://git-scm.com/download (2):TortoiseGit 下载地址:https://tortoisegit.org/downlo ...

  7. apache-tomcat-7.0.94在Windows上启动时,控制台黑窗口出现乱码解决

    一.问题 二.解决 原因是tomcat日志编码的配置问题. 打开tomcat/conf/logging.properties配置文件. 把编码注释掉或者改为gbk就可以了. 参考:https://bl ...

  8. 在centos7 中docker info报错docker bridge-nf-call-iptables is disabled 的解决方法

    在centos7中安装好docker以后,启动成功,运行命令 docker info ,报错: [root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker info Client ...

  9. 深入理解枚举属性与for-in和for-of

    首先要分清什么是可枚举属性,什么是不可枚举属性 1.可枚举属性 在JavaScript中,对象的属性分为可枚举和不可枚举之分,它们是由属性的enumerable值决定的.可枚举性决定了这个属性能否被f ...

  10. 【IPHONE开发-OBJECTC入门学习】文件的操作,读写复制文件

    转自:http://blog.csdn.net/java886o/article/details/9041547 FileTools.h FileTools.m #import "FileT ...