给一个只含‘I','D','?'三种字符的字符串,I表示当前数字大于前面的数字,D表示当前的数字小于前面一位的数字,?表示当前位既可以小于又可以大于。
问1~n的排列中有多少个满足该字符串。
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.
#include <iostream> #include <cmath> #include <math.h> #include <vector> #include <cstdio> #include <cstring> #include <string> #include <string.h> #include <algorithm> using namespace std; #define ll long long const int mod = 1000000007; const int n = 1005; ll sum[n][n]; int main() { memset(sum,0,sizeof(sum)); string s; sum[0][1] = 1; while(cin>>s) { for(int i=1;i<=s.size();i++) { for(int j=1;j<=i+1;j++) { sum[i][j] = sum[i][j-1]; if(s[i-1] != 'D') sum[i][j] += sum[i-1][j-1]; if(s[i-1] != 'I') sum[i][j] += sum[i-1][i] -sum[i-1][j-1]+mod; sum[i][j] %= mod; } } cout<<sum[ s.size() ][ s.size() + 1 ]<<endl; } return 0; }