[背包DP] 背包总结+求最大转换序列
2014-10-24 02:10
405 查看
背包常规解法:
物品
0__1 __2 __3 __4 __5 __.. __ __ __ __ __ __ __ __
|__ __装
|__ __不装
能处理 2^idx < MAXINT 的情况
能处理idx*maxW < MAXINT
http://www.cnblogs.com/yuris115/p/3537540.html
这道题,无重量限制求可达价值的背包DP, maxP换成bool t[ int MOD=77077 ],
处理 idx*MOD < MAXINT
Topcoder TCHS SRM2 DIV 1 1000-point
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__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 | ||||||||||||
| ||||||||||||
Limits | ||||||||||||
| ||||||||||||
Constraints | ||||||||||||
- | 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) | ||||||||||||
| ||||||||||||
1) | ||||||||||||
| ||||||||||||
2) | ||||||||||||
| ||||||||||||
3) | ||||||||||||
| ||||||||||||
4) | ||||||||||||
|
(c)2003, TopCoder, Inc. All rights reserved.
这是一个牛人的代码,我重写了一遍。第一次发现原来DFS还能这样用,给我很大启发,还有一点就是数论部分我学的太烂了,最近还是要补补。当然,这里不止用DFS这一种办法,你可以把它展开……:-D
相关文章推荐
- 0-1背包问题;动态规划;时间复杂度O(n方);给出最大价值与解得情况;内有动态规划思路总结;
- HDU 1231 最大连续子序列 dp
- HDU 1231 最大连续子序列(DP)
- 将求最大的连续组合值转换为背包问题
- hdu——1003(dp之最大子序列和 )
- pku 2479 最大连续和的两序列 dp
- 最大连续子序列 简单dp
- dp求最大和序列
- ZOJ1733 | | HDU1159简单的DP求两个字符串最大子序列的长度,没啥好说的,照着书上敲得。
- poj 1050 To the Max 二维矩阵转换一维求子序列最大值
- hdu 1024 Max Sum Plus Plus m段连续子序列最大和 dp
- hdu 1231 最大连续子序列(DP)
- hdu 1003(最大子序列 DP)
- 杭电hdu 1231 最大连续子序列 dp
- POJ1887 Testing the CATCHER 最大下降子序列 DP
- POJ 2479 + POJ 2593(DP 最大双子序列 ^_^)
- ZOJ 1108 FatMouse's Speed【DP】【最大上升子序列】
- hdu 1231 最大连续子序列和( DP )
- 【算法总结-DP】求子数组的最大和
- hdoj最大连续子序列 (简单dp)