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 绝对简明手册

    Python 绝对简明手册 help(函数名)来获取相关信息 另外,自带的文档和google也是不可少的 2. 基本语法2.1. if / elif / else x=int(raw_input(&q ...

  2. QList 和std::list的比较

    QList QList<T> 是一个Qt通用容器类.它存储一序列的值,并且提供基于索引的数据访问方法和快速的插入和删除操作. QList<T>, QLinkedList< ...

  3. jetty插件开发配置

    <plugins> <!-- jetty插件 --> <plugin> <groupId>org.mortbay.jetty</groupId&g ...

  4. 基于visual Studio2013解决C语言竞赛题之0806平均分

     题目

  5. WM_PAINT消息详解,使用InvalidateRect或InvalidateRgn函数刻意产生WM_PAINT消息(WIN7里有变化,“调整视觉效果”,将“启用桌面组合”去掉)

    什么时候会触发WM_PAINT消息消息呢? 以下内容来自大名鼎鼎的<Windows程序设计(第五版)> 大多数Windows程序在WinMain中进入消息循环之前的初始化期间都要呼叫函数U ...

  6. 《火球——UML大战需求分析》(第1章 大话UML)——1.5 小结和练习

    说明: <火球——UML大战需求分析>是我撰写的一本关于需求分析及UML方面的书,我将会在CSDN上为大家分享前面几章的内容,总字数在几万以上,图片有数十张.欢迎你按文章的序号顺序阅读,谢 ...

  7. linux系统挂掉问题的分析

    玩linux系统,经常遇到的一件事就是做了某个操作之后系统会突然挂掉,这要怎么办? 1. 首先我们要看log,看看是否会留下一些蛛丝马迹,比如PC/LR是否有留下来. PC是ARM的一个寄存器,即程序 ...

  8. PHP开发经验中介(thinkphp3.2使用技巧)

    1.在模板中截取字符串 {$vo.create_date|mb_substr=###,0,10,'utf-8'}

  9. Hadoop 2.x从零基础到挑战百万年薪第一季

    鉴于目前大数据Hadoop 2.x被企业广泛使用,在实际的企业项目中需要更加深入的灵活运用,并且Hadoop 2.x是大数据平台处理 的框架的基石,尤其在海量数据的存储HDFS.分布式资源管理和任务调 ...

  10. 全方位深度剖析--性能测试之LoardRunner 介绍

    一.介绍 LoardRunner是一种预测系统行为和性能负载的测试工具.通过模拟上千万用户实施并发负载及实时性能监控的方式来确认和查找系统的瓶颈,LoardRunner能够对整个企业架构进行测试.通过 ...