Number String

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1027 Accepted Submission(s): 448

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
 
Source
 
Recommend
lcy
很明显的dp,我们可以得到壮态转移方程 ,如果是增dp[i][j]=sum(dp[i-1][1-j-1]),如果是减 ,我们可以 得到dp[i][j]=sum(dp[i-1][j-i-1]);这样,马上就可以得出结果了!
#include <iostream>
#include <stdio.h>
#include <string.h>
#define mod 1000000007
using namespace std;
char str[1050];
int dp[1050][1050],sum[1050][1050];
int main()
{
int i,j;
while(scanf("%s",str)!=EOF)
{
memset(dp,0,sizeof(dp));
sum[1][1]=1;
int strnum=strlen(str);
for(i=2;i<=strnum+1;i++)
{
for(j=1;j<=i;j++)
{
if(str[i-2]=='I'||str[i-2]=='?')
{ dp[i][j]=(dp[i][j]+sum[i-1][j-1])%mod;
}
if(str[i-2]=='D'||str[i-2]=='?')
{ dp[i][j]=(dp[i][j]+((sum[i-1][i-1]-sum[i-1][j-1])%mod+mod)%mod)%mod; }
sum[i][j]=(sum[i][j-1]+dp[i][j])%mod;
}
}
printf("%d\n",sum[strnum+1][strnum+1]);
}
return 0;
}

 

Statistic |
Submit |
Discuss |
Note

hdu4055 Number String的更多相关文章

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

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

  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

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

  4. 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' = ...

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

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

  6. Number String

    Number String 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 dp 定义状态:dp[i][j]为当strlen=i,数字结尾为j的 ...

  7. js Number string

    Number string number Js只有一种数字类型(包括整型,浮点型) 极大或极小的可用科学计数法来表示.(7.7123e+1) 所有js数字均为64位 Js所有的数字都存储为浮点型 小数 ...

  8. 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] + ...

  9. HDU 4055 Number String dp

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

随机推荐

  1. java多线程中synchronized关键字的用法

    转自:http://www.cdtarena.com/javapx/201308/9596.html 由于同一进程内的多个线程共享内存空间,在Java中,就是共享实例,当多个线程试图同时修改某个实例的 ...

  2. java设计模式之——适配器模式

    适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 适配器模式的用途 用电器做例子,笔记本电脑的插头一般都是三相的,即除了阳极.阴极 ...

  3. bzoj 1901: Zju2112 Dynamic Rankings(树套树)

    1901: Zju2112 Dynamic Rankings 经典的带改动求区间第k小值问题 树套树模板,我是用的线段树套splay实现的,并且用的数组模拟的,所以可能空间略大,bzoj过了,zoj过 ...

  4. JEECG社区 一个微信教育站点案例源代码分享

    微信教育站点案例演示: http://t.cn/RvPgLcb 源代码分享: http://pan.baidu.com/s/1cUImy 截图演示: watermark/2/text/aHR0cDov ...

  5. jquery mobile左右滑动切换页面

    jquery mobile左右滑动切换页面 $(function() {$("body").bind('swiperight', function() {  $.mobile.ch ...

  6. linux下crontab的使用方法

    <span style="font-size:14px;">在Linux中任务可以被配置在指定的时间段.指定的日期.或系统平均载量低于指定的数量时自动运行. cront ...

  7. 零积分下载,2014年辛星mysql教程秋季版第一本已经完工,期待您的支持

    经过一段时间的不懈努力.终于,2014年辛星mysql教程秋季版的第一本,即夯实基础已经完工,在csdn的下载地址为:去下载地址 ,假设左边地址跪了,能够去http://download.csdn.n ...

  8. C++基础学习笔记----第十四课(new和malloc的区别、单例模式等深入)

    本节主要讲new关键字和malloc函数的差别,编译器对构造函数调用的实质,单例模式的实现等. new和malloc的差别 1.malloc和free是C语言的库函数,以字节为单位申请堆空间.new和 ...

  9. 代码格式化工具Astyle配置

    Astyle是一个很好的代码格式化工具,其他不多说,下面介绍一下我在VS 2010的配置 1. http://sourceforge.net/projects/astyle,这是该插件的网站,下载后把 ...

  10. “add measurements”(添加度量)菜单问题