Hdu4055 Number String

Number String

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

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

2011
Asia Dalian Regional Contest

/*
    dp方程的设定比较显然,dp[i][j]表示选了i个元素,最后一个是j的方案数。
    但是在状态转移的时候,我们不得不考虑前面选了什么,也就是状态的设定是有后效性的,
    所以考虑给状态再添一层含义:必须选前i个元素。
    那么这样岂不是每次只能选i吗?那么第二维岂不是没有用了?
    所以我们考虑用j把i替换出来,那么在状态转移的时候就需要考虑放入i时,怎么替换能使原来的大小顺序保持不变。
    将dp[i-1][j]的i-1个数的序列中 ≥j 的数都加1,这样i-1变成了i,j变成了j+1,而j自然就补在后面了。
    处理I:dp[i][j] = Σdp[i-1][x],其中1≤x≤j-1,可进一步简化,dp[i][j] = dp[i][j-1]+dp[i-1][j-1]
    处理D:dp[i][j] = Σdp[i-1][x],其中j≤x≤i-1,可进一步简化,dp[i][j] = dp[i-1][j+1]+dp[i-1][j]
    处理?:dp[i][j] = Σdp[i-1][x],其中1≤x≤i-1
*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 1010
#define mod 1000000007
char s[maxn];
int dp[maxn][maxn];
int main(){
    while(scanf("%s",s+1)!=EOF){
        int n=strlen(s+1);n++;
        int ans=0;
        memset(dp,0,sizeof(dp));
        dp[1][1]=1;
        for(int i=2;i<=n;i++){
            if(s[i-1]==‘I‘)
                for(int j=2;j<=i;j++)
                    dp[i][j]=(dp[i][j-1]+dp[i-1][j-1])%mod;
            else if(s[i-1]==‘D‘)
                for(int j=i-1;j>=1;j--)
                    dp[i][j]=(dp[i][j+1]+dp[i-1][j])%mod;
            else {
                int sum=0;
                for(int j=1;j<i;j++)sum=(sum+dp[i-1][j])%mod;
                for(int j=1;j<=i;j++)dp[i][j]=sum;
            }
        }
        for(int i=1;i<=n;i++)ans=(ans+dp[n][i])%mod;
        printf("%d\n",ans);
    }
}
时间: 2024-10-15 00:26:43

Hdu4055 Number String的相关文章

ZOJ3543 Number String 动态规划

ZOJ3543 Number String 状态f[i][j]表示长度为I的全排列,符合字符串的要求,最后一位数字是j的方案数. 重点在要始终保持是f算的排列的数量,然后转移时用这些推出来新的情况. 就是j<=i 从长度是i-1的排列推长度是i的时候.因为确定了最后一位以后,前面不考虑数值,考虑名次还是一个i-1的排列的某一个.所以,如果要再最后加一个j,可以考虑认为是把前面大于等于j的数字都加一,就得到i的一个排列. 对于'I'或者'?' f[i][j]+=f[i][2..j-1] 对于'D'

JavaScript变量二(数据类型,Number,String,Object)

Number类型: Number类型是JavaScript中最令人关注也是最复杂的类型了.这种类型使用IEEE754格式来表示整数和浮点数值(浮点数值在某些语言中也被称为双精度数值).为支持各种数值类型,ECMA-262定义了不同的数值字面量格式. 主要分为十进制,八进制,十六进制.除了以十进制表示外,整数还可以通过八进制(以8为基数)或十六进制(以16为基数)的自面值来表示,其中,八进制字面值的第一位必须是(0),然后是八进制数字序列(0-7).如果字面值中的数值超出了范围,那么前面的零将被忽

hdu 4055 Number String(dp)

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, otherwis

flex匹配json number, string

根据官网给出的number格式定义很容易匹配: DIGIT [0-9] DIGITS {DIGIT}+ INT "-"?([0-9]|[1-9][0-9]*) FRAC "."{DIGITS} E [eE][-+]? EXP {E}{DIGITS} NUMBER {INT}({FRAC}|{EXP}|{FRAC}{EXP})? string的匹配就复杂点, 开始一直没有匹配后, 主要是转移字符的问题, 后来在stack overflow上找到了解决办法. 主要参考的

Javascript中类型: undefined, number ,string ,object ,boolean

var a1; var a2 = true;var a3 = 1;var a4 = "Hello";var a5 = new Object();var a6 = null;var a7 = NaN;var a8 = undefined; alert(typeof a); //显示"undefined"alert(typeof a1); //显示"undefined"alert(typeof a2); //显示"boolean"

1038 Recover the Smallest Number string还能这样用 cmp真滴强大

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to differe

HDU 4055 Number String 计数DP

设dp[i][j]表示以j开头的,长度为i的排列的数目. 从字符串的后面到前面DP就得出答案了. #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; const int MAXN=1010; const int MOD=1000000007; char str[MAXN]; int dp[MAXN][MAXN]; in

ZOJ 3543 Number String 【2011大连区域赛】【dp】

题意 给出一串由D,I,?构成的长为n的字符串,这个字符串表示满足某种规则的1到n+1的排列集合,D表示该位置数字比前面一个小,I表示该位置的数字比前面一个大,?表示不确定可D可I 比如 满足DI的数字排列有3,1,2和2,1,3 1<=n<=1000,求有多少种满足此条件的字符串 设计的dp如下 dp[i][j] 代表满足上述字符串前i-1项的长为i尾巴为j的1到i的排列数目 当第i-1项为D时 dp[i][j]=sigma(dp[i-1][k]),i-1=>k>=j 当第i-1

UVA 1650 Number String

https://vjudge.net/problem/UVA-1650 题意:D表示比前一个数打,I表示比前一个数小,?表示不确定 给出一个长为n由D I?组成的字符串,问满足字符串大小要求的n+1的排列的方案数 SDOI2010地精部落的套路 http://www.cnblogs.com/TheRoadToTheGold/p/7305170.html dp[i][j]前i个数的排列中,第一个为j的方案数 #include<cstdio> #include<cstring> #de