Problem DescriptionThe Signature of a permutation is a string that's computed as follows:for each pair of consecutive El Ements of the permutation, write down the "I" (increasing) if the second element is greater than the first one, oth Erwise 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 be 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 are a sequence of length n that contains each of the integers 1 through n exactly once.
Inputeach test case consists of a string of 1 to characters long, containing only the letters ' I ', ' D ' or '? ', repres Enting a permutation signature.
Each test case occupies exactly one, without leading or trailing spaces.
Proceed to the end of file. The '? ' in these strings can be either ' I ' or ' D '.
Outputfor 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 Inputiiiddidd? D??
Sample Output122136
HintPermutation {signature} has the "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.
Authorhong, Qize
Asia Dalian Regional Contest
Test instructions: in all permutations of numbers 1 through n , the number of n-1 characters given by the question satisfies the question , if the first character is ' I ' represents the first of the permutations i-1 the number is less than the first I number of. If it is ' D ', then vice versa.
Idea: At first, there was no thought at all ...
In fact, to do DP words must first determine a good state transfer equation
State transfer equation: dp[i][j] Indicates the number of scenarios with J ending at the number of I, then you can get:
When s[i]= ' I ' or '? ' (indicates an increase), then Dp[i][j]+=dp[i-1][k] (1=<K<J)
When s[i]= ' D ' or '? ' (indicates reduction), then dp[i][j]+=dp[i-1][k] (I>K>=J)
But this time complexity is O (n^3), will time out Ah, so introduce sum[][] array to record the prefix, so that the time is reduced to O (n^2)
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <stdlib.h>6 using namespacestd;7 #defineN 10068 #defineMOD 10000000079 CharS[n];Ten intDp[n][n];//Dp[i][j] Indicates the number of sub-permutations that satisfy the condition in this arrangement where the first I digit ends with J. One intSum[n][n]; A intMain () - { - while(SCANF ("%s", s+2)!=EOF) the { - intN=strlen (s+2); -Memset (DP,0,sizeof(DP)); -memset (SUM,0,sizeof(sum)); +dp[1][1]=sum[1][1]=1; - for(intI=2; i<=n+1; i++) + { A for(intj=1; j<=i;j++) at { - if(s[i]=='I'|| s[i]=='?') - { - -dp[i][j]=dp[i][j]+sum[i-1][j-1]; -dp[i][j]%=MOD; in } - if(s[i]=='D'|| s[i]=='?') to { + -dp[i][j]=dp[i][j]+ (sum[i-1][i-1]-sum[i-1][j-1])%mod+MOD; thedp[i][j]%=MOD; * } $Sum[i][j]= (sum[i][j-1]+DP[I][J])%MOD;Panax Notoginseng } - the } + Aprintf"%d\n", sum[n+1][n+1]); the } + return 0; -}
View Code
HDU 4055 number String (DP)