[背包DP] 背包总结+求最大转换序列
来源:互联网 发布:三星手机炒股软件 编辑:程序博客网 时间:2024/05/17 05:56
背包常规解法:
物品
0__1 __2 __3 __4 __5 __.. __ __ __ __ __ __ __ __
|__ __装
|__ __不装
dfs( int[] prices, int [] weights, int curW, int curP, int idx, int &maxN) { //不装 dfs(prices, weights, curW, curP, idx+1, maxN) //装 maxN = max( curP+ prices[idx] ) dfs(prices, weights, curW+weights[idx], curP+prices[idx], idx+1, maxN)}
能处理 2^idx < MAXINT 的情况
dp(int[] prices, int [] weights, int &maxP[ int maxW ] ){ maxP[0] = 0 repUp(i, 0, N-1) repDown(j, maxW, weights[i]) maxP[j] <= maxP[ j-weights[i] ] + prices[i] //试装i
能处理idx*maxW < MAXINT
http://www.cnblogs.com/yuris115/p/3537540.html
这道题,无重量限制求可达价值的背包DP, maxP换成bool t[ int MOD=77077 ],
for i=1 to N: for j=0 to MOD-1: tmp[ j*a[i]%MOD ] || = t[j] t = tmp
处理 idx*MOD < MAXINT
Topcoder TCHS SRM2 DIV 1 1000-point
Problem Statement
A spell is defined by its incantation, the word spoken when the spell is being cast. If you know the incantation of a spell, you can create counterspells in the following manner. First, you can optionally delete any number of characters from the original incantation. Note that empty incantations are not valid so you cannot delete all characters. Then, you can replace any number of the remaining characters according to a set of replacement rules. The replacement rules tell you which letters can be replaced, and the letters with which they can be replaced. For example, if the original incantation is "AA" and the allowed replacement is 'A' to 'Z', the following counterspells are possible: "AA", "A", "Z", "ZZ", "AZ", "ZA".The best counterspell is the one with the greatest power. The power of a spell is the product of all its character values modulo 77077, where the value for a character is its position in the alphabet (character 'A' has value 1, character 'B' value 2, character 'C' value 3, and so on). The best possible counterspell for "AA" in the example above is "ZZ", which has a power of 676.
Please note that if the allowed replacements are 'A' to 'B' and 'B' to 'C', the counterspell for "AB" is "BC" not "CC". You cannot change a character that has already been changed, even if it would lead to a more powerful spell.
You will be given a string spell, the incantation of the spell you are going to counter, and a string rules, the allowed replacements for letters. The first character in rules is the allowed replacement for the letter 'A', the second for 'B' and so on. The character '-' is used to denote a letter that cannot be replaced. Your program must return a string, the most powerful counterspell available. If multiple return values are possible, return the shortest among them. If a tie still exists return the lexicographically earliest.
Definition
Class:WizardingMethod:counterspellParameters:string, stringReturns:stringMethod signature:string counterspell(string spell, string rules)(be sure your method is public)Limits
Time limit (s):2.000Memory limit (MB):64Constraints
-spell will contain between 1 and 13 characters, inclusive.-spell will contain only uppercase letters ('A'-'Z').-rules will contain exactly 26 characters.-rules will contain only uppercase letters ('A'-'Z') and dashes ('-').Examples
0)"AA"
"Z-------------------------"
Returns: "ZZ"The example from the problem statement.1)
"AB"
"ZS------------------------"
Returns: "ZS"The possible counterspells are "AB", "A", "B", "Z", "S", "ZB", "AS" and "ZS".
"ZS" is the most powerful.2)
"ZZZZ"
"-------------------------Z"
Returns: "ZZZZ"3)
"ABCDE"
"ZYXXYXZZXYXXZZXZYYXZZZX---"
Returns: "ZXXE"4)
"ABCDEABCDEABC"
"ACBDESKADSLOEDDDASDBADEDAE"
Returns: "CCDECCECC"
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
这是一个牛人的代码,我重写了一遍。第一次发现原来DFS还能这样用,给我很大启发,还有一点就是数论部分我学的太烂了,最近还是要补补。当然,这里不止用DFS这一种办法,你可以把它展开……:-D
0 0
- [背包DP] 背包总结+求最大转换序列
- [hdu3732]dp-背包-转换
- 背包dp训练总结
- DP总结(1) 01背包 完全背包 多重背包
- dp之背包总结篇
- dp之背包总结篇
- dp求最大和序列
- 【DP(背包)】 hdu1864 最大报销额
- HDU5119【dp背包求方案数】
- 将求最大的连续组合值转换为背包问题
- DP背包
- 背包 dp
- 背包dp
- 背包dp
- DP-背包
- 背包dp
- 背包dp
- 背包dp
- 怎么查看我女朋友与别人之间的微信聊天记录信息
- 微信账号被封而且还是永远能解封吗
- chrome embedded framework: dialog based MFC browser
- WTA问题研究计划
- 如何别人可以查我手机的通话记录信息吗
- [背包DP] 背包总结+求最大转换序列
- jquery常用过滤选择器
- 【每日一算】开始
- Vim的Python编辑器详细配置过程
- http://www.rayfile.com/zh-cn/files/20bdf5b0-5ade-11e4-b4a4-0015c55db73d/c66184ab/
- linux中dc一个任意精度的计算器
- [数据结构]手写hashmap<string, node>
- fdggdsfdseewwewe
- linux-redhat5.6虚拟学习机安装.txt