SRM 586 Div II Level Three: StringWeightDiv2,Dynamic Programming or Math

题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12695

解析:https://apps.topcoder.com/wiki/display/tc/SRM+586


这道题目当时比赛的时候没有做出来,当时感觉是用dp做,不过还是太水了,完全不知道怎么搞,后来看别人代码发现都是基本都是用的排列组合原理做的,觉得自己真是太弱了,再看这题给出的解析,发现用dp其实也蛮简单,主要是要想到子问题该怎么划分,又怎样由子问题生成最后的解。

另外发现用递归来做dp比非递归的简单多了,用递归只需要按照正常的思路自顶向下,然后确定好递归结束条件就好了,用非递归要自底向上的搞,不太直观。

再接再励!

代码如下,给出了用数学排列组合及用dp两种方法解的代码:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <climits>
#include <cmath>

using namespace std;



/************** Program  Begin *********************/

static const int MOD = 1000000009;
long long dp[27][1001];
class StringWeightDiv2 {
public:
	// Math
	int countMinimums(int L) {
		long long res = 1;

		if (L <= 26) {
			int t = 26;
			for (int i = L; i > 0; i--) {
				res *= t;
				res %= MOD;
				--t;
			}
		} else {
			// 计算 26! * C(L-1, 25)
			res = 26;
			for (int i = L - 1; i > L - 26; i--) {
				res *= i;
				res %= MOD;
			}
		}

		return static_cast<int>(res);
	}

	// Dynamic Programming
	// a代表可以使用的字符的种类,L表示需要生成的字符串的长度
	long long onceConsecutive(int a, int L)
	{
		long long & res = dp[a][L];
		if (res == -1) {
			if (L == 0) {      // base case,L=0时,a=0, 1, a>1, 0
				res = (a == 0);
			} else if (a == 1) {
				res = 1;
			} 
			else {
				// calculate the sum. (We need to use modular arithmetic)
				res = 0;
				// 做了一点改动,改为了i <= L-a+1而不是原来的i <= L,保证了a>=L
				for (int i = 1; i <= L-a+1; i++) {
					res += (a * onceConsecutive(a-1, L-i) ) % MOD;
				}
				res %= MOD;
			}
		} 
		return res;
	}

	int countMinimums2(int L)
	{
		memset(dp, -1, sizeof(dp));
		long long res = 1;
		if ( L <= 26 ) {
			// The simple case:
			//26 * 25 * 24 * ...
			for (int i=0; i < L; i++) {
				res = (res * (26 - i)) % MOD;
			}
		} else {
			// each letter must appear at least once, all instances
			// of each letter must be consecutive.
			res = onceConsecutive(26, L); //Use the recurrence relation
		}

		return res;
	}
};

/************** Program End ************************/


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值