DP具体解释见:

http://blog.csdn.net/liguan1/article/details/10468139

Derangement

Time Limit: 7000/7000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)

Total Submission(s): 846    Accepted Submission(s): 256

Problem Description
A derangement is a permutation such that none of the elements appear in their original position. For example, [5, 4, 1, 2, 3] is a derangement of [1, 2, 3, 4, 5]. Subtracting the original permutation from the derangement, we get the derangement difference [4,
2, -2, -2, -2], where none of its elements is zero. Taking the signs of these differences, we get the derangement sign [+, +, -, -, -]. Now given a derangement sign, how many derangements are there satisfying the given derangement sign?
 
Input
There are multiple test cases. Process to the End of File.

Each test case is a line of derangements sign whose length is between 1 and 20, inclusively.
 
Output
For each test case, output the number of derangements.
 
Sample Input
+-
++---
 
Sample Output
1
13
 
Author
Zejun Wu (watashi)
 
Source
 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; typedef long long int LL; LL dp[50][50];
char str[50]; int main()
{
while(cin>>str)
{
if(str[0]=='-')
{
puts("0"); continue;
}
memset(dp,0,sizeof(dp));
int n=strlen(str);
dp[1][1]=1;
for(int i=2;i<=n;i++)
{
for(int j=0;j<=i;j++)
{
if(str[i-1]=='+')
{
if(j) dp[i][j]+=dp[i-1][j-1];
dp[i][j]+=dp[i-1][j]*j;
}
else if(str[i-1]=='-')
{
dp[i][j]+=dp[i-1][j]*j;
dp[i][j]+=dp[i-1][j+1]*(j+1)*(j+1);
}
}
}
cout<<dp[n][0]<<endl;
}
return 0;
}

最新文章

  1. X509 证书生成
  2. 使用git将代码push到osc上
  3. python面向对象随笔
  4. 使用Subversion进行版本控制
  5. POJ 1724 Roads
  6. 1671. Anansi&#39;s Cobweb(并查集)
  7. Android打地鼠游戏源码带道具购买的Android游戏开发
  8. php 两个数组是否相同,并且输出全面的数据,相同的加一个字段标示
  9. 使用Sublime Text搭建python调试环境
  10. 在Centos 5.x或6.x上安装RHEL EPEL Repo
  11. ASP.NET MVC 项目直接预览PDF文件
  12. js拖拽效果详细讲解
  13. Linux 打包压缩与搜索命令
  14. vue.js+SSH添加和查询
  15. 事物的隔离级别与并发完美体现了cap理论(确保数据完整、安全、一致性,在此基础上实现高性能访问(鱼和熊掌不可兼得)
  16. ScrureCRT访问CentOS时出现乱码的解决办法
  17. Go语言【第十篇】:Go数据结构之:指针
  18. 对EasyDarwin开源项目2018的思考与2019发展的规划:继续站在巨人的肩膀引入更多巨人
  19. 进程 query foreach
  20. 第七章 Java中的13个原子操作类

热门文章

  1. x86 下的 struct 變數 member 擺放位置
  2. AC日记——[USACO11DEC]牧草种植Grass Planting 洛谷 P3038
  3. 神秘的FrontCache
  4. R语言table()函数
  5. 济南day3
  6. java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.pagehelper.Page pagehelper报错无法类型转化。
  7. Java多线程中Lock的使用
  8. 算法之美--3.2.3 KMP算法
  9. 一个Netfilter nf_conntrack流表查找的优化-为conntrack添加一个per cpu cache
  10. Hibernate中的session和load延迟载入矛盾问题,怎样解决?