总是不能正确的将一个大问题变成子问题,而且又找不到状态转移方程。 直接导致这题想了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. Android权限说明 system权限 root权限

    原文链接:http://blog.csdn.net/rockwupj/article/details/8618655 Android权限说明 Android系统是运行在Linux内核上的,Androi ...

  2. Python基础--字典:当索引不好用时

    当列表或是元组的索引不能达到我们的目的时,我们想到了还有一种序列,即字典. 创建 字典 由多个键以及相应的值构成的键-值对组成. 键唯一.值能够不唯一 phonebook = {'xidada':'1 ...

  3. 【POJ 3140】 Contestants Division(树型dp)

    id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  4. tomcat 工作原理简析

    https://github.com/HappyTomas/another-tutorial-about-java-web/blob/master/00-08.md 在00-02.理解HTTP中给出了 ...

  5. laravel数据库操作sql语句用Eloquent ORM来构造

    现在有查询语句: SELECT users.sNmame, users.iCreateTime, users_ext.iAge, users_ext.sSex FROM users LEFT JOIN ...

  6. 用verilog表示两个4x4矩阵的乘法运算?及单个矩阵的求逆

    input[63:0] A0, //A0表示A矩阵的第一行 其中A0[63:48] A0 [47:32] A0[31:16] A0 [15:0]分别表示第一行中的四个元素(每个元素16位表示),下同i ...

  7. 新标准C++程序设计读书笔记_继承和多态

    简单继承的例子: #include <iostream> #include <string> using namespace std; class CStudent { pri ...

  8. flex and bison学习笔记01

    工作需要,学习一下Flex and bison,以前在编译原理的课上听老师说过他们的前辈,lex and yacc.Flex and bison就是lex and yacc的升级版. 参考书:flex ...

  9. oracle /plsql 计算平闰年天数函数

    --计算平闰年天数函数 CREATE OR REPLACE FUNCTION f_ping_run_nian (i_year NUMBER --定义函数名 ) RETURN VARCHAR2 IS - ...

  10. Retrofit--官网2.1.0

    Retrofit--官网2.1.0 android Retrofit 介绍 API 描述 请求方法 URL 处理 请求体 表单的 ENCODED 和 MULTIPART HEADER 处理 同步 VS ...