Hdu4055 Number String
阿新 • • 發佈:2017-06-08
fin 含義 with only 表示 eat key 沒有 其中
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.
Number String
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2007 Accepted Submission(s):
973
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.
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‘.
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
Source 2011 Asia Dalian Regional Contest
/* dp方程的設定比較顯然,dp[i][j]表示選了i個元素,最後一個是j的方案數。 但是在狀態轉移的時候,我們不得不考慮前面選了什麽,也就是狀態的設定是有後效性的, 所以考慮給狀態再添一層含義:必須選前i個元素。 那麽這樣豈不是每次只能選i嗎?那麽第二維豈不是沒有用了? 所以我們考慮用j把i替換出來,那麽在狀態轉移的時候就需要考慮放入i時,怎麽替換能使原來的大小順序保持不變。 將dp[i-1][j]的i-1個數的序列中 ≥j 的數都加1,這樣i-1變成了i,j變成了j+1,而j自然就補在後面了。 處理I:dp[i][j] = Σdp[i-1][x],其中1≤x≤j-1,可進一步簡化,dp[i][j] = dp[i][j-1]+dp[i-1][j-1] 處理D:dp[i][j] = Σdp[i-1][x],其中j≤x≤i-1,可進一步簡化,dp[i][j] = dp[i-1][j+1]+dp[i-1][j] 處理?:dp[i][j] = Σdp[i-1][x],其中1≤x≤i-1 */ #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define maxn 1010 #define mod 1000000007 char s[maxn]; int dp[maxn][maxn]; int main(){ while(scanf("%s",s+1)!=EOF){ int n=strlen(s+1);n++; int ans=0; memset(dp,0,sizeof(dp)); dp[1][1]=1; for(int i=2;i<=n;i++){ if(s[i-1]==‘I‘) for(int j=2;j<=i;j++) dp[i][j]=(dp[i][j-1]+dp[i-1][j-1])%mod; else if(s[i-1]==‘D‘) for(int j=i-1;j>=1;j--) dp[i][j]=(dp[i][j+1]+dp[i-1][j])%mod; else { int sum=0; for(int j=1;j<i;j++)sum=(sum+dp[i-1][j])%mod; for(int j=1;j<=i;j++)dp[i][j]=sum; } } for(int i=1;i<=n;i++)ans=(ans+dp[n][i])%mod; printf("%d\n",ans); } }
Hdu4055 Number String