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, 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.
 
 
题意:对于一个1~n的排列,根据相邻两个数的大小关系,递增记为' I ',递减记为‘ D ’,所以可以得到一个长为n-1的字符串,现在每次给一个包含‘ I ’、‘ D ’和‘ ? ’ 的字符串s[1~n],‘ ? ’ 表示‘ I ’ 或 ‘ D ’ ,现在求满足这个字符串的排列有多少种?
 
思路:动态规划,定义dp[ i ][ j ] 表示满足字符串s[1]~s[i]且以 j 结束的1~(i+1)的排列种数。
         1、如果第i+1个字符为‘ I ’,那么dp[i+1][j]=sum(dp[i][k]) (1<=k<j),对于dp[i][k]表示的任意一个排列,这个排列为以k结束的1~(i+1)的一个排列,那么对于这个排列顺序不变,将这个排列中大于等于 j 的数字分别+1,然后在排列尾加上数字 j ,则构成了一个长为(i+2)且满足s[1~i+1]字符串的排列。
         2、同理,如果第i+1个字符串为‘D ’,那么dp[i+1][j]=sum(dp[i][k]) (j<=k<=i+1)。
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
const int N=1005;
char s[N];
LL dp[N][N];

int main()
{
    while(scanf("%s",s+1)!=EOF)
    {
        int len=strlen(s+1);
        memset(dp,0,sizeof(dp));
        dp[0][1]=1;
        for(int i=1;i<=len;i++)
        {
            if(s[i]=='I')
                for(int j=2;j<=i+1;j++)
                    dp[i][j]=(dp[i][j-1]+dp[i-1][j-1])%mod;
            else if(s[i]=='D')
                for(int j=i;j>=1;j--)
                    dp[i][j]=(dp[i][j+1]+dp[i-1][j])%mod;
            else
            {
                for(int j=1;j<=i+1;j++)
                    dp[i][j]=(dp[i][j-1]+dp[i-1][j-1])%mod;
                LL sum=0;
                for(int j=i;j>=1;j--)
                {
                    sum=(sum+dp[i-1][j])%mod;
                    dp[i][j]=(dp[i][j]+sum)%mod;
                }
            }
        }
        LL ans=0;
        for(int i=1;i<=len+1;i++) ans=(ans+dp[len][i])%mod;
        printf("%lld
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/chen9510/p/7636983.html