Problem Description
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.

 
Author
HONG, Qize
 

2011 Asia Dalian Regional Contest

题意:由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果第i字符是‘I’表示排列中的第i-1个数是小于第i个数的。如果是‘D’,则反之。

思路:刚开始完全没有思路。。。

其实做dp的话首先一定要确定好状态转移方程

状态转移方程: dp[i][j]表示在i个数时以j结尾的方案数,那么可以得到:

当s[i]='I'或'?'时(表示增加),那么dp[i][j]+=dp[i-1][k](1=<k<j)

当s[i]='D'或'?'时(表示减少),那么dp[i][j]+=dp[i-1][k](i>k>=j)

但是这样时间复杂度是O(n^3),会超时啊,所以引入sum[][]数组来记录前缀,使时间降为O(n^2)

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define N 1006
#define MOD 1000000007
char s[N];
int dp[N][N];//dp[i][j]表示在这个排列中第i个数字以j结尾的,满足条件的子排列有多少个。
int sum[N][N];
int main()
{
while(scanf("%s",s+)!=EOF)
{
int n=strlen(s+);
memset(dp,,sizeof(dp));
memset(sum,,sizeof(sum));
dp[][]=sum[][]=;
for(int i=;i<=n+;i++)
{
for(int j=;j<=i;j++)
{
if(s[i]=='I' || s[i]=='?')
{ dp[i][j]=dp[i][j]+sum[i-][j-];
dp[i][j]%=MOD;
}
if(s[i]=='D' || s[i]=='?')
{ dp[i][j]=dp[i][j]+(sum[i-][i-]-sum[i-][j-])%MOD+MOD;
dp[i][j]%=MOD;
}
sum[i][j]=(sum[i][j-]+dp[i][j])%MOD;
} } printf("%d\n",sum[n+][n+]);
}
return ;
}

hdu 4055 Number String(dp)的更多相关文章

  1. hdu 4055 Number String (基础dp)

    Number String Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. HDU4055 - number string(DP)

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

  3. HDU 4055 Number String (计数DP)

    题意:由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果第i字符是‘I’表示排列中的第i-1个数是小于第i个数的. 如果是‘D’,则反之. 析:dp[i][j] 表示前 i ...

  4. Number String(DP)

    题目: 题意: 给你一个字符串s,s[i] = 'D'表示排列中a[i] > a[i+1],s[i] = 'I'表示排列中a[i] < a[i+1]. 比如排列 {3, 1, 2, 7, ...

  5. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  6. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  7. HDU 4055 Number String dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4055 Number String Time Limit: 10000/5000 MS (Java/O ...

  8. hdu 4055 Number String(有点思维的DP)

    Number String Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. hdu 4055 Number String

    Number String http://acm.hdu.edu.cn/showproblem.php?pid=4055 Time Limit: 10000/5000 MS (Java/Others) ...

随机推荐

  1. 知方可补不足~用xsl来修饰xml

    概念相关 XSL是可扩展样式表语言的外语缩写,是一种用于以可读格式呈现 XML(标准通用标记语言的子集)数据的语言. 起始于 XSL 万维网联盟(W3C)开始发展 XSL 的原因是:存在着对于基于 X ...

  2. xss漏洞校验

    Xss(跨站脚本攻击)大家应该已经都有所了解,下面讲讲怎样查找xss漏洞吧. 确定xss漏洞的基本方法是使用攻击字符串来验证的,例如”><script>alert(document. ...

  3. [AngularJS] Angular 1.5 $transclude with named slot

    In Angular 1.5, there is no link and compile. So use if you transclude, you cannot access the fifth ...

  4. HBase学习(十四)LINUX下用Eclipse构建HBase开发环境

    Eclipse,HBase版本号眼下没有发现须要特别指定 1:从HBase集群中复制一份Hbase部署文件,放置在开发端某一文件夹下(如在/app/hadoop/hbase096文件夹下). 2:在e ...

  5. debian linux 中如何查看软件包是否已经安装和如何安装、卸载软件

    练习 1 方案:确定软件包是否安装 如果您不确定某个软件包是否已经安装,可以使用 dpkg 的 -l (L的小写) 选项: $ dpkg -l zsh No packages found matchi ...

  6. KindEditor简单的Demo使用

    一般的做网站后台都会用到富文本编辑器,网上也有很多优秀的富文本编辑器,这里是开源中国的富文本编辑器推荐:http://www.oschina.net/project/tag/172/wysiwyg 我 ...

  7. android中正确保存view的状态

    英文原文: http://trickyandroid.com/saving-android-view-state-correctly/ 转载此译文须注明出处. 今天我们聊一聊安卓中保存和恢复view状 ...

  8. Windows下Node.js开发环境搭建-合适的开发环境

    1)生产环境中的Node.js应用 Windows + Linus 2)虚拟机工具 VirtualBox 虚拟机CentOS安装 3)xShell与xFtp(windows到linux文件传输) 4) ...

  9. python 下的数据结构与算法---1:让一切从无关开始

    这段时间把<Data Structure and Algorithms with python>以及<Problem Solving with  Algorithms and Dat ...

  10. ASP.NET MVC上传文件的几种方法

    1.Form表单提交 <p>Form提交</p> <form action="@Url.Action("SavePictureByForm" ...