题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4055
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.
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'.
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 6HintPermutation {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.http://acm.hdu.edu.cn/showproblem.php?pid=4055
Problem DescriptionThe 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.
InputEach 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'.
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 InputII ID DI DD ?D ??
Sample Output1 2 2 1 3 6HintPermutation {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./*题意: 给你一个字符串s,s[i] = 'D'表示排列中a[i] > a[i+1],s[i] = 'I'表示排列中a[i] < a[i+1]。 比如排列 {3, 1, 2, 7, 4, 6, 5} 表示为字符串 DIIDID。 解题思路: 很巧妙的DP做法,dp[i][j]表示前i个满足字符串条件的结尾为j的 i 的排列,注意是i的排列,前面并没有数大于i。那又是如何往下递推呢? 如果s[i - 1]是' I ',那么dp[i][j] = dp[i-1][j-1] + dp[i-1][j-2] + .. + dp[i-1][1] 如果s[i - 1]是‘D’,那么dp[i][j] = dp[i-1][j] + dp[i-1][j+1] + ... + dp[i-1][i],因为要令当前位为j, 如果前面出现过j,就令前面的所有大于等于j的数+1,就能构造出新的排列了。比如 {1, 3, 5, 2, 4},要在第六位插入3,令 >= 3的数都+1,于是就构造出新的 排列{1, 4, 6, 2, 5, 3}。 然后代码的话处理出前缀和sum[i][j],就不用dp[i][j]了。感觉还是很巧妙的,好题! */ #include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; const int mod = 1000000007; const int maxn = 1000 + 10; int dp[maxn][maxn];//dp[i][j] 代表长度为i以j结尾的字段满足条件的有几个 int sum[maxn][maxn];//sum[i-1][j-1] 代表 dp[i-1][j-1] + dp[i-1][j-2] + .. + dp[i-1][1] //用前缀和优化的话能将时间复杂度降到0(N2) char str[maxn];//保存序列 int main() { dp[1][1] = sum[1][1] = 1; while(~scanf("%s", str+1)){ int n = strlen(str+1) + 1;//计算出序列最大的数是n for(int i = 2 ; i <= n ; i++){//长度为1的已经赋值完毕,下面从长度为2的开始赋值 for(int j = 1 ; j <= i ; j++){//因为当前长度为i 所以j最大为i if(str[i-1] == 'I') dp[i][j] = sum[i-1][j-1]; else if(str[i-1] == 'D') dp[i][j] = (sum[i-1][i-1] - sum[i-1][j-1] + mod)%mod;//这里必须加mod 但是为什么呢?笔者还没想明白 else dp[i][j] = sum[i-1][i-1]; sum[i][j] = (sum[i][j-1] + dp[i][j]) % mod; }//for(int j = 1 ; j <= i ; j++) }//for(int i = 2 ; i <= n ; i++) printf("%d\n", sum[n][n]); }//while return 0; }//int main