﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Tanky Woo的程序人生</title><link>http://www.cppblog.com/tanky-woo/</link><description>追逐C++的强大，追寻算法的内涵</description><language>zh-cn</language><lastBuildDate>Thu, 23 Apr 2026 04:11:34 GMT</lastBuildDate><pubDate>Thu, 23 Apr 2026 04:11:34 GMT</pubDate><ttl>60</ttl><item><title>《算法导论》学习总结 — 21.第16章 贪心算法(1) 基础入门1</title><link>http://www.cppblog.com/tanky-woo/archive/2011/06/14/148626.html</link><dc:creator>Tanky Woo</dc:creator><author>Tanky Woo</author><pubDate>Tue, 14 Jun 2011 05:17:00 GMT</pubDate><guid>http://www.cppblog.com/tanky-woo/archive/2011/06/14/148626.html</guid><wfw:comment>http://www.cppblog.com/tanky-woo/comments/148626.html</wfw:comment><comments>http://www.cppblog.com/tanky-woo/archive/2011/06/14/148626.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/tanky-woo/comments/commentRss/148626.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tanky-woo/services/trackbacks/148626.html</trackback:ping><description><![CDATA[<div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:01ce21cd-9402-48f7-997e-0260405a89a9" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 12px; color: rgb(51,51,51); line-height: 18px; font-family: 'Lucida Grande', Arial, Helvetica, sans-serif"> 
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">建议先看看前言：<a style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/?p=2298">http://www.wutianqi.com/?p=2298</a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">连载总目录：<a style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/?p=2403">http://www.wutianqi.com/?p=2403</a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">说到贪心算法，避免不了于DP对比，所以前面的DP要了解。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><span style="color: rgb(0,0,255)">贪心算法是使所做的选择看起来都是当前最佳的，期望通过所做的局部最优选择来产生一个全局最优解。</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">依然和上一章总结DP一样，我先给出一个最容易入门的例子，来看看神马是贪心？(是人就会贪心，这个算法很人性化啊</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">=。=)</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">一个最简单的例子：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">部分背包问题：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">有N个物品，第i个物品价值vi，重wi，现在你有一个可以装W 磅的包，你可以选择带走每个物品的全部或一部分，求如何选择可以使背包所装的价值最大？(<span style="color: rgb(0,0,255)">这个是不是和前面DP中讲的01背包很像？认真看清楚两者题目的不同</span>！)</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">假如有三种物品，背包可装50磅的物品，物品1重10磅，价值60元；物品2重20磅，价值100元；物品3重30磅，价值120元。因此，既然可以选择只装一部分，我们可以算出每种物品的单位价值，物品1是每磅6元，物品2是美邦5元，物品3是每磅4元。按照贪心策略，应该现状物品1，如果装完物品1背包还有空间，再装物品2&#8230;&#8230;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><a class="highslide-image" style="cursor: url(http://www.wutianqi.com/wp-content/plugins/auto-highslide/highslide/graphics/zoomin.cur), pointer; color: rgb(49,52,40); text-decoration: none; outline-style: none; outline-width: initial; outline-color: initial" onclick="return hs.expand(this);" href="http://www.wutianqi.com/wp-content/uploads/2011/06/16_2.png"><img title="16_2" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="252" alt="16_2" src="http://www.wutianqi.com/wp-content/uploads/2011/06/16_2_thumb.png" width="295" border="0" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">最后的结果是：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><a class="highslide-image" style="cursor: url(http://www.wutianqi.com/wp-content/plugins/auto-highslide/highslide/graphics/zoomin.cur), pointer; color: rgb(49,52,40); text-decoration: none; outline-style: none; outline-width: initial; outline-color: initial" onclick="return hs.expand(this);" href="http://www.wutianqi.com/wp-content/uploads/2011/06/16_3.png"><img title="16_3" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="243" alt="16_3" src="http://www.wutianqi.com/wp-content/uploads/2011/06/16_3_thumb.png" width="110" border="0" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">而如果按01背包，则结果是：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><a class="highslide-image" style="cursor: url(http://www.wutianqi.com/wp-content/plugins/auto-highslide/highslide/graphics/zoomin.cur), pointer; color: rgb(49,52,40); text-decoration: none; outline-style: none; outline-width: initial; outline-color: initial" onclick="return hs.expand(this);" href="http://www.wutianqi.com/wp-content/uploads/2011/06/16_4.png"><img title="16_4" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="243" alt="16_4" src="http://www.wutianqi.com/wp-content/uploads/2011/06/16_4_thumb.png" width="346" border="0" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">有兴趣的可以拿我那01背包的程序去验证这个结果。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">下面是一个部分背包的小程序：</p>
<div class="wp_syntax" style="border-right: silver 1px solid; border-top: silver 1px solid; overflow-y: hidden; overflow-x: auto; margin: 0px 0px 1.5em; border-left: silver 1px solid; width: 618px; color: rgb(17,0,0); border-bottom: silver 1px solid; background-color: rgb(249,249,249)">
<table style="border-right: rgb(204,204,204) 1px solid; border-top: rgb(204,204,204) 1px solid; border-left: rgb(204,204,204) 1px solid; border-bottom: rgb(204,204,204) 1px solid; border-collapse: collapse; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px">
<tbody>
<tr>
<td class="line_numbers" style="border-right: rgb(204,204,204) 1px solid; padding-right: 4px; border-top: rgb(204,204,204) 1px solid; overflow-y: visible; padding-left: 4px; overflow-x: visible; padding-bottom: 2px; vertical-align: top; border-left: rgb(204,204,204) 1px solid; color: gray; padding-top: 2px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(221,238,255); text-align: right; background-origin: initial; background-clip: initial"><pre style="clear: none; overflow-y: visible; font-size: 12px; float: none; overflow-x: visible; margin: 0px; width: auto; line-height: 1.333; white-space: pre">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td>
<td class="code" style="border-right: rgb(204,204,204) 1px solid; padding-right: 4px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; padding-bottom: 2px; vertical-align: top; border-left: rgb(204,204,204) 1px solid; padding-top: 2px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(240,240,240); background-origin: initial; background-clip: initial"><pre class="cpp" style="clear: none; overflow-y: visible; font-size: 12px; float: none; overflow-x: visible; margin: 0px; width: auto; line-height: 1.333; font-family: monospace; white-space: pre">&nbsp;
<span style="color: rgb(51,153,0)">#include &lt;iostream&gt;</span>
<span style="color: rgb(51,153,0)">#include &lt;algorithm&gt;</span>
<span style="color: rgb(0,0,255)">using</span> <span style="color: rgb(0,0,255)">namespace</span> std<span style="color: rgb(0,128,128)">;</span>
&nbsp;
<span style="color: rgb(0,0,255)">typedef</span> <span style="color: rgb(0,0,255)">struct</span> Thing<span style="color: rgb(0,128,0)">{</span>
	<span style="color: rgb(0,0,255)">double</span> v<span style="color: rgb(0,128,128)">;</span>     <span style="color: rgb(102,102,102)">// value</span>
	<span style="color: rgb(0,0,255)">double</span> w<span style="color: rgb(0,128,128)">;</span>     <span style="color: rgb(102,102,102)">// weight</span>
<span style="color: rgb(0,128,0)">}</span>Thing<span style="color: rgb(0,128,128)">;</span>
&nbsp;
Thing arr<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">100</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
<span style="color: rgb(0,0,255)">int</span> n<span style="color: rgb(0,128,128)">;</span>
<span style="color: rgb(0,0,255)">double</span> W<span style="color: rgb(0,128,128)">;</span>
&nbsp;
&nbsp;
<span style="color: rgb(0,0,255)">bool</span> cmp<span style="color: rgb(0,128,0)">(</span>Thing a, Thing b<span style="color: rgb(0,128,0)">)</span>
<span style="color: rgb(0,128,0)">{</span>
	<span style="color: rgb(0,0,255)">return</span> a.<span style="color: rgb(0,119,136)">v</span><span style="color: rgb(0,0,64)">/</span>a.<span style="color: rgb(0,119,136)">w</span> <span style="color: rgb(0,0,128)">&gt;</span> b.<span style="color: rgb(0,119,136)">v</span><span style="color: rgb(0,0,64)">/</span>b.<span style="color: rgb(0,119,136)">w</span><span style="color: rgb(0,128,128)">;</span>
<span style="color: rgb(0,128,0)">}</span>
&nbsp;
&nbsp;
<span style="color: rgb(0,0,255)">int</span> main<span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,128,0)">)</span>
<span style="color: rgb(0,128,0)">{</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"输入物品个数: "</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cin</span> <span style="color: rgb(0,0,128)">&gt;&gt;</span> n<span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"输入背包可载重量: "</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cin</span> <span style="color: rgb(0,0,128)">&gt;&gt;</span> W<span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"输入"</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> n <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"个物品的价值和重量:"</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> endl<span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> i<span style="color: rgb(0,0,128)">=</span><span style="color: rgb(0,0,221)">0</span><span style="color: rgb(0,128,128)">;</span> i<span style="color: rgb(0,0,128)">&lt;</span>n<span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">++</span>i<span style="color: rgb(0,128,0)">)</span>
		<span style="color: rgb(0,0,221)">cin</span> <span style="color: rgb(0,0,128)">&gt;&gt;</span> arr<span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,128,0)">]</span>.<span style="color: rgb(0,119,136)">v</span> <span style="color: rgb(0,0,128)">&gt;&gt;</span> arr<span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,128,0)">]</span>.<span style="color: rgb(0,119,136)">w</span><span style="color: rgb(0,128,128)">;</span>
	sort<span style="color: rgb(0,128,0)">(</span>arr, arr<span style="color: rgb(0,0,64)">+</span>n, cmp<span style="color: rgb(0,128,0)">)</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">int</span> k <span style="color: rgb(0,0,128)">=</span> <span style="color: rgb(0,0,221)">0</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">double</span> value <span style="color: rgb(0,0,128)">=</span> <span style="color: rgb(0,0,221)">0</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">while</span><span style="color: rgb(0,128,0)">(</span>W<span style="color: rgb(0,128,0)">)</span>
	<span style="color: rgb(0,128,0)">{</span>
		<span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,128,0)">(</span>W <span style="color: rgb(0,0,128)">&gt;=</span> arr<span style="color: rgb(0,128,0)">[</span>k<span style="color: rgb(0,128,0)">]</span>.<span style="color: rgb(0,119,136)">w</span><span style="color: rgb(0,128,0)">)</span>
		<span style="color: rgb(0,128,0)">{</span>
			W <span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,128)">=</span> arr<span style="color: rgb(0,128,0)">[</span>k<span style="color: rgb(0,128,0)">]</span>.<span style="color: rgb(0,119,136)">w</span><span style="color: rgb(0,128,128)">;</span>
			value <span style="color: rgb(0,0,64)">+</span><span style="color: rgb(0,0,128)">=</span> arr<span style="color: rgb(0,128,0)">[</span>k<span style="color: rgb(0,128,0)">]</span>.<span style="color: rgb(0,119,136)">v</span><span style="color: rgb(0,128,128)">;</span>
		<span style="color: rgb(0,128,0)">}</span>
		<span style="color: rgb(0,0,255)">else</span>
		<span style="color: rgb(0,128,0)">{</span>
			value <span style="color: rgb(0,0,64)">+</span><span style="color: rgb(0,0,128)">=</span> W <span style="color: rgb(0,0,64)">*</span> arr<span style="color: rgb(0,128,0)">[</span>k<span style="color: rgb(0,128,0)">]</span>.<span style="color: rgb(0,119,136)">v</span> <span style="color: rgb(0,0,64)">/</span> arr<span style="color: rgb(0,128,0)">[</span>k<span style="color: rgb(0,128,0)">]</span>.<span style="color: rgb(0,119,136)">w</span><span style="color: rgb(0,128,128)">;</span>
			W <span style="color: rgb(0,0,128)">=</span> <span style="color: rgb(0,0,221)">0</span><span style="color: rgb(0,128,128)">;</span>
		<span style="color: rgb(0,128,0)">}</span>
		<span style="color: rgb(0,0,64)">++</span>k<span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,128,0)">}</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"最大价值是: "</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> value <span style="color: rgb(0,0,128)">&lt;&lt;</span> endl<span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(0,0,221)">0</span><span style="color: rgb(0,128,128)">;</span>
