【HDU - 4055】Number String(dp,思维)
題干:
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 6Hint
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.題目大意:
給你一個含n個字符的字符串,字符為'D'時表示小于號,字符為“I”時表示大于號,字符為“?”時表示大小于都可以。比如排列 {3, 1, 2, 7, 4, 6, 5} 表示為字符串 DIIDID。任務是計算所有能產生給定字符串的序列數量,每個序列含n+1個數字,分別為1~n+1,即從1開始且不重復。
一句話題意:給一個序列相鄰元素各個上升下降情況('I'上升'D'下降'?'隨便),問有幾種滿足的排列。例:ID? 答:2 (231和132)
解題報告:
因為最終是一個排列,觀察性質,n個數1~n,如果是一個排列a的話,你去掉a[n]之后,并且讓所有大于a[n]的數都-1,會構成一個新的排列。所以定義狀態dp[i][j]為構造了i個數并且是i個數的排列,并且最后一個數是j的方案數。
轉移的時候,dp[i][j],如果字符串s[i]=='I'說明上一個字符可以任意取1~i-1;
如果s[i]=='D',同理,我可以任選一個比較小的并且使得比他大的增加1即可。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 1005 + 5; const ll mod = 1e9 + 7; ll dp[MAX][MAX],sum[MAX]; char s[MAX]; int main() {while(~scanf("%s",s+1)) {int n = strlen(s+1);for(int i = 0; i<=n+1; i++) for(int j = 0; j<=n+1; j++) dp[i][j] = 0;dp[0][1]=1;for(int i = 1; i<=n; i++) {for(int j = 1; j<=i+1; j++) sum[j] = sum[j-1] + dp[i-1][j];for(int j = 1; j<=i+1; j++) {if(s[i] == 'I') dp[i][j] = sum[j-1];else if(s[i] == 'D') dp[i][j] = sum[i]-sum[j-1];else dp[i][j]=sum[i];dp[i][j]%=mod;}}ll ans = 0;for(int i = 1; i<=n+1; i++) ans = (ans + dp[n][i]) % mod; printf("%lld\n",ans % mod);}return 0 ; }?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的【HDU - 4055】Number String(dp,思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 每月35元!B站电视大会员更名超级大会员
- 下一篇: 【牛客 - 371牛客OI周赛7-提高组