zoukankan      html  css  js  c++  java
  • 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.
     
    Author
    HONG, Qize
     

      2011 Asia Dalian Regional Contest

    题意:由数字1n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果第i字符是‘I’表示排列中的第i-1个数是小于第i个数的。如果是‘D’,则反之。

    思路:刚开始完全没有思路。。。

    其实做dp的话首先一定要确定好状态转移方程

    状态转移方程: dp[i][j]表示在i个数时以j结尾的方案数,那么可以得到:

                   当s[i]='I'或'?'时(表示增加),那么dp[i][j]+=dp[i-1][k](1=<k<j)

                   当s[i]='D'或'?'时(表示减少),那么dp[i][j]+=dp[i-1][k](i>k>=j)

    但是这样时间复杂度是O(n^3),会超时啊,所以引入sum[][]数组来记录前缀,使时间降为O(n^2)

     

     

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 #define N 1006
     8 #define MOD 1000000007
     9 char s[N];
    10 int dp[N][N];//dp[i][j]表示在这个排列中第i个数字以j结尾的,满足条件的子排列有多少个。
    11 int sum[N][N];
    12 int main()
    13 {
    14     while(scanf("%s",s+2)!=EOF)
    15     {
    16         int n=strlen(s+2);
    17         memset(dp,0,sizeof(dp));
    18         memset(sum,0,sizeof(sum));
    19         dp[1][1]=sum[1][1]=1;
    20         for(int i=2;i<=n+1;i++)
    21         {
    22             for(int j=1;j<=i;j++)
    23             {
    24                 if(s[i]=='I' || s[i]=='?')
    25                 {
    26                     
    27                         dp[i][j]=dp[i][j]+sum[i-1][j-1];
    28                         dp[i][j]%=MOD;
    29                 }
    30                 if(s[i]=='D' || s[i]=='?')
    31                 {
    32                     
    33                         dp[i][j]=dp[i][j]+(sum[i-1][i-1]-sum[i-1][j-1])%MOD+MOD;
    34                         dp[i][j]%=MOD;
    35                 }
    36                 sum[i][j]=(sum[i][j-1]+dp[i][j])%MOD;
    37             }
    38             
    39         }
    40 
    41         printf("%d
    ",sum[n+1][n+1]);
    42     }
    43     return 0;
    44 }
    View Code
  • 相关阅读:
    微信小程序开发前期准备
    怎样在vs2013和vs2015中实现自动编译sass
    在MVC中使用Bundle打包压缩js和css
    Html5 突破微信限制实现大文件分割上传
    Automapper 实现自动映射
    Polly一种.NET弹性和瞬态故障处理库(重试策略、断路器、超时、隔板隔离、缓存、回退、策略包装)
    关于transactionscope 事务的脏数据
    IIS设置session时长
    已禁用对分布式事务管理器(MSDTC)的网络访问的解决方法之一
    DAL.SQLHelper 的类型初始值设定项引发异常的处理
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4735443.html
Copyright © 2011-2022 走看看