题干:

The signature of a permutation is a string that is computed as follows: for each pair of consecutive elements of the permutation, write down the letter 'I' (increasing) if the second element is greater than the first one, otherwise write down the letter 'D' (decreasing). For example, the signature of the permutation {3,1,2,7,4,6,5} is "DIIDID". 
Your task is as follows: You are given a string describing the signature of many possible permutations, find out how many permutations satisfy this signature.
Note: For any positive integer n, a permutation of n elements is a sequence of length n that contains each of the integers 1 through n exactly once.

Input

Each test case consists of a string of 1 to 1000 characters long, containing only the letters 'I', 'D' or '?', representing a permutation signature. 
Each test case occupies exactly one single line, without leading or trailing spaces. 
Proceed to the end of file. The '?' in these strings can be either 'I' or 'D'.

Output

For each test case, print the number of permutations satisfying the signature on a single line. In case the result is too large, print the remainder modulo 1000000007.

Sample Input

II
ID
DI
DD
?D
??

Sample Output

1
2
2
1
3
6

Hint

Permutation {1,2,3} has signature "II".
Permutations {1,3,2} and {2,3,1} have signature "ID".
Permutations {3,1,2} and {2,1,3} have signature "DI".
Permutation {3,2,1} has signature "DD".
"?D" can be either "ID" or "DD".
"??" gives all possible permutations of length 3.

题目大意:

给你一个含n个字符的字符串,字符为'D'时表示小于号,字符为“I”时表示大于号,字符为“?”时表示大小于都可以。比如排列 {3, 1, 2, 7, 4, 6, 5} 表示为字符串 DIIDID。任务是计算所有能产生给定字符串的序列数量,每个序列含n+1个数字,分别为1~n+1,即从1开始且不重复。

一句话题意:给一个序列相邻元素各个上升下降情况('I'上升'D'下降'?'随便),问有几种满足的排列。例:ID  答:2 (231和132)

解题报告:

因为最终是一个排列,观察性质,n个数1~n,如果是一个排列a的话,你去掉a[n]之后,并且让所有大于a[n]的数都-1,会构成一个新的排列。所以定义状态dp[i][j]为构造了i个数并且是i个数的排列,并且最后一个数是j的方案数。

转移的时候,dp[i][j],如果字符串s[i]=='I'说明上一个字符可以任意取1~i-1;

如果s[i]=='D',同理,我可以任选一个比较小的并且使得比他大的增加1即可。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 1005 + 5;
const ll mod = 1e9 + 7;
ll dp[MAX][MAX],sum[MAX];
char s[MAX];
int main()
{while(~scanf("%s",s+1)) {int n = strlen(s+1);for(int i = 0; i<=n+1; i++) for(int j = 0; j<=n+1; j++) dp[i][j] = 0;dp[0][1]=1;for(int i = 1; i<=n; i++) {for(int j = 1; j<=i+1; j++) sum[j] = sum[j-1] + dp[i-1][j];for(int j = 1; j<=i+1; j++) {if(s[i] == 'I') dp[i][j] = sum[j-1];else if(s[i] == 'D') dp[i][j] = sum[i]-sum[j-1];else dp[i][j]=sum[i];dp[i][j]%=mod;}}ll ans = 0;for(int i = 1; i<=n+1; i++) ans = (ans + dp[n][i]) % mod; printf("%lld\n",ans % mod);}return 0 ;
}

