http://acm.hdu.edu.cn/showproblem.php?pid=4055

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.

/**
hdu4055 dp
题目大意:给定一个字符串,I表示本字符要比前一个字符大,D表示本字符要不前一个字符小,?可大可小。问1~n的全部排列中,
有多少满足条件
解题思路:能够用dp[i][j]表示:处理完第i位,序列末尾位j的序列共同拥有多少个。最后的结果为sigma{dp[N][i]},1≤i≤N
处理dp[1~i][]的过程中i是依次1~n相加。处理完dp[i-1][]后,增加的数即为i,而dp[i][j]是要将i放进去j换
出来,而这里有一种将i放进去j换出来。同一时候不影响升降顺序的方法是:
将dp[i-1][j]的i-1个数的序列中 ≥j 的数都加1。这样i-1变成了i,j变成了j+1,而j自然就补在后面了。 所以对”ID“序列依次处理就可以,初始条件:dp[1][1] = 1; 即仅仅有{1}。
处理‘I’:dp[i][j] = sigma{dp[i-1][x]},当中1≤x≤j-1,可进一步简化,dp[i][j] = dp[i][j-1]+dp[i-1][j-1]
处理‘D’:dp[i][j] = sigma{dp[i-1][x]},当中j≤x≤i-1。可进一步简化。dp[i][j] = dp[i-1][j+1]+dp[i-1][j]
处理‘? ’:dp[i][j] = sigma{dp[i-1][x]}。当中1≤x≤i-1
*/
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
const int mod=1e9+7;
char a[1005];
int dp[1005][1005];
int main()
{
while(~scanf("%s",a))
{
int n=strlen(a)+1;
memset(dp,0,sizeof(dp));
dp[1][1]=1;
for(int i=2;i<=n;i++)
{
char ch=a[i-2];
if(ch=='?')
{
int sum=0;
for(int j=1;j<i;j++)
{
sum=(sum+dp[i-1][j])%mod;
}
for(int j=1;j<=i;j++)
{
dp[i][j]=sum;
}
}
else if(ch=='I')
{
for(int j=2;j<=i;j++)
{
dp[i][j]=(dp[i][j-1]+dp[i-1][j-1])%mod;
}
}
else
{
for(int j=i-1;j>=1;j--)
{
dp[i][j]=(dp[i][j+1]+dp[i-1][j])%mod;
}
}
}
int ans=0;
for(int i=1;i<=n;i++)
{
ans=(ans+dp[n][i])%mod;
}
printf("%d\n",ans);
}
return 0;
}

hdu4055 dp的更多相关文章

  1. HDU4055 - number string(DP)

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

  2. hdu4055

    hdu4055 题意 给出一个长度为 n - 1 的字符串,要求构造一个包含数字 [1, n] 的排列,从第二位开始,'I' 表示当前位数字比前一位大,'D' 表示当前位数字比前一位小,'?' 表示可 ...

  3. HDU-4055:Number String

    链接:HDU-4055:Number String 题意:给你一个字符串s,s[i] = 'D'表示排列中a[i] > a[i+1],s[i] = 'I'表示排列中a[i] < a[i+1 ...

  4. HDU-4055 Number String 动态规划 巧妙的转移

    题目链接:https://cn.vjudge.net/problem/HDU-4055 题意 给一个序列相邻元素各个上升下降情况('I'上升'D'下降'?'随便),问有几种满足的排列. 例:ID 答: ...

  5. [总结-动态规划]经典DP状态设定和转移方程

    马上区域赛,发现DP太弱,赶紧复习补上. #普通DP CodeForces-546D Soldier and Number Game 筛法+动态规划 待补 UVALive-8078 Bracket S ...

  6. BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4142  Solved: 1964[Submit][Statu ...

  7. 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...

  8. AEAI DP V3.7.0 发布,开源综合应用开发平台

    1  升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...

  9. AEAI DP V3.6.0 升级说明,开源综合应用开发平台

    AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...

随机推荐

  1. CentOS NFS的安装配置、启动及mount挂载方法

    一.环境介绍: 服务器:centos 192.168.1.225 客户端:centos 192.168.1.226 二.安装: NFS的安装配置:centos 5 : yum -y install n ...

  2. android sax解析xml 文件 动态加载标题

    要解决一个问题 : 问题描述为 把标题动态的加载到 listView子布局中 我们首先通过 java程序写一个把标题写到xml文件的程序.这个程序会在以后讲解. 现在截图 已经写好的xm文件格式如下 ...

  3. Objective-C探究alloc方法的实现

    OS X,iOS中的大部分作为开源软件公开在 Apple Open Source 上.虽然想让大家参考NSObject类的源代码,但是很遗憾,包含NSObject类的Foundation框架没有公开. ...

  4. C#小写人民币转大写

    public string GetRMB(decimal moneyAmount) { string s = moneyAmount.ToString("#L#E#D#C#K#E#D#C#J ...

  5. out返回值的用法与用途

    static void Main(string[] arr) { Console.WriteLine("请输入用户名"); string uname = Console.ReadL ...

  6. KindleRSS推送服务器搭建

    参考http://xcode.so/2010/12/google-gae-rss-to-kindle/这篇文章 1.首先尝试在本机搭建服务器直接推送到kindle 需要使用到kindlereader这 ...

  7. win7下安装搭建PHP环境

    由于最近新找的工作要求php,所以在电脑上安装搭建了PHP环境.主要参考了这篇文章http://www.leapsoul.cn/?p=695(之前第一次搭建时由于版本问题没有弄好) 1.先装apach ...

  8. jquery1.9学习笔记 之选择器(基本元素三)

    标签选择器("element") 描述: 选择所有与给出标签名相匹配的元素. 同功能的JS原生方法:getElementByTagName() 例子:  查找每个div元素. &l ...

  9. 使用NSTimer实现倒计时-备

    今天在CocoaChina上面看到有人在问倒计时怎么做,记得以前在看Iphone31天的时候做过一个,今天翻出来运行不了了,原因是我的IphoneSDK升级到3.1了,以前使用的是2.2.1,在2.2 ...

  10. Unity 3D中的菜单项

    1.File菜单:主要是包含项目和场景的创建.保存以及输出等功能.2.Edit(编辑)菜单:只要包括对场景进行一系列的编辑以及环境设置操作等命令.3.Assets(资源)菜单:掌握资源在Unity中的 ...