题目描述:

我们给出 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. 你不得不知的Golang线程模型 [转载]

    原著:翟陆续(加多) 资深Java , 著Java并发编程之美 一.前言 本节我们来探讨Go的线程模型,首先我们先来回顾下常见的三种线程模型,然后在介绍Go中独特的线程模型. 二.三种线程模型 线程的 ...

  2. 联合 CNCF 共同出品:Kubernetes and Cloud Native Meetup 成都站

    亮点解读 云原生前沿技术分享:阿里经济体“云原生化”宝贵经验与最佳实践成果 OpenKruise 价值几何? 防踩坑指南:国内知名容器平台架构师解读从 ECS 迁移到 K8S 走过哪些坑. ​云原生服 ...

  3. 一个JAVA应用启动缓慢问题排查 --来自jdk securerandom 的问候

    开发某个项目过程中,就需求,搭建了一套测试环境.很快完成! 后来代码中加入了许多新功能,会涉及到反复重启,然后就发现了启动特别慢.这给原本功能就不多的应用增添了许多的负担. 我决定改变这一切!找到启动 ...

  4. 2019-11-27-WPF-全屏透明窗口

    原文:2019-11-27-WPF-全屏透明窗口 title author date CreateTime categories WPF 全屏透明窗口 lindexi 2019-11-27 09:22 ...

  5. Spring Boot MVC 使用 JSP 作为模板

    Spring Boot 默认使用 Thymeleaf 作为模板引擎,直接在 template 目录中存放 JSP 文件并不能正常访问,需要在 main 目录下新建一个文件夹来存放 JSP 文件,而且需 ...

  6. easyui datagird 解决行高不一致问题!

    <style>.datagrid-btable .datagrid-cell {padding: 6px 4px;overflow: hidden;text-overflow: ellip ...

  7. 【设计模式】Singleton

    前言 Singleton设计模式,确保全局只存在一个该类的实例.将构造器声明为private,防止调用(虽然还是可以使用反射来调用).声明一个静态的类实例在类中,声明一个公共的获取实例的方法.这篇博文 ...

  8. 学习shiro第一天

    shiro是一个强大而且易用的安全框架(主要包括认证和授权),它比spring security更加简单,而且它不依赖于任何容器,可以和许多框架集成. shiro的核心是安全管理器(SecurityM ...

  9. RHEL7.5 静默安装(silent mode)oracle11gr2数据库软件

    如果没有图形界面多可怕,或者图形界面安装总报些奇怪的错误多可怕,静默安装数据库软件了解一下 修改主机名.关闭selinux [root@localhost ~]$ sed -i '3,$d' /etc ...

  10. c语言的布尔量

    #include <stdio.h> #include <stdbool.h> int main() { bool b = true; bool t = false; ; }