hdu4055(dp)

想了好久。。没想出来qaq

可以说是道好题,给处理全排列提供了一个不错的思路。。。orz雨姐姐还是强

难点在于避免取重复的数。。

设d[i][j]为取前i位,且只取前i个数,末位为j的方案数

那么加入一个j数的时候除了i本身,其他取法必然要与前面冲突。。

事实上这个冲突很好处理,把前i-1项大于等于j的加1,问题完美解决。。

然后就是随便转移。。

转移显然可以用前缀和把N^3降到N^2。。事实上还可以转化为2个dp状态的和,这个是窝想练的,所以用了这方法。。



/**
 *          ┏┓    ┏┓
 *          ┏┛┗━━━━━━━┛┗━━━┓
 *          ┃       ┃  
 *          ┃   ━    ┃
 *          ┃ >   < ┃
 *          ┃       ┃
 *          ┃... ⌒ ...  ┃
 *          ┃              ┃
 *          ┗━┓          ┏━┛
 *          ┃          ┃ Code is far away from bug with the animal protecting          
 *          ┃          ┃   神兽保佑,代码无bug
 *          ┃          ┃           
 *          ┃          ┃        
 *          ┃          ┃
 *          ┃          ┃           
 *          ┃          ┗━━━┓
 *          ┃              ┣┓
 *          ┃              ┏┛
 *          ┗┓┓┏━━━━━━━━┳┓┏┛
 *           ┃┫┫       ┃┫┫
 *           ┗┻┛       ┗┻┛
 */ 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<>1)
#define NM 1005
#define nm 5000005
#define pi 3.1415926535897931
const ll inf=1000000007;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}




ll d[NM][NM],ans;
int n;
char s[NM];
int main(){
    while(~scanf("%s",s+2)){
	n=strlen(s+2)+1;mem(d);
	d[1][1]=1;ans=0;
	inc(i,2,n)if(s[i]=='I')
	    inc(j,1,i)d[i][j]=(d[i][j-1]+d[i-1][j-1])%inf;
	else if(s[i]=='D')
	    dec(j,i,1)d[i][j]=(d[i][j+1]+d[i-1][j])%inf;
	else{
	    int t=0;
	    inc(j,1,i)t+=d[i-1][j],t%=inf;
	    inc(j,1,i)d[i][j]=t;
	}
	inc(i,1,n)ans+=d[n][i],ans%=inf;
	printf("%lld\n",ans);
	mem(s);
    }
    return 0;
}




Number String

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2551    Accepted Submission(s): 1248


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.
 

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.
 

Author
HONG, Qize
 

Source
2011 Asia Dalian Regional Contest
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   4054  4057  4053  4056  4059 
 

Statistic |  Submit |  Discuss | Note

你可能感兴趣的:(DP)