<span style="color: rgb(0,128,0)">}</span></pre></td></tr></tbody></table></div>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">结果如图：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><a class="highslide-image" style="cursor: url(http://www.wutianqi.com/wp-content/plugins/auto-highslide/highslide/graphics/zoomin.cur), pointer; color: rgb(49,52,40); text-decoration: none; outline-style: none; outline-width: initial; outline-color: initial" onclick="return hs.expand(this);" href="http://www.wutianqi.com/wp-content/uploads/2011/06/16_1.png"><img title="16_1" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="243" alt="16_1" src="http://www.wutianqi.com/wp-content/uploads/2011/06/16_1_thumb.png" width="310" border="0" /></a></p>
<div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:01ce21cd-9402-48f7-997e-0260405a89a9" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">Tanky Woo 标签:<span class="Apple-converted-space">&nbsp;</span><a style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/Tanky+Woo" rel="tag">Tanky Woo</a>，<a style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/%e8%b4%aa%e5%bf%83%e7%ae%97%e6%b3%95" rel="tag">贪心算法</a>，<a style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/%e7%ae%97%e6%b3%95%e5%af%bc%e8%ae%ba" rel="tag">算法导论</a></div></span></span><a href="http://www.wutianqi.com/%e7%ae%97%e6%b3%95%e5%af%bc%e8%ae%ba" rel="tag"><font color="#313428"></font></a></div><img src ="http://www.cppblog.com/tanky-woo/aggbug/148626.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tanky-woo/" target="_blank">Tanky Woo</a> 2011-06-14 13:17 <a href="http://www.cppblog.com/tanky-woo/archive/2011/06/14/148626.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《算法导论》学习总结 — 20.第15章 动态规划(5) 分析几道DP题</title><link>http://www.cppblog.com/tanky-woo/archive/2011/06/12/148521.html</link><dc:creator>Tanky Woo</dc:creator><author>Tanky Woo</author><pubDate>Sun, 12 Jun 2011 01:32:00 GMT</pubDate><guid>http://www.cppblog.com/tanky-woo/archive/2011/06/12/148521.html</guid><wfw:comment>http://www.cppblog.com/tanky-woo/comments/148521.html</wfw:comment><comments>http://www.cppblog.com/tanky-woo/archive/2011/06/12/148521.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/tanky-woo/comments/commentRss/148521.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tanky-woo/services/trackbacks/148521.html</trackback:ping><description><![CDATA[<p>看了下上一篇的日期，是5.16号，已经有20天没写了，郁闷啊，不过最近的考试终于结束了，接下来就是18号的六级和后面的三门考试，这几天可以安心研究算法了，开心啊。<br /></p>
<p><br />建议先看看前言：<a title="http://www.wutianqi.com/?p=2298" href="http://www.wutianqi.com/?p=2298"><font color="#313428">http://www.wutianqi.com/?p=2298</font></a></p>
<p>连载总目录：<a title="http://www.wutianqi.com/?p=2403" href="http://www.wutianqi.com/?p=2403"><font color="#313428">http://www.wutianqi.com/?p=2403</font></a></p>
<p>这一章，我准备把HDOJ上找几道经典的DP题目给大家分析一下。</p>
<p>1.HDOJ 1257 最少拦截系统</p>
<p>题目链接：<a title="http://acm.hdu.edu.cn/showproblem.php?pid=1257" href="http://acm.hdu.edu.cn/showproblem.php?pid=1257"><font color="#313428">http://acm.hdu.edu.cn/showproblem.php?pid=1257</font></a></p>
<p>分析+代码：<a title="http://www.wutianqi.com/?p=1841" href="http://www.wutianqi.com/?p=1841"><font color="#313428">http://www.wutianqi.com/?p=1841</font></a></p>
<p>经典的LIS，DP入门级题目。<br /></p>
<p><br />2.HDOJ 1176 免费馅饼</p>
<p>题目链接：<a title="http://acm.hdu.edu.cn/showproblem.php?pid=1176" href="http://acm.hdu.edu.cn/showproblem.php?pid=1176"><font color="#313428">http://acm.hdu.edu.cn/showproblem.php?pid=1176</font></a></p>
<p>分析+代码：<a title="http://www.wutianqi.com/?p=2457" href="http://www.wutianqi.com/?p=2457"><font color="#313428">http://www.wutianqi.com/?p=2457</font></a></p>
<p>这一题的经典在于由直线向数塔的转化，图形分析在上面的连接中给出。<br /></p>
<p><br />3.HDOJ 1160 FatMouse&#8217;s Speed</p>
<p>题目链接：<a title="http://acm.hdu.edu.cn/showproblem.php?pid=1160" href="http://acm.hdu.edu.cn/showproblem.php?pid=1160"><font color="#313428">http://acm.hdu.edu.cn/showproblem.php?pid=1160</font></a></p>
<p>分析+代码：<a title="http://www.wutianqi.com/?p=2290" href="http://www.wutianqi.com/?p=2290"><font color="#313428">http://www.wutianqi.com/?p=2290</font></a></p>
<p>最长上升子序列的问题，题目比较新颖，这里可以感受到我在前面写的，DP和BFS,递归和DFS的关系。<br /></p>
<p><br />4.HDOJ 1080 Human Gene Functions</p>
<p>题目链接：<a title="http://acm.hdu.edu.cn/showproblem.php?pid=1080" href="http://acm.hdu.edu.cn/showproblem.php?pid=1080"><font color="#313428">http://acm.hdu.edu.cn/showproblem.php?pid=1080</font></a></p>
<p>分析+代码：<a title="http://www.wutianqi.com/?p=2413" href="http://www.wutianqi.com/?p=2413"><font color="#313428">http://www.wutianqi.com/?p=2413</font></a></p>
<p>这题不知道该怎么说，反正个人做了后第一感觉就是经典！特此推荐。</p>
<p>另外，DP的题目个人觉得做多了就有感觉了，以前转载过牛人总结的HDOJ上46道DP题目，嘿嘿，给出链接：</p>
<p><a title="http://www.wutianqi.com/?p=550" href="http://www.wutianqi.com/?p=550"><font color="#313428">http://www.wutianqi.com/?p=550</font></a></p>
<p>谁要全部做完了记得告诉我一声，我要膜拜一下。</p>
<p>好了，DP到此结束，接下来的将是贪心算法了~~~<br /></p>
<div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ccf393c6-77f1-45d7-88d0-40ad75c6e580" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><br />Tanky Woo 标签: <a href="http://www.wutianqi.com/DP" rel="tag"><font color="#313428">DP</font></a>，<a href="http://www.wutianqi.com/%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92" rel="tag"><font color="#313428">动态规划</font></a>，<a href="http://www.wutianqi.com/Tanky+Woo" rel="tag"><font color="#313428">Tanky Woo</font></a></div>
<div class="wlWriterEditableSmartContent" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<p>在我独立博客上的原文：<a href="http://www.wutianqi.com/?p=2559">http://www.wutianqi.com/?p=2559</a></p>
<p>欢迎大家互相学习，互相进步！</p></div>
<p><script type="text/javascript">
if ($ != jQuery) {
	$ = jQuery.noConflict();
}
var isLogined = true;
var cb_blogId = 72950;
var cb_entryId = 2059116;
var cb_blogApp = "tanky_woo";
var cb_blogUserGuid = "99a47d77-f68b-df11-ba8f-001cf0cd104b";
var cb_entryCreatedDate = '2011/5/26 18:55:00';
</script></p><img src ="http://www.cppblog.com/tanky-woo/aggbug/148521.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tanky-woo/" target="_blank">Tanky Woo</a> 2011-06-12 09:32 <a href="http://www.cppblog.com/tanky-woo/archive/2011/06/12/148521.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>谈一下ACM的入门书籍及方法</title><link>http://www.cppblog.com/tanky-woo/archive/2011/06/08/148295.html</link><dc:creator>Tanky Woo</dc:creator><author>Tanky Woo</author><pubDate>Wed, 08 Jun 2011 12:08:00 GMT</pubDate><guid>http://www.cppblog.com/tanky-woo/archive/2011/06/08/148295.html</guid><wfw:comment>http://www.cppblog.com/tanky-woo/comments/148295.html</wfw:comment><comments>http://www.cppblog.com/tanky-woo/archive/2011/06/08/148295.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/tanky-woo/comments/commentRss/148295.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tanky-woo/services/trackbacks/148295.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 12px; color: rgb(51,51,51); line-height: 18px; font-family: 'Lucida Grande', Arial, Helvetica, sans-serif"> 
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">首先说一下，ACM的入门方法多种多样，大部分人还是跟着学校一起参加集训，所以我这里主要是想对那些准备ACM入门的业余的朋友谈的。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><strong><span style="font-size: x-large; color: rgb(255,0,0)">入门书籍</span></strong>：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">首先推荐一些ACM的书籍：<br />(以下我都会给出在当当网的页面，方便大家直接购买，以下排名不分先后)</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">1.《程序设计导引及在线实践》<br /><a style="color: rgb(49,52,40); text-decoration: none" href="http://product.dangdang.com/product.aspx?product_id=20051430&amp;ref=search-1-pub">http://product.dangdang.com/product.aspx?product_id=20051430&amp;ref=search-1-pub</a><br />这是我的第一本入门书，这本书是配套北大的百炼习题，注意不是POJ，貌似是北大内部测试用的，不过也是对外开放的，去年好像百炼变化过，所以[u]不知道这本书还适不适合那个新的百炼系统[/u]。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">2.《算法竞赛入门经典》<br /><a style="color: rgb(49,52,40); text-decoration: none" href="http://product.dangdang.com/product.aspx?product_id=20724029&amp;ref=search-1-pub">http://product.dangdang.com/product.aspx?product_id=20724029&amp;ref=search-1-pub</a><br />这本书没话说，刘汝佳的白书，经典的算法入门书籍。[b]强烈推荐[/b]！</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">3.《算法艺术与信息学竞赛》<br /><a style="color: rgb(49,52,40); text-decoration: none" href="http://product.dangdang.com/product.aspx?product_id=8811386&amp;ref=search-1-pub">http://product.dangdang.com/product.aspx?product_id=8811386&amp;ref=search-1-pub</a><br />刘汝佳的黑书，难度较深，题目基本来至Uva，我是看了前面以部分，后面就没咋看了。。。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">4.《算法导论》<br /><a style="color: rgb(49,52,40); text-decoration: none" href="http://product.dangdang.com/product.aspx?product_id=9211884&amp;ref=search-1-pub">http://product.dangdang.com/product.aspx?product_id=9211884&amp;ref=search-1-pub</a><br />经典的书籍是不需要解释的。<br />这是我曾经上传过的英文版CHM算法导论，可以下载了看看：<br /><a style="color: rgb(49,52,40); text-decoration: none" href="http://www.cppleyuan.com/viewthread.php?tid=5130&amp;highlight=%E7%AE%97%E6%B3%95%E5%AF%BC%E8%AE%BA">http://www.cppleyuan.com/viewthread.php?tid=5130&amp;highlight=%E7%AE%97%E6%B3%95%E5%AF%BC%E8%AE%BA</a><br />我最近也在写算法导论的读书总结，欢迎大家探讨：<br /><a style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/?p=2403">http://www.wutianqi.com/?p=2403</a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">5.《编程之美》<br /><a style="color: rgb(49,52,40); text-decoration: none" href="http://product.dangdang.com/product.aspx?product_id=20170952&amp;ref=search-1-pub">http://product.dangdang.com/product.aspx?product_id=20170952&amp;ref=search-1-pub</a><br />挺有意思的，不能作为一个算法的全面书籍，而是作为一本拓宽思维的书籍，有兴趣的建议要看看。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">6.《计算机程序设计艺术》<br /><a style="color: rgb(49,52,40); text-decoration: none" href="http://product.dangdang.com/product.aspx?product_id=690222&amp;ref=search-1-pub">http://product.dangdang.com/product.aspx?product_id=690222&amp;ref=search-1-pub</a><br />有好几卷的，只给出一卷的连接，而且网上版本很多，大家可以自行选择。<br />这个还没看，关键是没时间了，准备考研完了就趁着假期看完。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">7.《组合数学》<br /><a style="color: rgb(49,52,40); text-decoration: none" href="http://product.dangdang.com/product.aspx?product_id=8976756&amp;ref=search-0-mix">http://product.dangdang.com/product.aspx?product_id=8976756&amp;ref=search-0-mix</a><br />鸽巢原理，博弈，容斥原理，Catalan数等都属于这个范畴的，建议看看。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">8.《数据结构（C语言版）》严蔚敏<br /><a style="color: rgb(49,52,40); text-decoration: none" href="http://product.dangdang.com/product.aspx?product_id=9268172&amp;ref=search-1-pub">http://product.dangdang.com/product.aspx?product_id=9268172&amp;ref=search-1-pub</a><br />数据结构，这个必须得学好啊~~~</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">9.《数据结构与算法分析C++描述（第三版）》<br /><a style="color: rgb(49,52,40); text-decoration: none" href="http://product.dangdang.com/product.aspx?product_id=9239535&amp;ref=search-1-pub">http://product.dangdang.com/product.aspx?product_id=9239535&amp;ref=search-1-pub</a><br />有时间可以看看，C++ Template写的，可以顺便巩固下template。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">以下基本都没看过，不过貌似很有名，给出书名和连接：<br />10.《世界大学生程序设计竞赛（ACM/ICPC）高级教程.第一册.程序设计中常用的计算思维方式》<br /><a style="color: rgb(49,52,40); text-decoration: none" href="http://product.dangdang.com/product.aspx?product_id=20645866&amp;ref=search-1-pub">http://product.dangdang.com/product.aspx?product_id=20645866&amp;ref=search-1-pub</a><br />这本我其实买了，但是还没有时间看。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">11.《国际大学生程序设计竞赛指南&#8212;ACM程序设计》<br /><a style="color: rgb(49,52,40); text-decoration: none" href="http://product.dangdang.com/product.aspx?product_id=20450827&amp;ref=search-1-pub">http://product.dangdang.com/product.aspx?product_id=20450827&amp;ref=search-1-pub</a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">12.《国际大学生程序设计竞赛例题解（三）图论、动态规划算法、综合题专集》<br /><a style="color: rgb(49,52,40); text-decoration: none" href="http://product.dangdang.com/product.aspx?product_id=9352432&amp;ref=search-1-pub">http://product.dangdang.com/product.aspx?product_id=9352432&amp;ref=search-1-pub</a><br />这个好像也有好几册，每一册都是单独讲一个方面的。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">13.《挑战编程：程序设计竞赛训练手册》<br /><a style="color: rgb(49,52,40); text-decoration: none" href="http://product.dangdang.com/product.aspx?product_id=20637355&amp;ref=search-1-pub">http://product.dangdang.com/product.aspx?product_id=20637355&amp;ref=search-1-pub</a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><span style="color: rgb(255,255,255)">(作者：<strong>Tanky Woo</strong>, 个人博客：</span><a style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/"><span style="color: rgb(255,255,255)">http://www.wutianqi.com</span></a><span style="color: rgb(255,255,255)"><span class="Apple-converted-space">&nbsp;</span>，C++/算法论坛：</span><a title="http://www.cppleyuan.com/" style="color: rgb(49,52,40); text-decoration: none" href="http://www.cppleyuan.com/"><span style="color: rgb(255,255,255)">http://www.cppleyuan.com/</span></a><span style="color: rgb(255,255,255)"><strong><span class="Apple-converted-space">&nbsp;</span></strong>。转载请注明个人及原文连接，谢谢合作)</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><span style="font-size: x-large; color: rgb(255,0,0)"><strong>入门方法</strong></span>：<br />这么多书，不可能全部都看的，我觉得前10本，也就是我看过的，都还不错，大家可以看看。<br />另外，我个人推荐ACM可以这样入门(以下用到了上面书籍前面的序号)：（当然，如果学校有专门培训的，则跟着学校来更好）<br />1.<strong>数据结构</strong>是基础，建议先把8号严蔚敏老师的《数据结构》好好看1~2遍，代码都手动敲一敲。<br />2.再看2号<strong>刘汝佳的白书</strong>。<br />3.去年暑假(2010.7~2010.9月)，我曾经给我的论坛(C++奋斗乐园：<a title="http://www.cppleyuan.com/" style="color: rgb(49,52,40); text-decoration: none" href="http://www.cppleyuan.com/">http://www.cppleyuan.com/</a>)搞过一次<strong>ACM专题训练</strong>，训练题全部来至HDOJ，当时我是由易到难，每天选择一个专题，在HDOJ上找3~4题，然后在论坛给出题目，大家可以到HDOJ去提交，然后贴到论坛供其他朋友参考。板块是：<a style="color: rgb(49,52,40); text-decoration: none" href="http://www.cppleyuan.com/forumdisplay.php?fid=40">http://www.cppleyuan.com/forumdisplay.php?fid=40，前面都是看书，这里就建议大家开始实战了，在论坛里一共除了200多题，大家一定要做！</a><br />4.有了一定的基础，就可以再<strong>一边进行深入(看书)，一边做题</strong>了。这个时候神马《算法导论》，《计算机程序设计艺术》等等都可以看看。<br />5.到了这个阶段，没啥说的了，<strong>自由学习</strong>~~~</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">最后说一句：算法魅力，无与伦比，欢迎大家来到ACM的世界！加油！</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">(作者：<strong>Tanky Woo</strong>, 个人博客：<a style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/">http://www.wutianqi.com</a><span class="Apple-converted-space">&nbsp;</span>，C++/算法论坛：<a title="http://www.cppleyuan.com/" style="color: rgb(49,52,40); text-decoration: none" href="http://www.cppleyuan.com/">http://www.cppleyuan.com/</a><strong><span class="Apple-converted-space">&nbsp;</span></strong>。转载请注明个人及原文连接，谢谢合作)</p></span></span><img src ="http://www.cppblog.com/tanky-woo/aggbug/148295.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tanky-woo/" target="_blank">Tanky Woo</a> 2011-06-08 20:08 <a href="http://www.cppblog.com/tanky-woo/archive/2011/06/08/148295.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《算法导论》学习总结 — 19.第15章 动态规划(4) 案例之LCS</title><link>http://www.cppblog.com/tanky-woo/archive/2011/05/26/147257.html</link><dc:creator>Tanky Woo</dc:creator><author>Tanky Woo</author><pubDate>Thu, 26 May 2011 10:55:00 GMT</pubDate><guid>http://www.cppblog.com/tanky-woo/archive/2011/05/26/147257.html</guid><wfw:comment>http://www.cppblog.com/tanky-woo/comments/147257.html</wfw:comment><comments>http://www.cppblog.com/tanky-woo/archive/2011/05/26/147257.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/tanky-woo/comments/commentRss/147257.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tanky-woo/services/trackbacks/147257.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 12px; color: rgb(51,51,51); line-height: 18px; font-family: 'Lucida Grande', Arial, Helvetica, sans-serif"> 
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">建议先看看前言：<a href="http://www.cppblog.com/tanky-woo/archive/2011/04/09/143794.html">http://www.cppblog.com/tanky-woo/archive/2011/04/09/143794.html</a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">这个案例也比较简单，最长公共子序列(LCS)，网上的分析非常多，给力啊！</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">按照上一篇总结所说的，找状态转移方程：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><a class="highslide-image" style="cursor: url(http://www.wutianqi.com/wp-content/plugins/auto-highslide/highslide/graphics/zoomin.cur), pointer; color: rgb(49,52,40); text-decoration: none; outline-style: none; outline-width: initial; outline-color: initial" onclick="return hs.expand(this);" href="http://www.wutianqi.com/wp-content/uploads/2011/05/15_5.png"><img title="15_5" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="69" alt="15_5" src="http://www.wutianqi.com/wp-content/uploads/2011/05/15_5_thumb.png" width="421" border="0" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">所以按照所给<a style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/?p=2505">方程</a>，写代码的工作就非常非常简单轻松了：</p>
<div class="wp_syntax" style="border-right: silver 1px solid; border-top: silver 1px solid; overflow-y: hidden; overflow-x: auto; margin: 0px 0px 1.5em; border-left: silver 1px solid; width: 618px; color: rgb(17,0,0); border-bottom: silver 1px solid; background-color: rgb(249,249,249)">
<table style="border-right: rgb(204,204,204) 1px solid; border-top: rgb(204,204,204) 1px solid; border-left: rgb(204,204,204) 1px solid; border-bottom: rgb(204,204,204) 1px solid; border-collapse: collapse; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px">
<tbody>
<tr>
<td class="line_numbers" style="border-right: rgb(204,204,204) 1px solid; padding-right: 4px; border-top: rgb(204,204,204) 1px solid; overflow-y: visible; padding-left: 4px; overflow-x: visible; padding-bottom: 2px; vertical-align: top; border-left: rgb(204,204,204) 1px solid; color: gray; padding-top: 2px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(221,238,255); text-align: right; background-origin: initial; background-clip: initial"><pre style="clear: none; overflow-y: visible; font-size: 12px; float: none; overflow-x: visible; margin: 0px; width: auto; line-height: 1.333; white-space: pre">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
</pre></td>
<td class="code" style="border-right: rgb(204,204,204) 1px solid; padding-right: 4px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; padding-bottom: 2px; vertical-align: top; border-left: rgb(204,204,204) 1px solid; padding-top: 2px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(240,240,240); background-origin: initial; background-clip: initial"><pre class="cpp" style="clear: none; overflow-y: visible; font-size: 12px; float: none; overflow-x: visible; margin: 0px; width: auto; line-height: 1.333; font-family: monospace; white-space: pre"><span style="color: rgb(255,0,0); font-style: italic">/*
Author: Tanky Woo
Blog:   www.WuTianQi.com
About:  《算法导论》15.4 最长公共自序列(LCS)
*/</span>
&nbsp;
<span style="color: rgb(51,153,0)">#include &lt;iostream&gt;</span>
<span style="color: rgb(0,0,255)">using</span> <span style="color: rgb(0,0,255)">namespace</span> std<span style="color: rgb(0,128,128)">;</span>
&nbsp;
<span style="color: rgb(0,0,255)">char</span> b<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">20</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">20</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
<span style="color: rgb(0,0,255)">int</span> c<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">20</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">20</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
&nbsp;
<span style="color: rgb(0,0,255)">char</span> x<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">20</span><span style="color: rgb(0,128,0)">]</span>, y<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">20</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
&nbsp;
<span style="color: rgb(0,0,255)">void</span> LCS<span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,128,0)">)</span>
<span style="color: rgb(0,128,0)">{</span>
	<span style="color: rgb(0,0,255)">int</span> m <span style="color: rgb(0,0,128)">=</span> <span style="color: rgb(0,0,221)">strlen</span><span style="color: rgb(0,128,0)">(</span>x<span style="color: rgb(0,0,64)">+</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">)</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">int</span> n <span style="color: rgb(0,0,128)">=</span> <span style="color: rgb(0,0,221)">strlen</span><span style="color: rgb(0,128,0)">(</span>y<span style="color: rgb(0,0,64)">+</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">)</span><span style="color: rgb(0,128,128)">;</span>
&nbsp;
	<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> i<span style="color: rgb(0,0,128)">=</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span> i<span style="color: rgb(0,0,128)">&lt;=</span>m<span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">++</span>i<span style="color: rgb(0,128,0)">)</span>
		c<span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">0</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> <span style="color: rgb(0,0,221)">0</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> j<span style="color: rgb(0,0,128)">=</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span> j<span style="color: rgb(0,0,128)">&lt;=</span>n<span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">++</span>j<span style="color: rgb(0,128,0)">)</span>
		c<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">0</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> <span style="color: rgb(0,0,221)">0</span><span style="color: rgb(0,128,128)">;</span>
&nbsp;
	<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> i<span style="color: rgb(0,0,128)">=</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span> i<span style="color: rgb(0,0,128)">&lt;=</span>m<span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">++</span>i<span style="color: rgb(0,128,0)">)</span>
		<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> j<span style="color: rgb(0,0,128)">=</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span> j<span style="color: rgb(0,0,128)">&lt;=</span>n<span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">++</span>j<span style="color: rgb(0,128,0)">)</span>
		<span style="color: rgb(0,128,0)">{</span>
			<span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,128,0)">(</span>x<span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">==</span> y<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">)</span>
			<span style="color: rgb(0,128,0)">{</span>
				c<span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> c<span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> <span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span>
				b<span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> <span style="color: rgb(255,0,0)">'\<span style="font-weight: bold; color: rgb(0,0,153)">\'</span>;    // 注意这里第一个\\是转移字符，代表<span style="font-weight: bold; color: rgb(0,0,153)">\
</span>			}
			else if(c[i-1][j] &gt;= c[i][j-1])
			{
				c[i][j] = c[i-1][j];
				b[i][j] = '</span><span style="color: rgb(0,0,64)">|</span><span style="color: rgb(255,0,0)">';
			}
			else
			{
				c[i][j] = c[i][j-1];
				b[i][j] = '</span><span style="color: rgb(0,0,64)">-</span><span style="color: rgb(255,0,0)">';
			}
		}
}
&nbsp;
void printLCS(int i, int j)
{
	if(i == 0 || j == 0)
		return;
	if(b[i][j] == '</span>\\<span style="color: rgb(255,0,0)">')
	{
		printLCS(i-1, j-1);
		cout &lt;&lt; x[i] &lt;&lt; " ";
	}
	else if(b[i][j] == '</span><span style="color: rgb(0,0,64)">|</span><span style="color: rgb(255,0,0)">')
		printLCS(i-1, j);
	else
		printLCS(i, j-1);
}
&nbsp;
int main()
{
	cout &lt;&lt; "Input the array A:<span style="font-weight: bold; color: rgb(0,0,153)">\n</span>";
	cin &gt;&gt; x+1;
	cout &lt;&lt; "Input the array B:<span style="font-weight: bold; color: rgb(0,0,153)">\n</span>";
	cin &gt;&gt; y+1;
	LCS();
	printLCS(strlen(x+1), strlen(y+1));
}</span></pre></td></tr></tbody></table></div>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">看书上图15-6的结果图：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><a class="highslide-image" style="cursor: url(http://www.wutianqi.com/wp-content/plugins/auto-highslide/highslide/graphics/zoomin.cur), pointer; color: rgb(49,52,40); text-decoration: none; outline-style: none; outline-width: initial; outline-color: initial" onclick="return hs.expand(this);" href="http://www.wutianqi.com/wp-content/uploads/2011/05/15_6.png"><img title="15_6" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="382" alt="15_6" src="http://www.wutianqi.com/wp-content/uploads/2011/05/15_6_thumb.png" width="403" border="0" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">又是一个给力的图,建议自己按照程序把这个图画出来.</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">另外,说到LCS,不得不说的是LIS(最长上升子序列)，也是一个DP，我曾经总结过：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><a title="http://www.wutianqi.com/?p=1850" style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/?p=1850">http://www.wutianqi.com/?p=1850</a></p>
<div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a63a4b3f-6a4b-4352-a4ca-3810986c83b1" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">Tanky Woo 标签:<span class="Apple-converted-space">&nbsp;</span><a style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/DP" rel="tag">DP</a>，<a style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92" rel="tag">动态规划</a>，<a style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/LCS" rel="tag">LCS</a><br /><br />
<div class="wlWriterEditableSmartContent" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<p>在我独立博客上的原文：<a href="http://www.wutianqi.com/?p=2505">http://www.wutianqi.com/?p=2505</a></p>
<p>欢迎大家互相学习，互相进步！</p></div><script type="text/javascript">
if ($ != jQuery) {
	$ = jQuery.noConflict();
}
var isLogined = true;
var cb_blogId = 72950;
var cb_entryId = 2054202;
var cb_blogApp = "tanky_woo";
var cb_blogUserGuid = "99a47d77-f68b-df11-ba8f-001cf0cd104b";
var cb_entryCreatedDate = '2011/5/23 12:03:00';
</script></div></span></span><img src ="http://www.cppblog.com/tanky-woo/aggbug/147257.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tanky-woo/" target="_blank">Tanky Woo</a> 2011-05-26 18:55 <a href="http://www.cppblog.com/tanky-woo/archive/2011/05/26/147257.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《算法导论》学习总结 — 18.第15章 动态规划(3) 基础入门2</title><link>http://www.cppblog.com/tanky-woo/archive/2011/05/23/146964.html</link><dc:creator>Tanky Woo</dc:creator><author>Tanky Woo</author><pubDate>Mon, 23 May 2011 04:03:00 GMT</pubDate><guid>http://www.cppblog.com/tanky-woo/archive/2011/05/23/146964.html</guid><wfw:comment>http://www.cppblog.com/tanky-woo/comments/146964.html</wfw:comment><comments>http://www.cppblog.com/tanky-woo/archive/2011/05/23/146964.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tanky-woo/comments/commentRss/146964.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tanky-woo/services/trackbacks/146964.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 12px; color: rgb(51,51,51); line-height: 18px; font-family: 'Lucida Grande', Arial, Helvetica, sans-serif"> 
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">建议先看看前言：<a href="http://www.cppblog.com/tanky-woo/archive/2011/04/09/143794.html">http://www.cppblog.com/tanky-woo/archive/2011/04/09/143794.html</a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">这一节可以看到<a style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/?p=2484">《算法导论》学习总结 &#8212; 16.第15章 动态规划(1) 基本入门</a>的补充。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><font color="#0000ff">采用动态规划的最优化问题的两个要素：<strong><font color="#ff8000">最优子结构</font></strong>和<strong><font color="#ff8000">重叠子问题</font></strong>。</font></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">先看看最优子结构：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">在第17篇总结时，装配线调度问题中，已经设计到了最优子结构，证明最优子结构问题可以用书上说的&#8220;剪贴技术&#8221;，即假设存在更优的解，来反正最优解矛盾。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">再看看重叠子问题：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">当一个递归算法不断的调用同一个问题时，我们说该最有问题包含&#8220;重叠子问题&#8221;。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">上面这句话不好理解？</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">看看下面这个比较：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">递归算法：自顶而下，对在递归树中重复出现的每个子问题都要重复解一次。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">动态规划：自下而上，对每个只解一次。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">结合第16篇总结的三角形求值例子，可以看得到，自下而上导致每个子问题只求解一次。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">以上理论性有点强，我最开始学DP看的是HDOJ的课件，有兴趣的可以去看看。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">在那里面，主要讲到了是找状态转移方程，在第16篇的三角形求值例子和第17篇的装配线调度例子，那些递归公式都是状态转移方程。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">下面这段话好好理解：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">动态规划的几个概念:<span class="Apple-converted-space">&nbsp;</span><br />阶段：据空间顺序或时间顺序对问题的求解划分阶段。<span class="Apple-converted-space">&nbsp;</span><br />状态：描述事物的性质，不同事物有不同的性质，因而用不同的状态来刻画。对问题的求解状态的描述是分阶段的。<span class="Apple-converted-space">&nbsp;</span><br />决策：根据题意要求，对每个阶段所做出的某种选择性操作。<span class="Apple-converted-space">&nbsp;</span><br />状态转移方程：用数学公式描述与阶段相关的状态间的演变规律。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">动态规划是运筹学的一个重要分支，是解决多阶段决策过程最优化的一种方法。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">所谓多阶段决策过程，是将所研究的过程划分为若干个相互联系的阶段，在求解时，对每一个阶段都要做出决策，前一个决策确定以后，常常会影响下一个阶段的决策。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">动态规划所依据的是&#8220;最优性原理&#8221;。<span class="Apple-converted-space">&nbsp;</span><br />&#8220;最优性原理&#8221;可陈述为：不论初始状态和第一步决策是什么，余下的决策相对于前一次决策所产生的新状态，构成一个最优决策序列。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">最优决策序列的子序列，一定是局部最优决策子序列。<span class="Apple-converted-space">&nbsp;</span><br />包含有非局部最优的决策子序列，一定不是最优决策序列。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><font color="#0000ff">动态规划的指导思想是：</font></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><font color="#0000ff">在做每一步决策时，列出各种可能的局部解，之后依据某种判定条件，舍弃那些肯定不能得到最优解的局部解。这样，在每一步都经过筛选，以每一步都是最优的来保证全局是最优的。筛选相当于最大限度地有效剪枝（从搜索角度看），效率会十分高。但它又不同于贪心法。贪心法只能做到局部最优，不能保证全局最优，因为有些问题不符合最优性原理。</font></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">看见有人说递归就是DFS，而DP就是BFS，感觉有那么一点意思，对于DP，就是从底层一层层的计算，然后在当层中选取最优，逐层最优以至总体最优。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">其实这个还是多做一些题就好了(&#8857;ｏ&#8857;)，大家别认为我是做题控，其实说实在话，看N遍不如做一题，说白了，算法数学本一家，算法就是数学，走过高中的，都知道数学题得多做，尤其压轴题，看N遍不如做一遍，这个也是一样做几题就知道DP是神马东东了！</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">&nbsp;</p>
<div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:62a4f892-b0d6-44f3-99f1-a2e278d7e0dc" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">Tanky Woo 标签:<span class="Apple-converted-space">&nbsp;</span><a style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/DP" rel="tag">DP</a>，<a style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92" rel="tag">动态规划</a></div></span></span><br />
<p>在我独立博客上的原文：<a href="http://www.wutianqi.com/?p=2500">http://www.wutianqi.com/?p=2500</a></p>
<p>欢迎大家互相学习，互相进步！</p><img src ="http://www.cppblog.com/tanky-woo/aggbug/146964.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tanky-woo/" target="_blank">Tanky Woo</a> 2011-05-23 12:03 <a href="http://www.cppblog.com/tanky-woo/archive/2011/05/23/146964.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《算法导论》学习总结 — 17.第15章 动态规划(2) 案例之装配线调度</title><link>http://www.cppblog.com/tanky-woo/archive/2011/05/20/146804.html</link><dc:creator>Tanky Woo</dc:creator><author>Tanky Woo</author><pubDate>Fri, 20 May 2011 03:57:00 GMT</pubDate><guid>http://www.cppblog.com/tanky-woo/archive/2011/05/20/146804.html</guid><wfw:comment>http://www.cppblog.com/tanky-woo/comments/146804.html</wfw:comment><comments>http://www.cppblog.com/tanky-woo/archive/2011/05/20/146804.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/tanky-woo/comments/commentRss/146804.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tanky-woo/services/trackbacks/146804.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 12px; color: rgb(51,51,51); line-height: 18px; font-family: 'Lucida Grande', Arial, Helvetica, sans-serif"> 
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">建议先看看前言：<a href="http://www.cppblog.com/tanky-woo/archive/2011/04/09/143794.html">http://www.cppblog.com/tanky-woo/archive/2011/04/09/143794.html</a><br /><br />原来打算把算法导论在7月份前搞定，现在已经过去一个多月了，才只看到第15章，后面也只零散看了一些，不知道假期前能否看完。。。够呛啊，马上要期末考试了，上学期GPA不到2，被学位警告了，虽说以后不学这个专业了，但起码成绩单上也不能有挂科是吧。。。要是平时一点不看，考前靠春哥，曾哥，关公哥都不行啊。。。这进度，郁闷！</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">尽力吧！</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">顺便还是说两句话：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">1.有些书上分析的相当好了，我不想做画蛇添足的人，所以有的地方我会适当省略，当然也不是说我总结的地方就是书上讲的不好的地方，只是没人有一套自己的理解方式，我用自己的话去总结了，当时也就是最适合我的知识！所以，建议大家多写一些算法总结，你会体会到好处的！</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">2.而且我这个的性质是总结，不是对一个算法的具体讲解，所以不先看书，大家应该读不懂的，就比如下面，题目我就没有贴出来，大家不看数，肯定就读不懂，我的目的是大家看完书后，谢谢总结，或者看看别人写的总结，说不定可以发现自己哪些地方理解错了，哪些地方不理解，或是哪些地方值得探讨。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">建议先看看前言：<a title="http://www.wutianqi.com/?p=2298" style="color: rgb(49,52,40); text-decoration: none" href="http://www.wutianqi.com/?p=2298">http://www.wutianqi.com/?p=2298</a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">这一次主要是分析15.1节的例子&#8212;装配线调度问题。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">题目有点长，首先得把题目读懂。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">这个例子书上花了6面纸的篇幅来详细分析，由此可见其重要性。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">谈到DP,不得不说的就是暴力法，大家都知道，如果用暴力解决类似问题，一般时间复杂度都是非常非常的高，这个时候救世主DP就出来了，DP以避免了许多重复的计算，而大大降低了时间复杂度。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">按照书上的四个步骤，我在这里提取一些重点，建议还是把P194~196这四步完整步骤看书上的分析。只有慢慢品味，你才会发现《算法导论》的美。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><strong>步骤一</strong>：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">分析问题，比如一个底盘要到达S[1][j]，则有两种情况，第一种是从S[1][j-1]到S[1][j]，第二种是从S[2][j-1]到S[1][j]。找出这两者所花时间的最小，则就是S[1][j]所需时间的最小。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">这就是有局部最优解求全局最优解。也就是说，一个问题的最优解包含了子问题的一个最优解。我们称这个性质为最优子结构。这是是否可以应用DP的标志之一。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><strong>步骤二</strong>：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">找出这个递归关系，由步骤一可以得到这个递归关系：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><a class="highslide-image" style="cursor: url(http://www.wutianqi.com/wp-content/plugins/auto-highslide/highslide/graphics/zoomin.cur), pointer; color: rgb(49,52,40); text-decoration: none; outline-style: none; outline-width: initial; outline-color: initial" onclick="return hs.expand(this);" href="http://www.wutianqi.com/wp-content/uploads/2011/05/15_2.png"><img title="15_2" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="105" alt="15_2" src="http://www.wutianqi.com/wp-content/uploads/2011/05/15_2_thumb.png" width="453" border="0" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><strong>步骤三</strong>：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">因为递归的关系，一般总是可以转换为非递归的算法。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">由已知量f1[1], f2[1]逐步推出未知量，推啊推，推啊推，最后就推到结果了~~~~</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><strong>步骤四</strong>：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">再由已知结果返回求路径。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">这一节最给力的就是这个例子以及相应的<strong>图</strong>：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><a class="highslide-image" style="cursor: url(http://www.wutianqi.com/wp-content/plugins/auto-highslide/highslide/graphics/zoomin.cur), pointer; color: rgb(49,52,40); text-decoration: none; outline-style: none; outline-width: initial; outline-color: initial" onclick="return hs.expand(this);" href="http://www.wutianqi.com/wp-content/uploads/2011/05/15_3.png"><img title="15_3" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="395" alt="15_3" src="http://www.wutianqi.com/wp-content/uploads/2011/05/15_3_thumb.png" width="644" border="0" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">拿起笔，用书上给出的例子，分析这个图！</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">以下是代码：</p>
<div class="wp_syntax" style="border-right: silver 1px solid; border-top: silver 1px solid; overflow-y: hidden; overflow-x: auto; margin: 0px 0px 1.5em; border-left: silver 1px solid; width: 618px; color: rgb(17,0,0); border-bottom: silver 1px solid; background-color: rgb(249,249,249)">
<table style="border-right: rgb(204,204,204) 1px solid; border-top: rgb(204,204,204) 1px solid; border-left: rgb(204,204,204) 1px solid; border-bottom: rgb(204,204,204) 1px solid; border-collapse: collapse; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px">
<tbody>
<tr>
<td class="line_numbers" style="border-right: rgb(204,204,204) 1px solid; padding-right: 4px; border-top: rgb(204,204,204) 1px solid; overflow-y: visible; padding-left: 4px; overflow-x: visible; padding-bottom: 2px; vertical-align: top; border-left: rgb(204,204,204) 1px solid; color: gray; padding-top: 2px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(221,238,255); text-align: right; background-origin: initial; background-clip: initial"><pre style="clear: none; overflow-y: visible; font-size: 12px; float: none; overflow-x: visible; margin: 0px; width: auto; line-height: 1.333; white-space: pre">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
</pre></td>
<td class="code" style="border-right: rgb(204,204,204) 1px solid; padding-right: 4px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; padding-bottom: 2px; vertical-align: top; border-left: rgb(204,204,204) 1px solid; padding-top: 2px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(240,240,240); background-origin: initial; background-clip: initial"><pre class="cpp" style="clear: none; overflow-y: visible; font-size: 12px; float: none; overflow-x: visible; margin: 0px; width: auto; line-height: 1.333; font-family: monospace; white-space: pre"><span style="color: rgb(255,0,0); font-style: italic">/*
Author: Tanky Woo
Blog:   www.WuTianQi.com
About:  《算法导论》15.1 装配线调度
*/</span>
<span style="color: rgb(51,153,0)">#include &lt;iostream&gt;</span>
<span style="color: rgb(0,0,255)">using</span> <span style="color: rgb(0,0,255)">namespace</span> std<span style="color: rgb(0,128,128)">;</span>
&nbsp;
<span style="color: rgb(0,0,255)">int</span> n<span style="color: rgb(0,128,128)">;</span>                 <span style="color: rgb(102,102,102)">// 一个装配线上有n个装配站</span>
<span style="color: rgb(0,0,255)">int</span> e1, e2<span style="color: rgb(0,128,128)">;</span>            <span style="color: rgb(102,102,102)">// 进入装配线1,2需要的时间</span>
<span style="color: rgb(0,0,255)">int</span> x1, x2<span style="color: rgb(0,128,128)">;</span>            <span style="color: rgb(102,102,102)">// 离开装配线1,2需要的时间</span>
<span style="color: rgb(0,0,255)">int</span> t<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">3</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">100</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>         <span style="color: rgb(102,102,102)">// t[1][j]表示底盘从S[1][j]移动到S[2][j+1]所需时间,同理t[2][j]</span>
<span style="color: rgb(0,0,255)">int</span> a<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">3</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">100</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>         <span style="color: rgb(102,102,102)">// a[1][j]表示在装配站S[1][j]所需时间</span>
<span style="color: rgb(0,0,255)">int</span> f1<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">100</span><span style="color: rgb(0,128,0)">]</span>, f2<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">100</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>  <span style="color: rgb(102,102,102)">// f1[j], f2[j]分别表示在第一/第二条装配线上第j个装配站的最优解</span>
<span style="color: rgb(0,0,255)">int</span> ln1<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">100</span><span style="color: rgb(0,128,0)">]</span>, ln2<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">100</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span><span style="color: rgb(102,102,102)">// ln1[j]记录第一条装配线上,最优解时第j个装配站的前一个装配站是第一条线还是第二条线上</span>
<span style="color: rgb(0,0,255)">int</span> f, ln<span style="color: rgb(0,128,128)">;</span>             <span style="color: rgb(102,102,102)">// 最优解是,f代表最小花费时间，ln表示最后出来时是从装配线1还是装配线2</span>
&nbsp;
<span style="color: rgb(0,0,255)">void</span> DP<span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,128,0)">)</span>
<span style="color: rgb(0,128,0)">{</span>
	f1<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> e1 <span style="color: rgb(0,0,64)">+</span> a<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
	f2<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> e2 <span style="color: rgb(0,0,64)">+</span> a<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">2</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> j<span style="color: rgb(0,0,128)">=</span><span style="color: rgb(0,0,221)">2</span><span style="color: rgb(0,128,128)">;</span> j<span style="color: rgb(0,0,128)">&lt;=</span>n<span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">++</span>j<span style="color: rgb(0,128,0)">)</span>
	<span style="color: rgb(0,128,0)">{</span>
		<span style="color: rgb(102,102,102)">// 处理第一条装配线的最优子结构</span>
		<span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,128,0)">(</span>f1<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> a<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">&lt;=</span> f2<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> t<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">2</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> a<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">)</span>
		<span style="color: rgb(0,128,0)">{</span>
			f1<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> f1<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> a<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
			ln1<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> <span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span>
		<span style="color: rgb(0,128,0)">}</span>
		<span style="color: rgb(0,0,255)">else</span>
		<span style="color: rgb(0,128,0)">{</span>
			f1<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> f2<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> t<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">2</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> a<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
			ln1<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> <span style="color: rgb(0,0,221)">2</span><span style="color: rgb(0,128,128)">;</span>
		<span style="color: rgb(0,128,0)">}</span>
		<span style="color: rgb(102,102,102)">// 处理第二条装配线的最优子结构</span>
		<span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,128,0)">(</span>f2<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> a<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">2</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">&lt;=</span> f1<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> t<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> a<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">2</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">)</span>
		<span style="color: rgb(0,128,0)">{</span>
			f2<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> f2<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> a<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">2</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
			ln2<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> <span style="color: rgb(0,0,221)">2</span><span style="color: rgb(0,128,128)">;</span>
		<span style="color: rgb(0,128,0)">}</span>
		<span style="color: rgb(0,0,255)">else</span>
		<span style="color: rgb(0,128,0)">{</span>
			f2<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> f1<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> t<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> a<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">2</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
			ln2<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> <span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span>
		<span style="color: rgb(0,128,0)">}</span>
	<span style="color: rgb(0,128,0)">}</span>
	<span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,128,0)">(</span>f1<span style="color: rgb(0,128,0)">[</span>n<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> x1 <span style="color: rgb(0,0,128)">&lt;=</span> f2<span style="color: rgb(0,128,0)">[</span>n<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> x2<span style="color: rgb(0,128,0)">)</span>
	<span style="color: rgb(0,128,0)">{</span>
		f <span style="color: rgb(0,0,128)">=</span> f1<span style="color: rgb(0,128,0)">[</span>n<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> x1<span style="color: rgb(0,128,128)">;</span>
		ln <span style="color: rgb(0,0,128)">=</span> <span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,128,0)">}</span>
	<span style="color: rgb(0,0,255)">else</span>
	<span style="color: rgb(0,128,0)">{</span>
		f <span style="color: rgb(0,0,128)">=</span> f2<span style="color: rgb(0,128,0)">[</span>n<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,64)">+</span> x2<span style="color: rgb(0,128,128)">;</span>
		ln <span style="color: rgb(0,0,128)">=</span> <span style="color: rgb(0,0,221)">2</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,128,0)">}</span>
<span style="color: rgb(0,128,0)">}</span>
&nbsp;
<span style="color: rgb(0,0,255)">void</span> PrintStation<span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,128,0)">)</span>
<span style="color: rgb(0,128,0)">{</span>
	<span style="color: rgb(0,0,255)">int</span> i<span style="color: rgb(0,0,128)">=</span> ln<span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"line "</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> i <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">", station "</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> n <span style="color: rgb(0,0,128)">&lt;&lt;</span> endl<span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> j<span style="color: rgb(0,0,128)">=</span>n<span style="color: rgb(0,128,128)">;</span> j<span style="color: rgb(0,0,128)">&gt;=</span><span style="color: rgb(0,0,221)">2</span><span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">--</span>j<span style="color: rgb(0,128,0)">)</span>
	<span style="color: rgb(0,128,0)">{</span>
		<span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,128,0)">(</span>i <span style="color: rgb(0,0,128)">==</span> <span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">)</span>
			i <span style="color: rgb(0,0,128)">=</span> ln1<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
		<span style="color: rgb(0,0,255)">else</span>
			i <span style="color: rgb(0,0,128)">=</span> ln2<span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
		<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"line "</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> i <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">", station "</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> j<span style="color: rgb(0,0,64)">-</span><span style="color: rgb(0,0,221)">1</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> endl<span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,128,0)">}</span>
