日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【HDU - 4055】Number String(dp,思维)

發布時間:2023/12/10 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 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.

題目大意:

給你一個含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,思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。