Zoj 3543 Number String(dp)

题目链接

Number String Time Limit: 5 Seconds      Memory Limit: 65536 KB

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,2,3,,,,n的排列来说,用字符串S表示排列的变化趋势。若第 i 个数小于后一个数,s[ i-1 ]=' I ',若第 i 个数大于后一个数,s[ i-1 ]=' D '。现在已知字符串S,问满足S的排列有多少种。若S[ i ]==‘?’,表示第 i-1 个数与后一个数的关系未知,S[ i ]既可以为‘ I ’,也可以为‘ D ’。

做这道题大概做了4个小时,还是没做出来,看来我dp还是太弱。。

题解:假设第1个字母为‘ I ’,对于第1个数来说,我们可以填1,2,3,,n-1,假设我们填了x,那么接下来就是求1到N的排列,第一个数为X并且满足字符串S[ 0 ] 到 S[ n-2 ] 。对于还没填的数字来说,因为我们只关心数字的相对大小关系,所以我们可以把剩下的数看成1到N-1 的排列,新的排列与原来的排列的大小关系我们很容易求出。比如现在我们填了2,把剩下的数离散化为一个新的排列后,新排列的2对应与以前的3,3对应与以前的4,1还是以前的1。得到了新排列,假设新排列的第1个数填了y,那么现在我们就要求一个1到N-1的排列,第一个数为y,并且满足字符串S[ 1 ]到S[ n -2 ]。这个问题显然是前一个问题的子问题。我们分别统计出子问题,就能统计出整个问题。

所以我们用dp[ i ][ j ],表示要填1到 i 的排列,让S的最后 i-1个字母满足条件,第1个数字填j ,满足条件的排列的个数。

假设字符为“  I ”,那么转移就是:

dp[ i ] [ j ]=dp[ i-1 ] [ j ]+dp[ i-1 ][ j+1]+.....dp[ i-1 ] [ i-1 ]

假设字符为“ D ”,那么转移就是:

dp[ i ] [ j ]=dp[ i-1 ][ 1 ]+dp[ i-1 ][ 2 ]+......dp[ i-1 ][ j-1 ]

假设字符串为“ ?”,那么转移就要包含前面两种情况。

转移可以优化到O(1)。复杂度O(N^2)。

代码如下:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#define nn 1100
#define inff 0x3fffffff
#define mod 1000000007
using namespace std;
typedef long long LL;
char s[nn];
LL dp[nn][nn];
LL sum[nn];
int main()
{
    int i,j;
    while(scanf("%s",s)!=EOF)
    {
        int ls=strlen(s);
        for(i=1;i<=ls+1;i++)
        {
            for(j=1;j<=ls+1;j++)
            {
                dp[i][j]=0;
            }
        }
        dp[1][1]=1;
        int n=1;
        for(i=ls-1;i>=0;i--)
        {
            n++;
            sum[0]=0;
            for(j=1;j<=n-1;j++)
                sum[j]=(dp[n-1][j]+sum[j-1])%mod;
            if(s[i]=='I')
            {
                for(j=1;j<=n;j++)
                {
                    dp[n][j]=(sum[n-1]-sum[j-1]+mod)%mod;
                }
            }
            else if(s[i]=='D')
            {
                for(j=1;j<=n;j++)
                {
                    dp[n][j]=sum[j-1];
                }
            }
            else
            {
                for(j=1;j<=n;j++)
                {
                    dp[n][j]=(sum[n-1]-sum[j-1]+mod)%mod;
                    dp[n][j]=(dp[n][j]+sum[j-1])%mod;
                }
            }
        }
        LL ans=0;
        for(i=1;i<=n;i++)
        {
            ans=(ans+dp[n][i])%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}




你可能感兴趣的:(dp,动态规划,ACM)