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. Python MySQLdb Mac安装遇到的问题

    Mac 下使用Python 连接Mysql 数据库,使用到模块MySQLdb,各种问题都出现,搜集整理下,最后发现最关键的还是Mac 下的Python 版本问题 前置条件: 1. 已经安装mysql ...

  2. C#由变量捕获引起对闭包

    C#由变量捕获引起对闭包的思考   前言 偶尔翻翻书籍看看原理性的东西确实有点枯燥,之前有看到园中有位园友说到3-6年工作经验的人应该了解的.NET知识,其中就有一点是关于C#中的闭包,其实早之前在看 ...

  3. Linux设备驱动中的ioctl

    memdev.h #ifndef _MEMDEV_H #define _MEMDEV_H #define MEM_MAGIC 'm' #define MEM_RESTART _IO(MEM_MAGIC ...

  4. Oracle基础知识笔记(10) 约束

    表尽管建立完毕了,可是表中的数据是否合法并不能有所检查,而假设要想针对于表中的数据做一些过滤的话,则能够通过约束完毕,约束的主要功能是保证表中的数据合法性,依照约束的分类,一共同拥有五种约束:非空约束 ...

  5. Qt多工程多目录的编译案例

    源地址:http://blog.csdn.net/libaineu2004/article/details/23625441 写这篇文章的目的是为了让Qt像VC++那样,支持一个工程包含多个项目.即1 ...

  6. 控件风格19种,必须倒背如流——其实就是控件所拥有的能力,即有条件使用VCL框架所提供的(功能)代码

    { New TControlStyles: csNeedsBorderPaint and csParentBackground. These two ControlStyles are only ap ...

  7. perl 继承小例子

    <pre name="code" class="html"><pre name="code" class="ht ...

  8. 基于visual Studio2013解决C语言竞赛题之1068指针数组

        题目 解决代码及点评 /* 68. 在主函数中输入10个不等长的字符串,用另一函数对它们排序. 然后在主函数中输出这10个已排好序的字符串,用指针数组完成. */ #inclu ...

  9. VC socket Connect 超时时间设置

    设置connect超时很简单,CSDN上也有人提到过使用select,但却没有一个令人满意与完整的答案.偶所讲的也正是select函数,此函数集成在winsock1.1中,简单点讲,"作用使 ...

  10. 南京三星面试准备3--数组&基础数据结构

    1.用递归颠倒一个栈. void PushToBottom(stack<int> &mystack,int num) { if(mystack.size()==0) { mysta ...