<span style="color: rgb(0,128,0)">}</span>
&nbsp;
<span style="color: rgb(0,0,255)">int</span> main<span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,128,0)">)</span>
<span style="color: rgb(0,128,0)">{</span>
	<span style="color: rgb(102,102,102)">//freopen("input.txt", "r", stdin);</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"输入装配站的个数: "</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cin</span> <span style="color: rgb(0,0,128)">&gt;&gt;</span> n<span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"输入进入装配线1，2所需的时间e1, e2 :"</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cin</span> <span style="color: rgb(0,0,128)">&gt;&gt;</span> e1 <span style="color: rgb(0,0,128)">&gt;&gt;</span> e2<span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"输入离开装配线1, 2所需的时间x1, x2: "</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cin</span> <span style="color: rgb(0,0,128)">&gt;&gt;</span> x1 <span style="color: rgb(0,0,128)">&gt;&gt;</span> x2<span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"输入装配线1上各站加工所需时间a[1][j]: "</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> i<span style="color: rgb(0,0,128)">=</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span> i<span style="color: rgb(0,0,128)">&lt;=</span>n<span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">++</span>i<span style="color: rgb(0,128,0)">)</span>
		<span style="color: rgb(0,0,221)">cin</span> <span style="color: rgb(0,0,128)">&gt;&gt;</span> a<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"输入装配线2上各站加工所需时间a[2][j]: "</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> i<span style="color: rgb(0,0,128)">=</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span> i<span style="color: rgb(0,0,128)">&lt;=</span>n<span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">++</span>i<span style="color: rgb(0,128,0)">)</span>
		<span style="color: rgb(0,0,221)">cin</span> <span style="color: rgb(0,0,128)">&gt;&gt;</span> a<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">2</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"输入装配线1上的站到装配线2上的站所需时间t[1][j]: "</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(102,102,102)">//注意这里是i&lt;n，不是i&lt;=n</span>
	<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> i<span style="color: rgb(0,0,128)">=</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span> i<span style="color: rgb(0,0,128)">&lt;</span>n<span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">++</span>i<span style="color: rgb(0,128,0)">)</span>
		<span style="color: rgb(0,0,221)">cin</span> <span style="color: rgb(0,0,128)">&gt;&gt;</span> t<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"输入装配线2上的站到装配线1上的站所需时间t[2][j]: "</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> i<span style="color: rgb(0,0,128)">=</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span> i<span style="color: rgb(0,0,128)">&lt;</span>n<span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">++</span>i<span style="color: rgb(0,128,0)">)</span>
		<span style="color: rgb(0,0,221)">cin</span> <span style="color: rgb(0,0,128)">&gt;&gt;</span> t<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">2</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
	DP<span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,128,0)">)</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"最快需要时间: "</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> f <span style="color: rgb(0,0,128)">&lt;&lt;</span> endl<span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">"路线是: "</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> endl<span style="color: rgb(0,128,128)">;</span>
	PrintStation<span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,128,0)">)</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> endl<span style="color: rgb(0,128,128)">;</span>
