HDU4055 - number string(DP)

原创 2016年08月29日 18:28:34
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


思路:dp[i][j]表示处理前i个字符以j结尾可能的序列数。
当a[i]=='I'时,dp[i][j]=sum(dp[i-1][k]),(1<=k<=j-1), 可进一步化为dp[i][j-1]+dp[i-1][j-1]。
当a[i]=='D'时,dp[i][j]=sum(dp[i-1][k]),(j<=k<=i-1),可进一步化为dp[i][j+1]+dp[i-1][j]。
当a[i]=='?'时,dp[i][j]=sum(dp[i-1][k]),(1<=k<=i-1)。

#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1e3 + 5;
const int mod = 1e9 + 7;
char a[N];
int dp[N][N];
int main()
{
    while(~scanf("%s",a+2))
    {
        memset(dp,0,sizeof(dp));
        int len = strlen(a+2) + 1;
        dp[1][1] = 1;
        for(int i = 2 ;i <= len ;i++)
        {
            if(a[i] == 'I')
            {
                for(int j = 2 ;j <= i ;j++)
                    dp[i][j] = (dp[i][j-1] + dp[i-1][j-1]) % mod;
            }
            else if(a[i] == '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-1 ;j++)
                    sum = (sum + dp[i-1][j]) % mod;
                for(int j = 1 ;j <= i ;j++)
                    dp[i][j] = sum;
            }
        }
        int ans = 0;
        for(int i = 1 ;i <= len ;i++)
            ans = (ans + dp[len][i]) % mod;
        printf("%d\n",ans);
    }
    return 0;
}


版权声明:

相关文章推荐

HDU 4055 Number String(DP)

Number String Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

计数dp hdu 4055 Number String

嗯,什么是计数dp我也不知道,这是第一次遇见类似的题目。 题意:给一个只含‘I','D','?'三种字符的字符串,I表示当前数字大于前面的数字,D表示当前的数字小于前面一位的数字,?表示当前位既可以...

hdu 4055 Number String(动态规划)

Number String Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

HDU 4055 Number String

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意:给一个长度为n的含( d , i , ? )字符串,表示一个1~n+1的排列,表示从第...

hdu 4055 Number String

题意:给出一个字符串,问1~n这n个数中有多少种排列满足给定字符串的要求,字符串str[i]=='I'代表第i位数字要比它前一位大,str[i]=='D'代表第i位数字要比它前一位小,str[i]==...

hdu 4055 Number String

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 Problem Description The signature of a...

HDU 4055 Number String

区域赛的题,虽然水,但是还是见思维,有个细节败了,写篇结题报告,哎不wa不幸福斯基。。。 题目就是给一个排列的增减性描述的字符串,让你求满足这个字符串模式的排列的个数。 一开始我是这么想的:dp过...

hdu 4055Number string

题解:f[i][j]表示前(1~i)的排列,末位为j满足条件的方案数。当s[i-2]=='D'时,有f[i][j]=f[i-1][j]+f[i-1][j+1]+...+f[i-1][i-1];当s[i...

2011年大连赛区E题 Number String DP

题目大意: 对于每一个1~n的排列都可以生成一个长度为n-1的字符串,这个字符串包含三种字符,分别是'I'、'D'、'?'。'I'表示下一位的数比当前位的数要大,'D'表示下一位的数比当前位的数要小...

Zoj 3543 Number String (dp) - 2011 ACM-ICPC Dalian Regional Contest Problem E

又是一道dp。比赛时以为是数学题,一直在找规律推公式。 /** 题意: 由{1,2,3}组成的一个排列132,对应一个字符串"ID",'I'表示Increase,'D'表示Decrease, ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)