【HDU - 4055】Number String(dp,思维)相关推荐

  1. HDU 4054 Number String

    HDU 4054 Number String 思路: 状态:dp[i][j]表示以j结尾i的排列 状态转移: 如果s[i - 1]是' I ',那么dp[i][j] = dp[i-1][j-1] + ...

  2. HDU odd-even number 数位dp

    题意 在l到r区间内求有多少个符合条件"当前数中所有连续的奇数长度是偶数 所有连续的偶数长度是奇数"的个数 分析 典型的数位dp问题 我们设置dp数组时可以根据 dp[pos][p ...

  3. HDU4055 - number string(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 思路:dp[i][j]表示处理前i个字符以j结尾可能的序列数. 当a[i]=='I'时,dp[i ...

  4. hdu 4055 hdu 4489 动态规划

    hdu 4055: 一开始我想的递推方向想得很复杂,看了别人的博客后才醍醐灌顶: 参照他的思路和代码: 1 #include<cstdio> 2 #include<cstring&g ...

  5. Ivan the Fool and the Probability Theory-Codeforces Round #594 (Div. 2)-C题(dp+思维)

    Ivan the Fool and the Probability Theory-Codeforces Round #594 (Div. 2)-C题(dp+思维) time limit per tes ...

  6. HDU 1005 Number Sequence

    [题目]                                                   Number Sequence Time Limit: 2000/1000 MS (Jav ...

  7. perl malformed JSON string, neither tag, array, object, number, string or atom, at character offset

    [root@wx03 ~]# cat a17.pl use JSON qw/encode_json decode_json/ ; use Encode; my $data = [{'name' =&g ...

  8. EXT核心API详解(二)-Array/Date/Function/Number/String

    EXT核心API详解(二)-Array/Date/Function/Number/String Array类 indexOf( Object o )  Number object是否在数组中,找不到返 ...

  9. hdu 5008 Boring String Problem(后缀数组+rmq)

    题目链接:hdu 5008 Boring String Problem 题意: 给你一个字符串,有q个询问,每次询问该字符串所有的子串中字典序第k小的是哪个串,输出位置,如果有多个位置,输出最靠左的那 ...

  10. HDU.1005 Number Sequence

    原题 HDU.1005 Number Sequence 分类 杂题 题意 给定一个数列{an}\left\{ a_n \right\}{an​}的前两项a1a_1a1​.a2a_2a2​,以及其递推公 ...

最新文章

  1. [SCOI2007]修车
  2. 概率编程库Pymc3案例之神经网络
  3. 【图像处理】MATLAB:退化函数及多种复原方法
  4. JVM调优系列:(三)类加载和执行机制
  5. 2017夏季达沃斯今日开幕,人工智能元素尤其突出
  6. 在Ubuntu服务器上打开第二个控制台会话
  7. python小练习—名片管理系统(增、删、改、查、数据本地保存)
  8. Typecho评论邮件提醒插件美化版CommentToMail
  9. mysql二分法查找亿行_算法——二分法查找(binarySearch)
  10. 蓝桥杯 ADV-223 算法提高 8-1因式分解
  11. __cplusplus、extern “C”关键字意义
  12. 如何理解和使用Java package包
  13. 自学网c语言教学视频教程下载,C语言从入门到精通教程 高清不加密 黄老师 视频教程 教学视频 百度网盘下载...
  14. 微信无法打开xlsx文件_微信打不开文件怎么办显示excel丢失或损坏
  15. 3个传教士与3个野人,哥带你们过河去
  16. CSI笔记【9】:阵列信号处理及MATLAB实现(第2版)阅读随笔(一)
  17. 关于一政网教育,考生们是如何看待的?
  18. 乘风破浪、厚积薄发国产服务器软件: LinWin Http Server
  19. Elasticsearch(ES) 基本知识
  20. HTTP中200、302、304、404和500等响应状态码含义

热门文章

  1. c#调用.exe程序
  2. 会php学java入门要多久_php8(java入门要多久)
  3. unity全栈开发是什么意思_unity游戏公司面试问题总结
  4. idea lombok 离线安装_Lombok与IntelliJ IDEA干了一架,完胜
  5. mpAndroidchart 坐标和图表距离_【玩转图表系列】六步,美化你的图表,让老板刮目相看!...
  6. qt支持Linux下word导出么,qt怎么实现保存到Word
  7. mongodb如何根据字段(数组类型)的长度排序_大数据存储技术选型(七)——MongoDB设计模式及索引优化...
  8. uci数据集_数据分析找不到数据集?快来看这个盘点
  9. asm扩容流程_Oracle rac asm 扩容
  10. pandas 提取某几列_【科学计算工具二】初识Pandas