<span style="color: rgb(0,128,0)">}</span></pre></td></tr></tbody></table></div>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">最后还是要感叹一下，《算法导论》讲的真是棒极了，希望大家能静下心把这一章节好好看看。<br /><br /></p>
<p>在我独立博客上的原文：<a href="http://www.wutianqi.com/?p=2496">http://www.wutianqi.com/?p=2496</a></p>
<p>欢迎大家互相学习，互相进步！<br /></p></span></span><img src ="http://www.cppblog.com/tanky-woo/aggbug/146804.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tanky-woo/" target="_blank">Tanky Woo</a> 2011-05-20 11:57 <a href="http://www.cppblog.com/tanky-woo/archive/2011/05/20/146804.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《算法导论》学习总结 — 16.第15章 动态规划(1) 基本入门</title><link>http://www.cppblog.com/tanky-woo/archive/2011/05/20/146787.html</link><dc:creator>Tanky Woo</dc:creator><author>Tanky Woo</author><pubDate>Thu, 19 May 2011 23:27:00 GMT</pubDate><guid>http://www.cppblog.com/tanky-woo/archive/2011/05/20/146787.html</guid><wfw:comment>http://www.cppblog.com/tanky-woo/comments/146787.html</wfw:comment><comments>http://www.cppblog.com/tanky-woo/archive/2011/05/20/146787.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tanky-woo/comments/commentRss/146787.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tanky-woo/services/trackbacks/146787.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 12px; color: rgb(51,51,51); line-height: 18px; font-family: 'Lucida Grande', Arial, Helvetica, sans-serif"> 
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">第十四章我想放在后面再看，先搁下。希望大家总结的一些文章也能向我推荐下，大家互相学习。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">首先，还是建议看看前言：<a href="http://www.cppblog.com/tanky-woo/archive/2011/04/09/143794.html">http://www.cppblog.com/tanky-woo/archive/2011/04/09/143794.html</a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">其次，阿门，感谢老天送给了我们这么一本圣经，看了这一章，再次感受到了《算法导论》分析问题的精辟，强悍的魅力。Orz, Orz,各种Orz。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">这一章讲的是动态规划，学算法的朋友，尤其是搞ACM的，对这个策略一定非常熟悉，所以这个算法网上的分析讲解教程也是铺天盖地，大家可以多搜几篇学习学习。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><strong>动态规划</strong>(<span style="color: rgb(255,153,0)">Dynamic Programming，简称DP</span>)是通过组合子问题的解来解决问题的。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><strong>注意</strong>这里的programming不是指编程，而是指一种<strong>规划</strong>。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">因为DP用的太广泛了，并且书上也花了很大的篇幅来讲这一章，所以我也准备把这一章分几篇来总结，并且我不按照书上的顺序来总结，也不会全部用书上的例子。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">这一章首先给出一些基础的概念，然后给出一个最基础入门的DP问题，三角形求值问题。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><span style="color: rgb(0,0,255)">DP适用于子问题不是独立的情况，这样如果用分治法，也会作许多重复的工作，而DP只对子问题求解一次，将其结果保存在一张表中，从而避免了每次遇到各个子问题时重新计算的情况。</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">比较分治法于动态规划的区别：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><strong>分治法</strong>：将问题划分为一些独立的子问题，递归的求解各子问题，然后合并子问题的解而得到原问题的解。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">eg.</p><pre>MERGE-SORT(<em>A</em>, <em>p</em>, <em>r</em>)
1 <strong>if</strong> <em>p</em> &lt; <em>r</em>
2   <strong>then</strong> <em>q</em> &#8592; |(<em>p</em> + <em>r</em>)/2|
3        MERGE-SORT(<em>A</em>, <em>p</em>, <em>q</em>)
4        MERGE-SORT(<em>A</em>, <em>q</em> + 1, <em>r</em>)
5        MERGE(<em>A</em>, <em>p</em>, <em>q</em>, <em>r</em>)</pre><pre><strong>动态规划</strong>：<span style="color: rgb(0,0,255)">适用于子问题<strong>不是独立</strong>的情况，也就是各子问题包含<strong>公共的子子问题</strong>，鉴于会重复的求解各子问题，DP对每个问</span></pre><pre><span style="color: rgb(0,0,255)">题<strong>只求解一遍</strong>，将其<strong>保存在一张表</strong>中，从而避免重复计算。</span></pre>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">DP算法的设计可以分为<strong>四个步骤</strong>：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><span style="color: rgb(0,0,255)">&#9312;.描述最优解的结构。</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><span style="color: rgb(0,0,255)">&#9313;.递归定义最优解的值。</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><span style="color: rgb(0,0,255)">&#9314;.按自底而上的方式计算最优解的值。</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><span style="color: rgb(0,0,255)">&#9315;.由计算出的结果创造一个最优解。</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">下面来看看三角形求值这个问题：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">将一个由N行数字组成的三角形，如图所以，设计一个算法，计算出三角形的由顶至底的一条路径，使该路径经过的数字总和最大。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><a class="highslide-image" style="cursor: url(http://www.wutianqi.com/wp-content/plugins/auto-highslide/highslide/graphics/zoomin.cur), pointer; color: rgb(49,52,40); text-decoration: none; outline-style: none; outline-width: initial; outline-color: initial" onclick="return hs.expand(this);" href="http://www.wutianqi.com/wp-content/uploads/2011/05/sanjiaoxing.png"><img title="sanjiaoxing" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="286" alt="sanjiaoxing" src="http://www.wutianqi.com/wp-content/uploads/2011/05/sanjiaoxing_thumb.png" width="327" border="0" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">这是在我见过的DP题目中，算是最简单的了，我相信就算没有学过DP的也会。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">将上图转化一下：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><a class="highslide-image" style="cursor: url(http://www.wutianqi.com/wp-content/plugins/auto-highslide/highslide/graphics/zoomin.cur), pointer; color: rgb(49,52,40); text-decoration: none; outline-style: none; outline-width: initial; outline-color: initial" onclick="return hs.expand(this);" href="http://www.wutianqi.com/wp-content/uploads/2011/05/sanjiaoxing2.png"><img title="sanjiaoxing2" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="269" alt="sanjiaoxing2" src="http://www.wutianqi.com/wp-content/uploads/2011/05/sanjiaoxing2_thumb.png" width="348" border="0" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">假设上图用map[][]数组保存。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">令f[i][j]表示从顶点(1, 1)到顶点(i, j)的最大值。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">则可以得到状态转移方程：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">f[i][j] = max(f[i+1][j], f[i+1][j+1]) + map[i][j]</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">此题既适合自顶而下的方法做，也适合自底而上的方法，</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">当用自顶而下的方法做时，最后要在在最后一列数中找出最大值，</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">而用自底而上的方法做时，f[1][1]即为最大值。</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">所以我们将图2根据状态转移方程可以得到图3：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><a class="highslide-image" style="cursor: url(http://www.wutianqi.com/wp-content/plugins/auto-highslide/highslide/graphics/zoomin.cur), pointer; color: rgb(49,52,40); text-decoration: none; outline-style: none; outline-width: initial; outline-color: initial" onclick="return hs.expand(this);" href="http://www.wutianqi.com/wp-content/uploads/2011/05/sanjiaoxing3.png"><img title="sanjiaoxing3" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="295" alt="sanjiaoxing3" src="http://www.wutianqi.com/wp-content/uploads/2011/05/sanjiaoxing3_thumb.png" width="369" border="0" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">最大值是30.</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">很简单的DP题，代码如下：</p>
<div class="wp_syntax" style="border-right: silver 1px solid; border-top: silver 1px solid; overflow-y: hidden; overflow-x: auto; margin: 0px 0px 1.5em; border-left: silver 1px solid; width: 618px; color: rgb(17,0,0); border-bottom: silver 1px solid; background-color: rgb(249,249,249)">
<table style="border-right: rgb(204,204,204) 1px solid; border-top: rgb(204,204,204) 1px solid; border-left: rgb(204,204,204) 1px solid; border-bottom: rgb(204,204,204) 1px solid; border-collapse: collapse; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px">
<tbody>
<tr>
<td class="line_numbers" style="border-right: rgb(204,204,204) 1px solid; padding-right: 4px; border-top: rgb(204,204,204) 1px solid; overflow-y: visible; padding-left: 4px; overflow-x: visible; padding-bottom: 2px; vertical-align: top; border-left: rgb(204,204,204) 1px solid; color: gray; padding-top: 2px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(221,238,255); text-align: right; background-origin: initial; background-clip: initial"><pre style="clear: none; overflow-y: visible; font-size: 12px; float: none; overflow-x: visible; margin: 0px; width: auto; line-height: 1.333; white-space: pre">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td>
<td class="code" style="border-right: rgb(204,204,204) 1px solid; padding-right: 4px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; padding-bottom: 2px; vertical-align: top; border-left: rgb(204,204,204) 1px solid; padding-top: 2px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(240,240,240); background-origin: initial; background-clip: initial"><pre class="cpp" style="clear: none; overflow-y: visible; font-size: 12px; float: none; overflow-x: visible; margin: 0px; width: auto; line-height: 1.333; font-family: monospace; white-space: pre"><span style="color: rgb(255,0,0); font-style: italic">/*
Author: Tanky Woo
Blog:   www.WuTianQi.com
Description: 动态规划之三角形求值问题
*/</span>
<span style="color: rgb(51,153,0)">#include &lt;iostream&gt;</span>
<span style="color: rgb(0,0,255)">using</span> <span style="color: rgb(0,0,255)">namespace</span> std<span style="color: rgb(0,128,128)">;</span>
&nbsp;
<span style="color: rgb(0,0,255)">int</span> map<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">6</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">6</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> 
<span style="color: rgb(0,128,0)">{</span>
	<span style="color: rgb(0,128,0)">{</span><span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">0</span><span style="color: rgb(0,128,0)">}</span>,
	<span style="color: rgb(0,128,0)">{</span><span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">7</span>, <span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">0</span><span style="color: rgb(0,128,0)">}</span>,
	<span style="color: rgb(0,128,0)">{</span><span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">3</span>, <span style="color: rgb(0,0,221)">8</span>, <span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">0</span><span style="color: rgb(0,128,0)">}</span>,
	<span style="color: rgb(0,128,0)">{</span><span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">8</span>, <span style="color: rgb(0,0,221)">1</span>, <span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">0</span><span style="color: rgb(0,128,0)">}</span>,
	<span style="color: rgb(0,128,0)">{</span><span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">2</span>, <span style="color: rgb(0,0,221)">7</span>, <span style="color: rgb(0,0,221)">4</span>, <span style="color: rgb(0,0,221)">4</span>, <span style="color: rgb(0,0,221)">0</span><span style="color: rgb(0,128,0)">}</span>,
	<span style="color: rgb(0,128,0)">{</span><span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">4</span>, <span style="color: rgb(0,0,221)">5</span>, <span style="color: rgb(0,0,221)">2</span>, <span style="color: rgb(0,0,221)">6</span>, <span style="color: rgb(0,0,221)">5</span><span style="color: rgb(0,128,0)">}</span>
<span style="color: rgb(0,128,0)">}</span><span style="color: rgb(0,128,128)">;</span>
&nbsp;
<span style="color: rgb(0,0,255)">int</span> f<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">6</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">6</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
&nbsp;
<span style="color: rgb(0,0,255)">int</span> _max<span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> a, <span style="color: rgb(0,0,255)">int</span> b<span style="color: rgb(0,128,0)">)</span>
<span style="color: rgb(0,128,0)">{</span>
	<span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,128,0)">(</span>a <span style="color: rgb(0,0,128)">&gt;=</span> b<span style="color: rgb(0,128,0)">)</span>
		<span style="color: rgb(0,0,255)">return</span> a<span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">return</span> b<span style="color: rgb(0,128,128)">;</span>
<span style="color: rgb(0,128,0)">}</span>
&nbsp;
<span style="color: rgb(0,0,255)">int</span> main<span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,128,0)">)</span>
<span style="color: rgb(0,128,0)">{</span>
	<span style="color: rgb(0,0,221)">memset</span><span style="color: rgb(0,128,0)">(</span>f, <span style="color: rgb(0,0,221)">0</span>, <span style="color: rgb(0,0,221)">sizeof</span><span style="color: rgb(0,128,0)">(</span>f<span style="color: rgb(0,128,0)">)</span><span style="color: rgb(0,128,0)">)</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> i<span style="color: rgb(0,0,128)">=</span><span style="color: rgb(0,0,221)">0</span><span style="color: rgb(0,128,128)">;</span> i<span style="color: rgb(0,0,128)">&lt;</span><span style="color: rgb(0,0,221)">6</span><span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">++</span>i<span style="color: rgb(0,128,0)">)</span>
		f<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">5</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> map<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">5</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> i<span style="color: rgb(0,0,128)">=</span><span style="color: rgb(0,0,221)">4</span><span style="color: rgb(0,128,128)">;</span> i<span style="color: rgb(0,0,128)">&gt;=</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">--</span>i<span style="color: rgb(0,128,0)">)</span>
		<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> j<span style="color: rgb(0,0,128)">=</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span> j<span style="color: rgb(0,0,128)">&lt;=</span>i<span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">++</span>j<span style="color: rgb(0,128,0)">)</span>
			f<span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">=</span> _max<span style="color: rgb(0,128,0)">(</span>f<span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,0,64)">+</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span>, f<span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,0,64)">+</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,0,64)">+</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">)</span> <span style="color: rgb(0,0,64)">+</span> map<span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> i<span style="color: rgb(0,0,128)">=</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span> i<span style="color: rgb(0,0,128)">&lt;=</span><span style="color: rgb(0,0,221)">5</span><span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">++</span>i<span style="color: rgb(0,128,0)">)</span>
	<span style="color: rgb(0,128,0)">{</span>
		<span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,255)">int</span> j<span style="color: rgb(0,0,128)">=</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,128)">;</span> j<span style="color: rgb(0,0,128)">&lt;=</span><span style="color: rgb(0,0,221)">5</span><span style="color: rgb(0,128,128)">;</span> <span style="color: rgb(0,0,64)">++</span>j<span style="color: rgb(0,128,0)">)</span>
		<span style="color: rgb(0,128,0)">{</span>
			<span style="color: rgb(0,0,221)">cout</span>.<span style="color: rgb(0,119,136)">width</span><span style="color: rgb(0,128,0)">(</span><span style="color: rgb(0,0,221)">2</span><span style="color: rgb(0,128,0)">)</span><span style="color: rgb(0,128,128)">;</span>
			<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> f<span style="color: rgb(0,128,0)">[</span>i<span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span>j<span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> <span style="color: rgb(255,0,0)">" "</span><span style="color: rgb(0,128,128)">;</span>
		<span style="color: rgb(0,128,0)">}</span>
		<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> endl<span style="color: rgb(0,128,128)">;</span>
	<span style="color: rgb(0,128,0)">}</span>
	<span style="color: rgb(0,0,221)">cout</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> f<span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span><span style="color: rgb(0,128,0)">[</span><span style="color: rgb(0,0,221)">1</span><span style="color: rgb(0,128,0)">]</span> <span style="color: rgb(0,0,128)">&lt;&lt;</span> endl<span style="color: rgb(0,128,128)">;</span>
