hdu 4055 Number String(有点思维的DP)

来源:互联网 发布:adobe 软件 停止工作 编辑:程序博客网 时间:2024/04/30 12:49

Number String

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


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
IIIDDIDD?D??
 

Sample Output
122136
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
 

Recommend
lcy
 题意:
由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果第i字符是‘I’表示排列中的第i-1个数是小于第i个数的。如果是‘D’,则反之。
思路:
首先考虑子问题。设所给模式串长度为i。我们考虑前i-1匹配结尾为j的方法数和长度为i以第k大结尾的方法数。
我们设dp[i][j]表示用前i个数字前i个位置匹配(即满足条件)且以j结尾的方法数,j为相对大小。那么有一个规律。如果前i个位置匹配。对于任意
1<=m<=i。其中x€[m,i]加1后仍匹配。因为大的增幅一致依然大。小的还是小。不过怎么得到递推方程呢?
我们要求dp[i][j]。
1.如果模式串第i位为'I'那么dp[i][j]=segma(dp[i-1][k]).k<j。相当于用因为dp[i-1][k]的组合中大于等于j的位置仍满足匹配。而k<j所以可以成功转移。
2.如果模式串第i位为'D'那么dp[i][j]=segma(dp[i-1][k]).k>=j。因为dp[i-1][k]的组合中大于等于j的位置仍满足匹配。而k>=加1后大于j所以可以成功转移。
不过如何保证状态全是dp[i-1]转移来的呢。其实dp[i][j]只是在dp[i-1][k]的基础上增加了一个数i。而把j这个数放到了最后一位而已。而我们用i这个数替换掉j这个数。前面还剩i-1个数。所以dp[i]的状态一定是从dp[i-1]对x€[m,i]加1后转移得到。本来转移有三层循环的不过可以有前缀和优化。用空间换时间了。
详细见代码:
#include <iostream>#include<stdio.h>#include<string.h>using namespace std;const int maxn=1010;const int mod=1000000007;char st[maxn];int dp[2][maxn],sum[2][maxn];int main(){    int i,j,len,v,u;    sum[1][0]=sum[0][0]=0;    while(~scanf("%s",st+2))    {        v=0;        len=strlen(st+2);        dp[0][1]=sum[0][1]=1;        for(i=2;i<=len+1;i++)//第一个数前没有数所以从2开始。一直到len+1        {            u=v^1;            for(j=1;j<=i;j++)            {                if(st[i]=='I')                    dp[u][j]=sum[v][j-1];                else if(st[i]=='D')                    dp[u][j]=(sum[v][i-1]-sum[v][j-1]+mod)%mod;//由于取模可能使大的变小                else                    dp[u][j]=sum[v][i-1];                sum[u][j]=(dp[u][j]+sum[u][j-1])%mod;                //printf("sum[%d][%d]:%d\n",i,j,sum[v^1][j]);            }            v=u;        }        printf("%d\n",sum[v][len+1]);    }    return 0;}


感想:
想了大半个晚上。终于想通点了。DP做着还不是那么顺手。还是题做少了啊。不过我不会放弃的。坚持一定会有效果。加油!



原创粉丝点击