Number String
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1016 Accepted Submission(s): 440
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'.
ID
DI
DD
?D
??
2
2
1
3
6
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<stdio.h>
#include<string.h>
using namespace std;
const int maxn=1010;
const int mod=1000000007;
char st[maxn];
int dp[2][maxn],sum[2][maxn]; int main()
{
int i,j,len,v,u; sum[1][0]=sum[0][0]=0;
while(~scanf("%s",st+2))
{
v=0;
len=strlen(st+2);
dp[0][1]=sum[0][1]=1;
for(i=2;i<=len+1;i++)//第一个数前没有数所以从2开始。一直到len+1
{
u=v^1;
for(j=1;j<=i;j++)
{
if(st[i]=='I')
dp[u][j]=sum[v][j-1];
else if(st[i]=='D')
dp[u][j]=(sum[v][i-1]-sum[v][j-1]+mod)%mod;//由于取模可能使大的变小
else
dp[u][j]=sum[v][i-1];
sum[u][j]=(dp[u][j]+sum[u][j-1])%mod;
//printf("sum[%d][%d]:%d\n",i,j,sum[v^1][j]);
}
v=u;
}
printf("%d\n",sum[v][len+1]);
}
return 0;
}