<span style="color: rgb(0,128,0)">}</span></pre></td></tr></tbody></table></div>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">结果如图：</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px"><a class="highslide-image" style="cursor: url(http://www.wutianqi.com/wp-content/plugins/auto-highslide/highslide/graphics/zoomin.cur), pointer; color: rgb(49,52,40); text-decoration: none; outline-style: none; outline-width: initial; outline-color: initial" onclick="return hs.expand(this);" href="http://www.wutianqi.com/wp-content/uploads/2011/05/sanjiaoxing4.png"><img title="sanjiaoxing4" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="167" alt="sanjiaoxing4" src="http://www.wutianqi.com/wp-content/uploads/2011/05/sanjiaoxing4_thumb.png" width="205" border="0" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.25em; line-height: 1.5; padding-top: 0px">下一篇会将装配线的调度。<br /><br /></p>
<p>在我独立博客上的原文：<a href="http://www.wutianqi.com/?p=2484"><u><font color="#800080">http://www.wutianqi.com/?p=2484</font></u></a></p>
<p>欢迎大家互相学习，互相进步！</span></span></p><img src ="http://www.cppblog.com/tanky-woo/aggbug/146787.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tanky-woo/" target="_blank">Tanky Woo</a> 2011-05-20 07:27 <a href="http://www.cppblog.com/tanky-woo/archive/2011/05/20/146787.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《算法导论》学习总结 — 15. 第13章 红黑树(4)</title><link>http://www.cppblog.com/tanky-woo/archive/2011/05/12/146262.html</link><dc:creator>Tanky Woo</dc:creator><author>Tanky Woo</author><pubDate>Thu, 12 May 2011 08:33:00 GMT</pubDate><guid>http://www.cppblog.com/tanky-woo/archive/2011/05/12/146262.html</guid><wfw:comment>http://www.cppblog.com/tanky-woo/comments/146262.html</wfw:comment><comments>http://www.cppblog.com/tanky-woo/archive/2011/05/12/146262.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/tanky-woo/comments/commentRss/146262.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tanky-woo/services/trackbacks/146262.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 建议先看看前言：http://www.cppblog.com/tanky-woo/archive/2011/04/09/143794.html这一章把前面三篇的代码总结起来，然后推荐一些网上红黑树的优秀讲解资源。代码：                                    1            2            3      ...&nbsp;&nbsp;<a href='http://www.cppblog.com/tanky-woo/archive/2011/05/12/146262.html'>阅读全文</a><img src ="http://www.cppblog.com/tanky-woo/aggbug/146262.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tanky-woo/" target="_blank">Tanky Woo</a> 2011-05-12 16:33 <a href="http://www.cppblog.com/tanky-woo/archive/2011/05/12/146262.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《算法导论》学习总结 — 14. 第13章 红黑树(3)</title><link>http://www.cppblog.com/tanky-woo/archive/2011/05/11/146177.html</link><dc:creator>Tanky Woo</dc:creator><author>Tanky Woo</author><pubDate>Wed, 11 May 2011 03:52:00 GMT</pubDate><guid>http://www.cppblog.com/tanky-woo/archive/2011/05/11/146177.html</guid><wfw:comment>http://www.cppblog.com/tanky-woo/comments/146177.html</wfw:comment><comments>http://www.cppblog.com/tanky-woo/archive/2011/05/11/146177.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/tanky-woo/comments/commentRss/146177.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tanky-woo/services/trackbacks/146177.html</trackback:ping><description><![CDATA[
<p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">建议先看看前言：http://www.cppblog.com/tanky-woo/archive/2011/04/09/143794.html</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">这一篇是关于红黑树的结点删除。</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">依然和上一篇的插入一样，先用到了BST的删除结点函数，然后做相应的调整。</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">不过，这里的调整思路颇为新颖。</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">还是来看看略微改变后的删除结点函数：</p><div class="wp_syntax" style="color: rgb(17, 0, 0); background-color: rgb(249, 249, 249); border-left-color: silver; margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; overflow-x: auto; overflow-y: hidden; width: 618px; font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; "><table style="border-collapse: collapse; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; "><tbody><tr><td class="line_numbers" style="padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 238, 255); vertical-align: top; text-align: right; color: gray; overflow-x: visible; overflow-y: visible; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; width: auto; float: none; clear: none; overflow-x: visible; overflow-y: visible; font-size: 12px; line-height: 1.333; white-space: pre; ">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code" style="padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(240, 240, 240); vertical-align: top; "><pre class="cpp" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; width: auto; float: none; clear: none; overflow-x: visible; overflow-y: visible; font-size: 12px; line-height: 1.333; white-space: pre; font-family: monospace; ">Node<span style="color: rgb(0, 0, 64); ">*</span> RBTreeDelete<span style="color: rgb(0, 128, 0); ">(</span>RBTree T, Node <span style="color: rgb(0, 0, 64); ">*</span>z<span style="color: rgb(0, 128, 0); ">)</span>
<span style="color: rgb(0, 128, 0); ">{</span>
	Node <span style="color: rgb(0, 0, 64); ">*</span>x, <span style="color: rgb(0, 0, 64); ">*</span>y<span style="color: rgb(0, 128, 128); ">;</span>
	<span style="color: rgb(102, 102, 102); ">// z是要删除的节点,而y是要替换z的节点</span>
	<span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 128, 0); ">(</span>z<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>lchild <span style="color: rgb(0, 0, 128); ">==</span> <span style="color: rgb(0, 0, 255); ">NULL</span> <span style="color: rgb(0, 0, 64); ">||</span> z<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>rchild <span style="color: rgb(0, 0, 128); ">==</span> <span style="color: rgb(0, 0, 255); ">NULL</span><span style="color: rgb(0, 128, 0); ">)</span>   
		y <span style="color: rgb(0, 0, 128); ">=</span> z<span style="color: rgb(0, 128, 128); ">;</span>   <span style="color: rgb(102, 102, 102); ">// 当要删除的z至多有一个子树，则y=z；</span>
	<span style="color: rgb(0, 0, 255); ">else</span>
		y <span style="color: rgb(0, 0, 128); ">=</span> RBTreeSuccessor<span style="color: rgb(0, 128, 0); ">(</span>z<span style="color: rgb(0, 128, 0); ">)</span><span style="color: rgb(0, 128, 128); ">;</span>  <span style="color: rgb(102, 102, 102); ">// y是z的后继</span>
	<span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 128, 0); ">(</span>y<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>lchild <span style="color: rgb(0, 0, 64); ">!</span><span style="color: rgb(0, 0, 128); ">=</span> <span style="color: rgb(0, 0, 255); ">NULL</span><span style="color: rgb(0, 128, 0); ">)</span>
		x <span style="color: rgb(0, 0, 128); ">=</span> y<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>lchild<span style="color: rgb(0, 128, 128); ">;</span>  
	<span style="color: rgb(0, 0, 255); ">else</span>
		x <span style="color: rgb(0, 0, 128); ">=</span> y<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>rchild<span style="color: rgb(0, 128, 128); ">;</span>
	<span style="color: rgb(102, 102, 102); ">// 无条件执行p[x] = p[y]</span>
	x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent <span style="color: rgb(0, 0, 128); ">=</span> y<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 128, 128); ">;</span>  
	<span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 128, 0); ">(</span>y<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent <span style="color: rgb(0, 0, 128); ">==</span> <span style="color: rgb(0, 0, 255); ">NULL</span><span style="color: rgb(0, 128, 0); ">)</span>   
		T <span style="color: rgb(0, 0, 128); ">=</span> x<span style="color: rgb(0, 128, 128); ">;</span>
	<span style="color: rgb(0, 0, 255); ">else</span> <span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 128, 0); ">(</span>y <span style="color: rgb(0, 0, 128); ">==</span> y<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>lchild<span style="color: rgb(0, 128, 0); ">)</span>   
		y<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>lchild <span style="color: rgb(0, 0, 128); ">=</span> x<span style="color: rgb(0, 128, 128); ">;</span>
	<span style="color: rgb(0, 0, 255); ">else</span>
		y<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>rchild <span style="color: rgb(0, 0, 128); ">=</span> x<span style="color: rgb(0, 128, 128); ">;</span>
	<span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 128, 0); ">(</span>y <span style="color: rgb(0, 0, 64); ">!</span><span style="color: rgb(0, 0, 128); ">=</span> z<span style="color: rgb(0, 128, 0); ">)</span>
		z<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>key <span style="color: rgb(0, 0, 128); ">=</span> y<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>key<span style="color: rgb(0, 128, 128); ">;</span>
	<span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 128, 0); ">(</span>y<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">==</span> BLACK<span style="color: rgb(0, 128, 0); ">)</span>
		RBDeleteFixup<span style="color: rgb(0, 128, 0); ">(</span>T, x<span style="color: rgb(0, 128, 0); ">)</span><span style="color: rgb(0, 128, 128); ">;</span>
	<span style="color: rgb(0, 0, 255); ">return</span> y<span style="color: rgb(0, 128, 128); ">;</span>
