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. error in Swift. “Consecutive Declarations On A Line Must Be Separated By ';'”

    当输入的时候以上代码的时候出现这个错误,经过尝试发现条件表达式?前面应该有一个空格  不然会和swift中的?和 !混淆

  2. IT English Collection(16) of Message

    1 前言 本文介绍了关于Objective-C中的消息机制,详情如下. 转载请注明出处:http://blog.csdn.net/developer_zhang 2 详述 2.1 原文 A messa ...

  3. [React] React Router: activeStyle & activeClassName

    react-router provides two props for setting a specific style on a Link component whose path matches ...

  4. C++指针数组和数组指针

    指针相关问题 using namespace std; int main(){ //a) 一个整型数( An integer) int a; //b) 一个指向整型数的指针( A pointer to ...

  5. USB 管道 && 端点

    管道是对主机和usb设备间通信流的抽象.      管道和usb设备中的端点一一对应,一个usb设备含有多少个端点,其和主机进行通信时就可以使用多少条管道,且端点的类型决定了管道中数据的传输类型.  ...

  6. js精度丢失解决办法

    /** * 加法运算,避免数据相加小数点后产生多位数和计算精度损失. * * @param num1加数1 | num2加数2 */ function numAdd(num1, num2) { var ...

  7. java对象Integer不能引用传递

    java对象Integer不能引用传递 /** * The value of the <code>Integer</code>. * * @serial */ private ...

  8. ORACLE网络配置大全没有比这个更详细的【weber出品】

    一.起篇 现在怎么说也是互联网时代,数据库也要联网,很多朋友学习Oracle的时候无外乎搭建的是以下两种学习环境: 1.直接在windows环境下安装Oracle后直接sqlplus连接. 2.在wi ...

  9. Swift中对计算属性的理解和对之前的补充

    这个功能的重点作用应该是在计算上. 对于一般的属性,要么直接存一个,要么直接读一个,计算属性则可以根据所设置内容,进行一些修改或计算之类的, 比如: import UIKit class sample ...

  10. Canvas--2

    Canvas2(关键词:setLineDash .rect .strokeRect .clearRect .arc.sin .strokeText )   绘制其他样式: lineCap 结束端点的设 ...