总是不能正确的将一个大问题变成子问题,而且又找不到状态转移方程。 直接导致这题想了5个小时最后还是无果。。。

谨记!

Number String

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

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.

 
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define N 1100
#define MOD 1000000007 char g[N];
int dp[][N]; int main()
{
while(scanf("%s",g)!=EOF)
{
memset(dp,,sizeof(dp));
int a=,b=;
int len=strlen(g);
dp[a][]=;
int ti=;
for(int i=;i<=len+;i++)
{
for(int j=;j<=i;j++)
{
if(g[ti]=='I')//以j结尾的。 所有情况
{
dp[b][j] = (dp[b][j]+dp[a][j-]);
if(dp[b][j]>=MOD) dp[b][j]-=MOD;
if(dp[b][j]<) dp[b][j]+=MOD;
}
else if(g[ti]=='D')
{
dp[b][j]= (dp[b][j]+dp[a][i-]-dp[a][j-]);
if(dp[b][j]>=MOD) dp[b][j]-=MOD;
if(dp[b][j]<) dp[b][j]+=MOD;
}else
{
dp[b][j]= (dp[b][j]+dp[a][i-]);
if(dp[b][j]>=MOD) dp[b][j]-=MOD;
if(dp[b][j]<) dp[b][j]+=MOD;
}
}
swap(a,b);
for(int j=;j<=i;j++)
{
dp[b][j]=;
dp[a][j]=(dp[a][j]+dp[a][j-]);
if(dp[a][j]>=MOD) dp[a][j]-=MOD;
if(dp[a][j]<) dp[a][j]+=MOD;
}
ti++;
}
//for(int i=1;i<=len+1;i++)
// ans= (ans+dp[a][i])%MOD;
printf("%d\n",(dp[a][len+]%MOD+MOD)%MOD);
}
return ;
}
Author
HONG, Qize
 
Source
 
Recommend
lcy
 

hdu 4055(经典问题)的更多相关文章

  1. hdu 4055 && hdu 4489 动态规划

    hdu 4055: 一开始我想的递推方向想得很复杂,看了别人的博客后才醍醐灌顶: 参照他的思路和代码: #include<cstdio> #include<cstring> # ...

  2. HDU 4055 The King’s Ups and Downs(DP计数)

    题意: 国王的士兵有n个,每个人的身高都不同,国王要将他们排列,必须一高一矮间隔进行,即其中的一个人必须同时高于(或低于)左边和右边.问可能的排列数.例子有1千个,但是最多只算到20个士兵,并且20个 ...

  3. HDU 4055 Number String dp

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

  4. hdu 4055 Number String

    Number String http://acm.hdu.edu.cn/showproblem.php?pid=4055 Time Limit: 10000/5000 MS (Java/Others) ...

  5. HDU 4055 Number String:前缀和优化dp【增长趋势——处理重复选数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意: 给你一个由'I', 'D', '?'组成的字符串,长度为n,代表了一个1~n+1的排列中 ...

  6. HDU 4055:Number String(DP计数)

    http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意:给一个仅包含‘I','D','?'的字符串,’I'表示前面的数字比后面的数字要小(Increase升 ...

  7. hdu 3943 经典数位dp好题

    /* 题意:求出p-q的第j个nya数 数位dp,求出p-q的所有nya数的个数很好求,但是询问求出最终那个第j个值时是我不会求了看了下别人的思路 具体就是把p-q的第j个转化成0-q的第low+j个 ...

  8. hdu 4055 递推

    转自:http://blog.csdn.net/shiqi_614/article/details/7983298 题意:由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果 ...

  9. HDU 1176 经典dp

    记录最晚时间 从time为2枚举到最晚时间 每个时间段的x轴节点都等于上一个时间段的可触及的最大馅饼数 #include<stdio.h> #include<string.h> ...

随机推荐

  1. Drupal启动阶段之三:数据库

    Drupal在数据库启动阶段仅仅是简单地包含了database.inc文件,然后再注册类加载器: function _drupal_bootstrap_database() { // Initiali ...

  2. ps修图之——四步去修图后的毛边

    PS修图时,多数PS工具都会在图片的边源处留下很多毛边如下图: 这个时候很多新手店主会非常苦脑,会退回原始图片上反复修图起图.可是结果也不怎么满意,当然也许有些店主会有其它方法. 其实不用那么麻烦,只 ...

  3. jBoss设置jvm参数

    jBoss版本: jboss-5.1.0.GA jboss-6.0.0.Final   jboss-5.1.0.GA和jboss-6.0.0.Final修改方法: 打开%JBOSS_HOME%\bin ...

  4. SQL数据表插入随机数(转)

    declare @T TABLE (id int identity(1,1),[Name] nvarchar(20), Randnum int) insert @T ([Name]) select ' ...

  5. vue结合Promise及async实现高效开发。

    在vue中平时的开发中我们应该都会遇到promise函数,比如我们常用的axios,resource这都是用来做http请求的插件. 在平时的开发里,关于axios我们可能是这样写的 import a ...

  6. jms、amqp、mqtt区别与联系

    消息传递作为基本通信机制已经在全世界成功运用.无论是人与人.机器与人还是机器与机器之间,消息传递一直都是唯一常用的通信方式.在双方(或更多)之间交换消息有两种基本机制. 同步消息传递 异步消息传递 同 ...

  7. jQuery国际化插件 jQuery.i18n.properties 【轻量级】

    jQuery.i18n.properties是一款轻量级的jQuery国际化插件,能实现Web前端的国际化. 国际化英文单词为:Internationalization,又称i18n,“i”为单词的第 ...

  8. php_memcahed 使用方法

    用php_memcache.dll 扩展库操作方法 1.下载php_memcache.dll 对应的PHP版本拷贝到PHP目录EXT下 2.在php.ini添加扩展extension=php_memc ...

  9. jfinal的maven配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  10. 点滴积累【C#】---C#实现上传word将路径保存到数据库,文件保存到服务器。并且按照名称读取服务器的word

    效果: 1. . . 数据库: 思路: 上传:先获取word物理地址,然后根据文件的类型判断,然后再保存到相应的文件夹下,再把路径插入到数据库中. 读取:首先根据输入的文件名字在数据库中查找出来文件的 ...