<span style="color: rgb(0, 128, 0); ">}</span></pre></td></tr></tbody></table></div><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">注意代码倒数第二和第三行，只有当后继结点y的颜色是黑色时，才做调整。</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">由此，引导出几个问题：</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; "><span style="color: rgb(0, 0, 255); ">1.问：考虑为何当y的颜色是黑色时，才调整？当y的颜色是红黑时，会不会破坏性质4？</span></p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; "><span style="color: rgb(0, 0, 255); ">&nbsp; 答：这里我一开始纠结了，后来反复看了几次BST的删除，再算想通。在BST中，删除结点z，并不是真的把z给移除了，其实删除的不是z，而是y！因为z始终没有动过，只是把y删除了，然后把y的key赋值给z的key。所以，在红黑树中，z的颜色没有变，依然符合红黑性质。（这里我先开始理解为y-&gt;color也要赋值给z-&gt;color，汗。。。）</span></p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">2.问：考虑y为黑色时，破坏了哪几条红黑性质？</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">&nbsp;&nbsp; 答：当y是根时，且y的一个孩子是红色，若此时这个孩子成为根结点。———&gt;破坏了性质2</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当x和p[y]都是红色时。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ———&gt;破坏了性质4</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 包含y的路径中，黑高度都减少了。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ———&gt;破坏了性质5</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">解决方法：</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">上一篇我解释过，性质五涉及到了整棵树，难以控制。</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; "><span style="color: rgb(0, 0, 255); ">因此将x的颜色增加一重黑色，那么当:</span></p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; "><span style="color: rgb(0, 0, 255); ">①.x原先颜色为RED时——-&gt;x包含RED和BLACK两颜色</span></p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; "><span style="color: rgb(0, 0, 255); ">②.x原先颜色是BLACK时—&#8211;&gt;x包含BLACK, BLACK两颜色。</span></p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">此时性质5解决，但是又破坏了性质1.</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">接下来就是恢复性质1，2，4了。</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; "><span style="color: rgb(0, 0, 255); ">将额外的一重黑色一直沿树向上移，直到x是根或者是红色结点。</span></p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">看看具体的实现代码：</p><div class="wp_syntax" style="color: rgb(17, 0, 0); background-color: rgb(249, 249, 249); border-left-color: silver; margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; overflow-x: auto; overflow-y: hidden; width: 618px; font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; "><table style="border-collapse: collapse; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; "><tbody><tr><td class="line_numbers" style="padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 238, 255); vertical-align: top; text-align: right; color: gray; overflow-x: visible; overflow-y: visible; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; width: auto; float: none; clear: none; overflow-x: visible; overflow-y: visible; font-size: 12px; line-height: 1.333; white-space: pre; ">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
</pre></td><td class="code" style="padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(240, 240, 240); vertical-align: top; "><pre class="cpp" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; width: auto; float: none; clear: none; overflow-x: visible; overflow-y: visible; font-size: 12px; line-height: 1.333; white-space: pre; font-family: monospace; "><span style="color: rgb(0, 0, 255); ">void</span> RBDeleteFixup<span style="color: rgb(0, 128, 0); ">(</span>RBTree <span style="color: rgb(0, 0, 64); ">&amp;</span>T, Node <span style="color: rgb(0, 0, 64); ">*</span>x<span style="color: rgb(0, 128, 0); ">)</span>
<span style="color: rgb(0, 128, 0); ">{</span>
	<span style="color: rgb(0, 0, 255); ">while</span><span style="color: rgb(0, 128, 0); ">(</span>x <span style="color: rgb(0, 0, 64); ">!</span><span style="color: rgb(0, 0, 128); ">=</span> T <span style="color: rgb(0, 0, 64); ">&amp;&amp;</span> x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">==</span> BLACK<span style="color: rgb(0, 128, 0); ">)</span>
	<span style="color: rgb(0, 128, 0); ">{</span>
		<span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 128, 0); ">(</span>x <span style="color: rgb(0, 0, 128); ">==</span> x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>lchild<span style="color: rgb(0, 128, 0); ">)</span>
		<span style="color: rgb(0, 128, 0); ">{</span>
			Node <span style="color: rgb(0, 0, 64); ">*</span>w <span style="color: rgb(0, 0, 128); ">=</span> x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>rchild<span style="color: rgb(0, 128, 128); ">;</span>
			<span style="color: rgb(102, 102, 102); ">///////////// Case 1 /////////////</span>
			<span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 128, 0); ">(</span>w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">==</span> RED<span style="color: rgb(0, 128, 0); ">)</span>
			<span style="color: rgb(0, 128, 0); ">{</span>
				w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> BLACK<span style="color: rgb(0, 128, 128); ">;</span>
				x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> RED<span style="color: rgb(0, 128, 128); ">;</span>
				LeftRotate<span style="color: rgb(0, 128, 0); ">(</span>T, x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 128, 0); ">)</span><span style="color: rgb(0, 128, 128); ">;</span>
				w <span style="color: rgb(0, 0, 128); ">=</span> x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>rchild<span style="color: rgb(0, 128, 128); ">;</span>
			<span style="color: rgb(0, 128, 0); ">}</span>
			<span style="color: rgb(102, 102, 102); ">///////////// Case 2 /////////////</span>
			<span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 128, 0); ">(</span>w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>lchild<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">==</span> BLACK <span style="color: rgb(0, 0, 64); ">&amp;&amp;</span> w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>rchild<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">==</span> BLACK<span style="color: rgb(0, 128, 0); ">)</span>
			<span style="color: rgb(0, 128, 0); ">{</span>
				w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> RED<span style="color: rgb(0, 128, 128); ">;</span>
				x <span style="color: rgb(0, 0, 128); ">=</span> x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 128, 128); ">;</span>
			<span style="color: rgb(0, 128, 0); ">}</span>
			<span style="color: rgb(0, 0, 255); ">else</span>
			<span style="color: rgb(0, 128, 0); ">{</span>
				<span style="color: rgb(102, 102, 102); ">///////////// Case 3 /////////////</span>
				<span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 128, 0); ">(</span>w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>rchild<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">==</span> BLACK<span style="color: rgb(0, 128, 0); ">)</span>
				<span style="color: rgb(0, 128, 0); ">{</span>
					w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>lchild<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> BLACK<span style="color: rgb(0, 128, 128); ">;</span>
					w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> RED<span style="color: rgb(0, 128, 128); ">;</span>
					RightRotate<span style="color: rgb(0, 128, 0); ">(</span>T, w<span style="color: rgb(0, 128, 0); ">)</span><span style="color: rgb(0, 128, 128); ">;</span>
					w <span style="color: rgb(0, 0, 128); ">=</span> x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>rchild<span style="color: rgb(0, 128, 128); ">;</span>
				<span style="color: rgb(0, 128, 0); ">}</span>
				<span style="color: rgb(102, 102, 102); ">///////////// Case 4 /////////////</span>
				w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color<span style="color: rgb(0, 128, 128); ">;</span>
				x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> BLACK<span style="color: rgb(0, 128, 128); ">;</span>
				w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>rchild<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> BLACK<span style="color: rgb(0, 128, 128); ">;</span>
				LeftRotate<span style="color: rgb(0, 128, 0); ">(</span>T, x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 128, 0); ">)</span><span style="color: rgb(0, 128, 128); ">;</span>
				x <span style="color: rgb(0, 0, 128); ">=</span> T<span style="color: rgb(0, 128, 128); ">;</span>
			<span style="color: rgb(0, 128, 0); ">}</span>
		<span style="color: rgb(0, 128, 0); ">}</span>
		<span style="color: rgb(0, 0, 255); ">else</span>
		<span style="color: rgb(0, 128, 0); ">{</span>
			Node <span style="color: rgb(0, 0, 64); ">*</span>w <span style="color: rgb(0, 0, 128); ">=</span> x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>lchild<span style="color: rgb(0, 128, 128); ">;</span>
			<span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 128, 0); ">(</span>w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">==</span> RED<span style="color: rgb(0, 128, 0); ">)</span>
			<span style="color: rgb(0, 128, 0); ">{</span>
				w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> BLACK<span style="color: rgb(0, 128, 128); ">;</span>
				x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> RED<span style="color: rgb(0, 128, 128); ">;</span>
				RightRotate<span style="color: rgb(0, 128, 0); ">(</span>T, x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 128, 0); ">)</span><span style="color: rgb(0, 128, 128); ">;</span>
				w <span style="color: rgb(0, 0, 128); ">=</span> x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>lchild<span style="color: rgb(0, 128, 128); ">;</span>
			<span style="color: rgb(0, 128, 0); ">}</span>
			<span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 128, 0); ">(</span>w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>lchild<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">==</span> BLACK <span style="color: rgb(0, 0, 64); ">&amp;&amp;</span> w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>rchild<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">==</span> BLACK<span style="color: rgb(0, 128, 0); ">)</span>
			<span style="color: rgb(0, 128, 0); ">{</span>
				w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> RED<span style="color: rgb(0, 128, 128); ">;</span>
				x <span style="color: rgb(0, 0, 128); ">=</span> x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 128, 128); ">;</span>
			<span style="color: rgb(0, 128, 0); ">}</span>
			<span style="color: rgb(0, 0, 255); ">else</span>
			<span style="color: rgb(0, 128, 0); ">{</span>
				<span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 128, 0); ">(</span>w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>lchild<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">==</span> BLACK<span style="color: rgb(0, 128, 0); ">)</span>
				<span style="color: rgb(0, 128, 0); ">{</span>
					w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>rchild<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> BLACK<span style="color: rgb(0, 128, 128); ">;</span>
					w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> RED<span style="color: rgb(0, 128, 128); ">;</span>
					LeftRotate<span style="color: rgb(0, 128, 0); ">(</span>T, w<span style="color: rgb(0, 128, 0); ">)</span><span style="color: rgb(0, 128, 128); ">;</span>
					w <span style="color: rgb(0, 0, 128); ">=</span> x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>lchild<span style="color: rgb(0, 128, 128); ">;</span>
				<span style="color: rgb(0, 128, 0); ">}</span>
				w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color<span style="color: rgb(0, 128, 128); ">;</span>
				x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> BLACK<span style="color: rgb(0, 128, 128); ">;</span>
				w<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>lchild<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> BLACK<span style="color: rgb(0, 128, 128); ">;</span>
				RightRotate<span style="color: rgb(0, 128, 0); ">(</span>T, x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>parent<span style="color: rgb(0, 128, 0); ">)</span><span style="color: rgb(0, 128, 128); ">;</span>
				x <span style="color: rgb(0, 0, 128); ">=</span> T<span style="color: rgb(0, 128, 128); ">;</span>
			<span style="color: rgb(0, 128, 0); ">}</span>
		<span style="color: rgb(0, 128, 0); ">}</span>
	<span style="color: rgb(0, 128, 0); ">}</span>
	x<span style="color: rgb(0, 0, 64); ">-</span><span style="color: rgb(0, 0, 128); ">&gt;</span>color <span style="color: rgb(0, 0, 128); ">=</span> BLACK<span style="color: rgb(0, 128, 128); ">;</span>
<span style="color: rgb(0, 128, 0); ">}</span></pre></td></tr></tbody></table></div><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">对于删除的调整，共八种情况(左右对称各四种)，这里在书上P175面讲的很详细，所以我也就不再画图了，大家可以自己拿起笔在草稿纸上画一</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">在我独立博客上的原文：http://www.wutianqi.com/?p=2449</p><p style="line-height: 1.5; margin-top: 0px; margin-right: 0px; margin-bottom: 1.25em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; ">欢迎大家互相讨论，一起进步！</p><img src ="http://www.cppblog.com/tanky-woo/aggbug/146177.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tanky-woo/" target="_blank">Tanky Woo</a> 2011-05-11 11:52 <a href="http://www.cppblog.com/tanky-woo/archive/2011/05/11/146177.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《算法导论》学习总结 — 13. 第13章 红黑树(2)</title><link>http://www.cppblog.com/tanky-woo/archive/2011/05/08/145926.html</link><dc:creator>Tanky Woo</dc:creator><author>Tanky Woo</author><pubDate>Sun, 08 May 2011 00:26:00 GMT</pubDate><guid>http://www.cppblog.com/tanky-woo/archive/2011/05/08/145926.html</guid><wfw:comment>http://www.cppblog.com/tanky-woo/comments/145926.html</wfw:comment><comments>http://www.cppblog.com/tanky-woo/archive/2011/05/08/145926.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/tanky-woo/comments/commentRss/145926.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tanky-woo/services/trackbacks/145926.html</trackback:ping><description><![CDATA[<span class=Apple-style-span style="WORD-SPACING: 0px; FONT: medium Simsun; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BORDER-COLLAPSE: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class=Apple-style-span style="FONT-SIZE: 12px; COLOR: rgb(51,51,51); LINE-HEIGHT: 18px; FONT-FAMILY: 'Lucida Grande', Arial, Helvetica, sans-serif">
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px"><span class=Apple-style-span style="WORD-SPACING: 0px; FONT: medium Simsun; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BORDER-COLLAPSE: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class=Apple-style-span style="FONT-SIZE: 12px; COLOR: rgb(51,51,51); LINE-HEIGHT: 18px; FONT-FAMILY: 'Lucida Grande', Arial, Helvetica, sans-serif">建议先看看前言：<a href="http://www.cppblog.com/tanky-woo/archive/2011/04/09/143794.html">http://www.cppblog.com/tanky-woo/archive/2011/04/09/143794.html</a><br></span></span><br>插入结点用到了上一次BST的插入函数(做了一点添加)，并且在此基础上增加了保持红黑性质的调整函数。</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">还是先看看插入函数：</p>
<div class=wp_syntax style="BORDER-RIGHT: silver 1px solid; BORDER-TOP: silver 1px solid; OVERFLOW-Y: hidden; OVERFLOW-X: auto; MARGIN: 0px 0px 1.5em; BORDER-LEFT: silver 1px solid; WIDTH: 618px; COLOR: rgb(17,0,0); BORDER-BOTTOM: silver 1px solid; BACKGROUND-COLOR: rgb(249,249,249)">
<table style="BORDER-RIGHT: rgb(204,204,204) 1px solid; BORDER-TOP: rgb(204,204,204) 1px solid; BORDER-LEFT: rgb(204,204,204) 1px solid; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BORDER-COLLAPSE: collapse; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px">
    <tbody>
        <tr>
            <td class=line_numbers style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: rgb(204,204,204) 1px solid; OVERFLOW-Y: visible; PADDING-LEFT: 4px; OVERFLOW-X: visible; PADDING-BOTTOM: 2px; VERTICAL-ALIGN: top; BORDER-LEFT: rgb(204,204,204) 1px solid; COLOR: gray; PADDING-TOP: 2px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(221,238,255); TEXT-ALIGN: right; background-origin: initial; background-clip: initial">
            <pre style="CLEAR: none; OVERFLOW-Y: visible; FONT-SIZE: 12px; FLOAT: none; OVERFLOW-X: visible; MARGIN: 0px; WIDTH: auto; LINE-HEIGHT: 1.333; WHITE-SPACE: pre">1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            35
            36
            </pre>
            </td>
            <td class=code style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; PADDING-BOTTOM: 2px; VERTICAL-ALIGN: top; BORDER-LEFT: rgb(204,204,204) 1px solid; PADDING-TOP: 2px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(240,240,240); background-origin: initial; background-clip: initial">
            <pre class=cpp style="CLEAR: none; OVERFLOW-Y: visible; FONT-SIZE: 12px; FLOAT: none; OVERFLOW-X: visible; MARGIN: 0px; WIDTH: auto; LINE-HEIGHT: 1.333; FONT-FAMILY: monospace; WHITE-SPACE: pre"><span style="COLOR: rgb(0,0,255)">void</span> RBTreeInsert<span style="COLOR: rgb(0,128,0)">(</span>RBTree <span style="COLOR: rgb(0,0,64)">&amp;</span>T, <span style="COLOR: rgb(0,0,255)">int</span> k<span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            <span style="COLOR: rgb(102,102,102)">//T-&gt;parent-&gt;color = BLACK;</span>
            Node <span style="COLOR: rgb(0,0,64)">*</span>y <span style="COLOR: rgb(0,0,128)">=</span> <span style="COLOR: rgb(0,0,255)">NULL</span><span style="COLOR: rgb(0,128,128)">;</span>
            Node <span style="COLOR: rgb(0,0,64)">*</span>x <span style="COLOR: rgb(0,0,128)">=</span> T<span style="COLOR: rgb(0,128,128)">;</span>
            Node <span style="COLOR: rgb(0,0,64)">*</span>z <span style="COLOR: rgb(0,0,128)">=</span> <span style="COLOR: rgb(0,0,221)">new</span> Node<span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>key <span style="COLOR: rgb(0,0,128)">=</span> k<span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>lchild <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>rchild <span style="COLOR: rgb(0,0,128)">=</span> <span style="COLOR: rgb(0,0,255)">NULL</span><span style="COLOR: rgb(0,128,128)">;</span>
            &nbsp;
            <span style="COLOR: rgb(0,0,255)">while</span><span style="COLOR: rgb(0,128,0)">(</span>x <span style="COLOR: rgb(0,0,64)">!</span><span style="COLOR: rgb(0,0,128)">=</span> <span style="COLOR: rgb(0,0,255)">NULL</span><span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            y <span style="COLOR: rgb(0,0,128)">=</span> x<span style="COLOR: rgb(0,128,128)">;</span>
            &nbsp;
            <span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,128,0)">(</span>k <span style="COLOR: rgb(0,0,128)">&lt;</span> x<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>key<span style="COLOR: rgb(0,128,0)">)</span>
            x <span style="COLOR: rgb(0,0,128)">=</span> x<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>lchild<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,0,255)">else</span>
            x <span style="COLOR: rgb(0,0,128)">=</span> x<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>rchild<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            &nbsp;
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent <span style="COLOR: rgb(0,0,128)">=</span> y<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,128,0)">(</span>y <span style="COLOR: rgb(0,0,128)">==</span> <span style="COLOR: rgb(0,0,255)">NULL</span><span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            T <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,128,128)">;</span>
            T<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent <span style="COLOR: rgb(0,0,128)">=</span> <span style="COLOR: rgb(0,0,255)">NULL</span><span style="COLOR: rgb(0,128,128)">;</span>
            T<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> BLACK<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            <span style="COLOR: rgb(0,0,255)">else</span>
            <span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,128,0)">(</span>k <span style="COLOR: rgb(0,0,128)">&lt;</span> y<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>key<span style="COLOR: rgb(0,128,0)">)</span>
            y<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>lchild <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,0,255)">else</span>
            y<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>rchild <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>lchild <span style="COLOR: rgb(0,0,128)">=</span> <span style="COLOR: rgb(0,0,255)">NULL</span><span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>rchild <span style="COLOR: rgb(0,0,128)">=</span> <span style="COLOR: rgb(0,0,255)">NULL</span><span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> RED<span style="COLOR: rgb(0,128,128)">;</span>
            RBInsertFixup<span style="COLOR: rgb(0,128,0)">(</span>T, z<span style="COLOR: rgb(0,128,0)">)</span><span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span></pre>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">和上一次的BST基本没区别，只是添加了对新加结点z的颜色和子节点的处理。</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">这里把z的颜色设置为<span style="COLOR: rgb(0,0,255)">红色</span>，然后进行处理。</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px"><span style="COLOR: rgb(0,0,255)"><strong>问</strong>：考虑为何把z的颜色设置为红色</span>？</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px"><span style="COLOR: rgb(0,0,255)"><strong>答</strong>：个人认为如果设置为黑色，则破坏了性质五，而性质五关于黑高度的问题，涉及到了整棵树，全局性难以把握，所以这里设置为红色，虽然破坏了性质4，然是相对破坏性质5来说，更容易恢复红黑性质。大家如果有不同的想法，也可以留言谈谈。</span></p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">接下来，就是对整棵树的调整了。</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">答：考虑插入z结点后，破坏的哪几条红黑性质？</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">答：破坏了性质2和性质4.</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">5条红黑性质要熟记，我这里再贴出来：</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">1. 每个结点或是红色，或是是黑色。<br>2. 根结点是黑的。<br>3. 所有的叶结点(NULL)是黑色的。（NULL被视为一个哨兵结点，所有应该指向NULL的指针，都看成指向了NULL结点。）<br>4. 如果一个结点是红色的，则它的两个儿子节点都是黑色的。<br>5. 对每个结点，从该结点到其子孙结点的所有路径上包含相同数目的黑结点。</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">所以我们要做的就是恢复性质2和性质4.</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">先来看看具体实现的代码(这里只贴出部分代码)：</p>
<div class=wp_syntax style="BORDER-RIGHT: silver 1px solid; BORDER-TOP: silver 1px solid; OVERFLOW-Y: hidden; OVERFLOW-X: auto; MARGIN: 0px 0px 1.5em; BORDER-LEFT: silver 1px solid; WIDTH: 618px; COLOR: rgb(17,0,0); BORDER-BOTTOM: silver 1px solid; BACKGROUND-COLOR: rgb(249,249,249)">
<table style="BORDER-RIGHT: rgb(204,204,204) 1px solid; BORDER-TOP: rgb(204,204,204) 1px solid; BORDER-LEFT: rgb(204,204,204) 1px solid; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BORDER-COLLAPSE: collapse; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px">
    <tbody>
        <tr>
            <td class=line_numbers style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: rgb(204,204,204) 1px solid; OVERFLOW-Y: visible; PADDING-LEFT: 4px; OVERFLOW-X: visible; PADDING-BOTTOM: 2px; VERTICAL-ALIGN: top; BORDER-LEFT: rgb(204,204,204) 1px solid; COLOR: gray; PADDING-TOP: 2px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(221,238,255); TEXT-ALIGN: right; background-origin: initial; background-clip: initial">
            <pre style="CLEAR: none; OVERFLOW-Y: visible; FONT-SIZE: 12px; FLOAT: none; OVERFLOW-X: visible; MARGIN: 0px; WIDTH: auto; LINE-HEIGHT: 1.333; WHITE-SPACE: pre">1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            35
            36
            </pre>
            </td>
            <td class=code style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; PADDING-BOTTOM: 2px; VERTICAL-ALIGN: top; BORDER-LEFT: rgb(204,204,204) 1px solid; PADDING-TOP: 2px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(240,240,240); background-origin: initial; background-clip: initial">
            <pre class=cpp style="CLEAR: none; OVERFLOW-Y: visible; FONT-SIZE: 12px; FLOAT: none; OVERFLOW-X: visible; MARGIN: 0px; WIDTH: auto; LINE-HEIGHT: 1.333; FONT-FAMILY: monospace; WHITE-SPACE: pre"><span style="COLOR: rgb(0,0,255)">void</span> RBInsertFixup<span style="COLOR: rgb(0,128,0)">(</span>RBTree <span style="COLOR: rgb(0,0,64)">&amp;</span>T, Node <span style="COLOR: rgb(0,0,64)">*</span>z<span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            <span style="COLOR: rgb(0,0,255)">while</span><span style="COLOR: rgb(0,128,0)">(</span>z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">==</span> RED<span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            <span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,128,0)">(</span>z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent <span style="COLOR: rgb(0,0,128)">==</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>lchild<span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            Node <span style="COLOR: rgb(0,0,64)">*</span>y <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>rchild<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(102,102,102)">//////////// Case1 //////////////</span>
            <span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,128,0)">(</span>y<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">==</span> RED<span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> BLACK<span style="COLOR: rgb(0,128,128)">;</span>
            y<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> BLACK<span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> RED<span style="COLOR: rgb(0,128,128)">;</span>
            z <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            <span style="COLOR: rgb(0,0,255)">else</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            <span style="COLOR: rgb(102,102,102)">////////////// Case 2 //////////////</span>
            <span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,128,0)">(</span>z <span style="COLOR: rgb(0,0,128)">==</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>rchild<span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            z <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,128,128)">;</span>
            LeftRotate<span style="COLOR: rgb(0,128,0)">(</span>T, z<span style="COLOR: rgb(0,128,0)">)</span><span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            <span style="COLOR: rgb(102,102,102)">////////////// Case 3 //////////////</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> BLACK<span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> RED<span style="COLOR: rgb(0,128,128)">;</span>
            RightRotate<span style="COLOR: rgb(0,128,0)">(</span>T, z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,128,0)">)</span><span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            <span style="COLOR: rgb(0,0,255)">else</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            <span style="COLOR: rgb(102,102,102)">///////////////////////</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            T<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> BLACK<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span></pre>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">分三种情况，在代码中已用Case 1, Case 2, Case 3标记出。</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">大致说下判断情况：</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">1.首先让一个指针y指向z的<span style="COLOR: rgb(0,0,255)">叔父结点</span>(z是要删除的结点)。</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">2.如果y的颜色是<span style="COLOR: rgb(0,0,255)">红色</span>，Case 1。则使z的父亲结点和叔父结点的颜色改为黑，z的祖父结点颜色改为红。然后让z转移到z的祖父结点。</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">3.当y的颜色是<span style="COLOR: rgb(0,0,255)">黑色</span>时，</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">①.首先判断z是否是其父亲结点的右儿子，若是，则<span style="COLOR: rgb(0,0,255)">调整为左儿子</span>(用旋转)。</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">②.其次使z的父亲结点颜色为黑，z的祖父结点颜色为红，并以<span style="COLOR: rgb(0,0,255)">z的祖父结点</span>为轴右旋。</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">具体我还是在草稿纸上画出。。。(可怜买不起数码相机，只能用手机拍了。。。)</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px"><a class=highslide-image style="CURSOR: url(http://www.wutianqi.com/wp-content/plugins/auto-highslide/highslide/graphics/zoomin.cur), pointer; COLOR: rgb(49,52,40); TEXT-DECORATION: none; outline-style: none; outline-width: initial; outline-color: initial" onclick="return hs.expand(this);" href="http://www.wutianqi.com/wp-content/uploads/2011/05/rbt_charu1.jpg"><img title=rbt_charu1 style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; DISPLAY: inline; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=484 alt=rbt_charu1 src="http://www.wutianqi.com/wp-content/uploads/2011/05/rbt_charu1_thumb.jpg" width=644 border=0></a></p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px"><a class=highslide-image style="CURSOR: url(http://www.wutianqi.com/wp-content/plugins/auto-highslide/highslide/graphics/zoomin.cur), pointer; COLOR: rgb(49,52,40); TEXT-DECORATION: none; outline-style: none; outline-width: initial; outline-color: initial" onclick="return hs.expand(this);" href="http://www.wutianqi.com/wp-content/uploads/2011/05/rbt_charu2.jpg"><img title=rbt_charu2 style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; DISPLAY: inline; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=484 alt=rbt_charu2 src="http://www.wutianqi.com/wp-content/uploads/2011/05/rbt_charu2_thumb.jpg" width=644 border=0></a></p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px"><a class=highslide-image style="CURSOR: url(http://www.wutianqi.com/wp-content/plugins/auto-highslide/highslide/graphics/zoomin.cur), pointer; COLOR: rgb(49,52,40); TEXT-DECORATION: none; outline-style: none; outline-width: initial; outline-color: initial" onclick="return hs.expand(this);" href="http://www.wutianqi.com/wp-content/uploads/2011/05/rbt_charu3.jpg"><img title=rbt_charu3 style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; DISPLAY: inline; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=484 alt=rbt_charu3 src="http://www.wutianqi.com/wp-content/uploads/2011/05/rbt_charu3_thumb.jpg" width=644 border=0></a></p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">（弱弱的问一句，看见网上有很多朋友做的一些代码讲解图很给力，不知道大家有什么软件吗？求推荐。。。）</p>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">以下是插入的完整代码：</p>
<div class=wp_syntax style="BORDER-RIGHT: silver 1px solid; BORDER-TOP: silver 1px solid; OVERFLOW-Y: hidden; OVERFLOW-X: auto; MARGIN: 0px 0px 1.5em; BORDER-LEFT: silver 1px solid; WIDTH: 618px; COLOR: rgb(17,0,0); BORDER-BOTTOM: silver 1px solid; BACKGROUND-COLOR: rgb(249,249,249)">
<table style="BORDER-RIGHT: rgb(204,204,204) 1px solid; BORDER-TOP: rgb(204,204,204) 1px solid; BORDER-LEFT: rgb(204,204,204) 1px solid; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BORDER-COLLAPSE: collapse; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px">
    <tbody>
        <tr>
            <td class=line_numbers style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: rgb(204,204,204) 1px solid; OVERFLOW-Y: visible; PADDING-LEFT: 4px; OVERFLOW-X: visible; PADDING-BOTTOM: 2px; VERTICAL-ALIGN: top; BORDER-LEFT: rgb(204,204,204) 1px solid; COLOR: gray; PADDING-TOP: 2px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(221,238,255); TEXT-ALIGN: right; background-origin: initial; background-clip: initial">
            <pre style="CLEAR: none; OVERFLOW-Y: visible; FONT-SIZE: 12px; FLOAT: none; OVERFLOW-X: visible; MARGIN: 0px; WIDTH: auto; LINE-HEIGHT: 1.333; WHITE-SPACE: pre">1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            35
            36
            37
            38
            39
            40
            41
            42
            43
            44
            45
            46
            47
            48
            49
            50
            51
            52
            53
            54
            55
            56
            57
            58
            59
            60
            61
            62
            63
            64
            65
            66
            67
            68
            69
            70
            71
            72
            73
            74
            75
            76
            77
            78
            79
            80
            81
            82
            83
            84
            85
            86
            87
            88
            89
            90
            91
            </pre>
            </td>
            <td class=code style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; PADDING-BOTTOM: 2px; VERTICAL-ALIGN: top; BORDER-LEFT: rgb(204,204,204) 1px solid; PADDING-TOP: 2px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(240,240,240); background-origin: initial; background-clip: initial">
            <pre class=cpp style="CLEAR: none; OVERFLOW-Y: visible; FONT-SIZE: 12px; FLOAT: none; OVERFLOW-X: visible; MARGIN: 0px; WIDTH: auto; LINE-HEIGHT: 1.333; FONT-FAMILY: monospace; WHITE-SPACE: pre"><span style="COLOR: rgb(0,0,255)">void</span> RBInsertFixup<span style="COLOR: rgb(0,128,0)">(</span>RBTree <span style="COLOR: rgb(0,0,64)">&amp;</span>T, Node <span style="COLOR: rgb(0,0,64)">*</span>z<span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            <span style="COLOR: rgb(0,0,255)">while</span><span style="COLOR: rgb(0,128,0)">(</span>z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">==</span> RED<span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            <span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,128,0)">(</span>z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent <span style="COLOR: rgb(0,0,128)">==</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>lchild<span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            Node <span style="COLOR: rgb(0,0,64)">*</span>y <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>rchild<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(102,102,102)">//////////// Case1 //////////////</span>
            <span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,128,0)">(</span>y<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">==</span> RED<span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> BLACK<span style="COLOR: rgb(0,128,128)">;</span>
            y<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> BLACK<span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> RED<span style="COLOR: rgb(0,128,128)">;</span>
            z <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            <span style="COLOR: rgb(0,0,255)">else</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            <span style="COLOR: rgb(102,102,102)">////////////// Case 2 //////////////</span>
            <span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,128,0)">(</span>z <span style="COLOR: rgb(0,0,128)">==</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>rchild<span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            z <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,128,128)">;</span>
            LeftRotate<span style="COLOR: rgb(0,128,0)">(</span>T, z<span style="COLOR: rgb(0,128,0)">)</span><span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            <span style="COLOR: rgb(102,102,102)">////////////// Case 3 //////////////</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> BLACK<span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> RED<span style="COLOR: rgb(0,128,128)">;</span>
            RightRotate<span style="COLOR: rgb(0,128,0)">(</span>T, z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,128,0)">)</span><span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            <span style="COLOR: rgb(0,0,255)">else</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            Node <span style="COLOR: rgb(0,0,64)">*</span>y <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>lchild<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,128,0)">(</span>y<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">==</span> RED<span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> BLACK<span style="COLOR: rgb(0,128,128)">;</span>
            y<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> BLACK<span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> RED<span style="COLOR: rgb(0,128,128)">;</span>
            z <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            <span style="COLOR: rgb(0,0,255)">else</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            <span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,128,0)">(</span>z <span style="COLOR: rgb(0,0,128)">==</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>lchild<span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            z <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,128,128)">;</span>
            RightRotate<span style="COLOR: rgb(0,128,0)">(</span>T, z<span style="COLOR: rgb(0,128,0)">)</span><span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> BLACK<span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> RED<span style="COLOR: rgb(0,128,128)">;</span>
            LeftRotate<span style="COLOR: rgb(0,128,0)">(</span>T, z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,128,0)">)</span><span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            T<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> BLACK<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            &nbsp;
            <span style="COLOR: rgb(0,0,255)">void</span> RBTreeInsert<span style="COLOR: rgb(0,128,0)">(</span>RBTree <span style="COLOR: rgb(0,0,64)">&amp;</span>T, <span style="COLOR: rgb(0,0,255)">int</span> k<span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            <span style="COLOR: rgb(102,102,102)">//T-&gt;parent-&gt;color = BLACK;</span>
            Node <span style="COLOR: rgb(0,0,64)">*</span>y <span style="COLOR: rgb(0,0,128)">=</span> <span style="COLOR: rgb(0,0,255)">NULL</span><span style="COLOR: rgb(0,128,128)">;</span>
            Node <span style="COLOR: rgb(0,0,64)">*</span>x <span style="COLOR: rgb(0,0,128)">=</span> T<span style="COLOR: rgb(0,128,128)">;</span>
            Node <span style="COLOR: rgb(0,0,64)">*</span>z <span style="COLOR: rgb(0,0,128)">=</span> <span style="COLOR: rgb(0,0,221)">new</span> Node<span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>key <span style="COLOR: rgb(0,0,128)">=</span> k<span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>lchild <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>rchild <span style="COLOR: rgb(0,0,128)">=</span> <span style="COLOR: rgb(0,0,255)">NULL</span><span style="COLOR: rgb(0,128,128)">;</span>
            &nbsp;
            <span style="COLOR: rgb(0,0,255)">while</span><span style="COLOR: rgb(0,128,0)">(</span>x <span style="COLOR: rgb(0,0,64)">!</span><span style="COLOR: rgb(0,0,128)">=</span> <span style="COLOR: rgb(0,0,255)">NULL</span><span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            y <span style="COLOR: rgb(0,0,128)">=</span> x<span style="COLOR: rgb(0,128,128)">;</span>
            &nbsp;
            <span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,128,0)">(</span>k <span style="COLOR: rgb(0,0,128)">&lt;</span> x<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>key<span style="COLOR: rgb(0,128,0)">)</span>
            x <span style="COLOR: rgb(0,0,128)">=</span> x<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>lchild<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,0,255)">else</span>
            x <span style="COLOR: rgb(0,0,128)">=</span> x<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>rchild<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            &nbsp;
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent <span style="COLOR: rgb(0,0,128)">=</span> y<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,128,0)">(</span>y <span style="COLOR: rgb(0,0,128)">==</span> <span style="COLOR: rgb(0,0,255)">NULL</span><span style="COLOR: rgb(0,128,0)">)</span>
            <span style="COLOR: rgb(0,128,0)">{</span>
            T <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,128,128)">;</span>
            T<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent <span style="COLOR: rgb(0,0,128)">=</span> <span style="COLOR: rgb(0,0,255)">NULL</span><span style="COLOR: rgb(0,128,128)">;</span>
            T<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>parent<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> BLACK<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span>
            <span style="COLOR: rgb(0,0,255)">else</span>
            <span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,128,0)">(</span>k <span style="COLOR: rgb(0,0,128)">&lt;</span> y<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>key<span style="COLOR: rgb(0,128,0)">)</span>
            y<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>lchild <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,0,255)">else</span>
            y<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>rchild <span style="COLOR: rgb(0,0,128)">=</span> z<span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>lchild <span style="COLOR: rgb(0,0,128)">=</span> <span style="COLOR: rgb(0,0,255)">NULL</span><span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>rchild <span style="COLOR: rgb(0,0,128)">=</span> <span style="COLOR: rgb(0,0,255)">NULL</span><span style="COLOR: rgb(0,128,128)">;</span>
            z<span style="COLOR: rgb(0,0,64)">-</span><span style="COLOR: rgb(0,0,128)">&gt;</span>color <span style="COLOR: rgb(0,0,128)">=</span> RED<span style="COLOR: rgb(0,128,128)">;</span>
            RBInsertFixup<span style="COLOR: rgb(0,128,0)">(</span>T, z<span style="COLOR: rgb(0,128,0)">)</span><span style="COLOR: rgb(0,128,128)">;</span>
            <span style="COLOR: rgb(0,128,0)">}</span></pre>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 1.25em; LINE-HEIGHT: 1.5; PADDING-TOP: 0px">下一篇是关于红黑树的删除。<br></p>
<div class=wlWriterEditableSmartContent id=scid:0767317B-992E-4b12-91E0-4F059A8CECA8:c9e5722a-5af1-4bb7-9170-ce34126d662c style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"><br>
<p>在我独立博客上的原文：<a href="http://www.wutianqi.com/?p=2446">http://www.wutianqi.com/?p=2446</a><a href="http://www.wutianqi.com/?p=2438"><u></u></a></p>
<p>欢迎大家互相学习，互相讨论！<br></p>
</div>
</span></span>
<p>&#160;</p>
<img src ="http://www.cppblog.com/tanky-woo/aggbug/145926.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tanky-woo/" target="_blank">Tanky Woo</a> 2011-05-08 08:26 <a href="http://www.cppblog.com/tanky-woo/archive/2011/05/08/145926.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>