﻿<?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++博客-I smell magic in the air-随笔分类-数据结构</title><link>http://www.cppblog.com/izualzhy/category/18177.html</link><description>坚持 相信自己</description><language>zh-cn</language><lastBuildDate>Wed, 28 Nov 2012 10:43:21 GMT</lastBuildDate><pubDate>Wed, 28 Nov 2012 10:43:21 GMT</pubDate><ttl>60</ttl><item><title>一个趣味迷宫的问题(2011-&gt;2012)</title><link>http://www.cppblog.com/izualzhy/archive/2012/11/17/195292.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Sat, 17 Nov 2012 02:21:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/11/17/195292.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/195292.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/11/17/195292.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/195292.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/195292.html</trackback:ping><description><![CDATA[<p> </p><div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a44a8214-7cc8-478d-9e1c-69ef4d4bd075" class="wlWriterEditableSmartContent">Technorati 标记: <a href="http://technorati.com/tags/%e8%bf%b7%e5%ae%ab" rel="tag">迷宫</a>,<a href="http://technorati.com/tags/bfs" rel="tag">bfs</a></div><p>&nbsp;</p> <p>原题在这里：</p> <p><a title="http://www2.stetson.edu/~efriedma/holiday/2011/index.html" href="http://www2.stetson.edu/~efriedma/holiday/2011/index.html">http://www2.stetson.edu/~efriedma/holiday/2011/index.html</a></p> <p>&nbsp;</p> <p>题目：</p> <p>有这样一个迷宫，从2011开始，不能回头，只能&#8220;朝前&#8221;走，走出迷宫的时候需要变成2012.</p> <p><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/659790e41d92_12571/image_2.png"><img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/659790e41d92_12571/image_thumb.png" width="244" height="89" /></a></p> <p>例如从2011开始，有+7,/2两种选择，选择/2后，又有+7,x3/-5三种选择。</p> <p>&nbsp;</p> <p>解法：</p> <p>其实就是2011为根的子树，不断的生成下一层的节点，直到节点值为2012。</p> <p>dfs遍历肯定不现实的，bfs算是比较适合的方法了。不过有更快捷的办法，我使用的是最原始的bfs。</p> <p>运算方式有四种，位置有三种，分别用枚举表示。</p> <p>&nbsp;</p> <p>代码：</p> <p>代码写的很长了，100行以内应该是没问题的，即使是使用我的这种笨方法o(&#9583;&#9633;&#9584;)o。为了条理清楚些，就多定义了些函数。</p> <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:09681f78-7dc0-47d3-ac3a-8e93d661826b" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px">Code Snippet</div> <div style="background: #fff; max-height: 500px; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"> <li><span style="color:#937a42">/*</span></li> <li style="background: #f3f3f3"><span style="color:#937a42"> * =====================================================================================</span></li> <li><span style="color:#937a42"> *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Filename:&nbsp;&nbsp;puzzle.cpp</span></li> <li style="background: #f3f3f3"><span style="color:#937a42"> *&nbsp;&nbsp;&nbsp;&nbsp;Description:&nbsp;&nbsp;</span></li> <li><span style="color:#937a42"> *</span></li> <li style="background: #f3f3f3"><span style="color:#937a42"> *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Version:&nbsp;&nbsp;1.0</span></li> <li><span style="color:#937a42"> *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Created:&nbsp;&nbsp;11/16/2012 04:34:49 PM</span></li> <li style="background: #f3f3f3"><span style="color:#937a42"> *</span></li> <li><span style="color:#937a42"> *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Author:&nbsp;&nbsp;zhy (), izualzhy@163.com</span></li> <li style="background: #f3f3f3"><span style="color:#937a42"> * =====================================================================================</span></li> <li><span style="color:#937a42"> */</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="color:#400080">#include</span> <span style="color:#259241">&lt;iostream&gt;</span></li> <li style="background: #f3f3f3"><span style="color:#400080">#include</span> <span style="color:#259241">&lt;vector&gt;</span></li> <li><span style="color:#400080">#include</span> <span style="color:#259241">&lt;queue&gt;</span></li> <li style="background: #f3f3f3"><span style="color:#400080">#include</span> <span style="color:#259241">&lt;string&gt;</span></li> <li><span style="color:#400080">using</span> <span style="color:#400080">namespace</span> std<span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="color:#400080">class</span> PuzzleGuessor {</li> <li style="background: #f3f3f3"><span style="color:#400080">public</span><span style="color:#2e53d1">:</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#937a42">/*可选的数学运算*/</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">enum</span> Op {</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OpNone<span style="color:#2e53d1">,</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddSeven<span style="color:#2e53d1">,</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DivTwo<span style="color:#2e53d1">,</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MultiThree<span style="color:#2e53d1">,</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SubFive<span style="color:#2e53d1">,</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OpCount</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;}<span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#937a42">/*位置，由位置和数学运算则可以推断下一步可选的运算方式*/</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">enum</span> Position {</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Left<span style="color:#2e53d1">,</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Middle<span style="color:#2e53d1">,</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Right</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;}<span style="color:#2e53d1">;</span></li> <li>&nbsp;</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#937a42">/*节点，记录当前的计算结果，位置和到达位置前的运算方式 */</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">struct</span> TreeNode {</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeNode<span style="color:#2e53d1">(</span><span style="color:#400080">double</span> d<span style="color:#2e53d1">,</span> Op op<span style="color:#2e53d1">,</span> Position position<span style="color:#2e53d1">)</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e53d1">:</span> data<span style="color:#2e53d1">(</span>d<span style="color:#2e53d1">)</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e53d1">,</span> oper<span style="color:#2e53d1">(</span>op<span style="color:#2e53d1">)</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e53d1">,</span> pos<span style="color:#2e53d1">(</span>position<span style="color:#2e53d1">)</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li style="background: #f3f3f3">&nbsp;</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">double</span> data<span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Op oper<span style="color:#2e53d1">;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Position pos<span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vector<span style="color:#2e53d1">&lt;</span>TreeNode<span style="color:#2e53d1">*&gt;</span> children<span style="color:#2e53d1">;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;}<span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;PuzzleGuessor<span style="color:#2e53d1">();</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">void</span> BuildTree<span style="color:#2e53d1">();</span><span style="color:#937a42">//构造树</span></li> <li>&nbsp;</li> <li style="background: #f3f3f3"><span style="color:#400080">private</span><span style="color:#2e53d1">:</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;queue<span style="color:#2e53d1">&lt;</span>TreeNode<span style="color:#2e53d1">*&gt;</span> q<span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;TreeNode<span style="color:#2e53d1">*</span> root<span style="color:#2e53d1">;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;string OpTable<span style="color:#2e53d1">[</span>OpCount<span style="color:#2e53d1">];</span><span style="color:#937a42">//运算对应的字符串表，输出用</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;string result<span style="color:#2e53d1">;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">bool</span> CreateNewChildForNode<span style="color:#2e53d1">(</span>TreeNode<span style="color:#2e53d1">*</span> node<span style="color:#2e53d1">);</span><span style="color:#937a42">//由节点处根据下一步可进行的运算产生下一层节点</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">bool</span> CalcNextFromLeft<span style="color:#2e53d1">(</span>TreeNode<span style="color:#2e53d1">*</span> node<span style="color:#2e53d1">);</span><span style="color:#937a42">//在左端时可能的节点</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">bool</span> CalcNextFromMiddle<span style="color:#2e53d1">(</span>TreeNode<span style="color:#2e53d1">*</span> node<span style="color:#2e53d1">);</span><span style="color:#937a42">//中间位置</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">bool</span> CalcNextFromRight<span style="color:#2e53d1">(</span>TreeNode<span style="color:#2e53d1">*</span> node<span style="color:#2e53d1">);</span><span style="color:#937a42">//右端</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">bool</span> Achieve2012<span style="color:#2e53d1">(</span>TreeNode<span style="color:#2e53d1">*</span> node<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">bool</span> Find<span style="color:#2e53d1">(</span>TreeNode<span style="color:#2e53d1">*</span> node<span style="color:#2e53d1">,</span> TreeNode<span style="color:#2e53d1">*</span> objNode<span style="color:#2e53d1">);</span></li> <li>}<span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li>PuzzleGuessor<span style="color:#2e53d1">::</span>PuzzleGuessor<span style="color:#2e53d1">()</span></li> <li style="background: #f3f3f3">{</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;root <span style="color:#2e53d1">=</span> <span style="color:#400080">new</span> TreeNode<span style="color:#2e53d1">(</span><span style="color:#259241">2011.0</span><span style="color:#2e53d1">,</span> OpNone<span style="color:#2e53d1">,</span> Left<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;TreeNode<span style="color:#2e53d1">*</span> child1 <span style="color:#2e53d1">=</span> <span style="color:#400080">new</span> TreeNode<span style="color:#2e53d1">(</span>root<span style="color:#2e53d1">-&gt;</span>data <span style="color:#2e53d1">+</span> <span style="color:#259241">7</span><span style="color:#2e53d1">,</span> AddSeven<span style="color:#2e53d1">,</span> Middle<span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;TreeNode<span style="color:#2e53d1">*</span> child2 <span style="color:#2e53d1">=</span> <span style="color:#400080">new</span> TreeNode<span style="color:#2e53d1">(</span>root<span style="color:#2e53d1">-&gt;</span>data <span style="color:#2e53d1">/</span> <span style="color:#259241">2</span><span style="color:#2e53d1">,</span> DivTwo<span style="color:#2e53d1">,</span> Middle<span style="color:#2e53d1">);</span> </li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;root<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>push_back<span style="color:#2e53d1">(</span>child1<span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;root<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>push_back<span style="color:#2e53d1">(</span>child2<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;q<span style="color:#2e53d1">.</span>push<span style="color:#2e53d1">(</span>child1<span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;q<span style="color:#2e53d1">.</span>push<span style="color:#2e53d1">(</span>child2<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;OpTable<span style="color:#2e53d1">[</span>OpNone<span style="color:#2e53d1">]</span> <span style="color:#2e53d1">=</span> <span style="color:#259241">"none"</span><span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;OpTable<span style="color:#2e53d1">[</span>AddSeven<span style="color:#2e53d1">]</span> <span style="color:#2e53d1">=</span> <span style="color:#259241">"+7"</span><span style="color:#2e53d1">;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;OpTable<span style="color:#2e53d1">[</span>DivTwo<span style="color:#2e53d1">]</span> <span style="color:#2e53d1">=</span> <span style="color:#259241">"/2"</span><span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;OpTable<span style="color:#2e53d1">[</span>MultiThree<span style="color:#2e53d1">]</span> <span style="color:#2e53d1">=</span> <span style="color:#259241">"x3"</span><span style="color:#2e53d1">;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;OpTable<span style="color:#2e53d1">[</span>SubFive<span style="color:#2e53d1">]</span> <span style="color:#2e53d1">=</span> <span style="color:#259241">"-5"</span><span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;BuildTree<span style="color:#2e53d1">();</span></li> <li>}</li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="color:#400080">void</span> PuzzleGuessor<span style="color:#2e53d1">::</span>BuildTree<span style="color:#2e53d1">()</span></li> <li style="background: #f3f3f3">{</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;result<span style="color:#2e53d1">.</span>clear<span style="color:#2e53d1">();</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">while</span> <span style="color:#2e53d1">(!</span>q<span style="color:#2e53d1">.</span>empty<span style="color:#2e53d1">())</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeNode<span style="color:#2e53d1">*</span> node <span style="color:#2e53d1">=</span> q<span style="color:#2e53d1">.</span>front<span style="color:#2e53d1">();</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CreateNewChildForNode<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">for</span> <span style="color:#2e53d1">(</span> <span style="color:#400080">int</span> i<span style="color:#2e53d1">=</span><span style="color:#259241">0</span><span style="color:#2e53d1">;</span> i<span style="color:#2e53d1">&lt;</span>node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>size<span style="color:#2e53d1">();</span> <span style="color:#2e53d1">++</span>i<span style="color:#2e53d1">)</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">if</span> <span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">[</span>i<span style="color:#2e53d1">]-&gt;</span>data <span style="color:#2e53d1">-</span> <span style="color:#259241">2012</span> <span style="color:#2e53d1">&lt;</span> <span style="color:#259241">1e-6</span> <span style="color:#2e53d1">&amp;&amp;</span> <span style="color:#259241">2012</span> <span style="color:#2e53d1">-</span> node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">[</span>i<span style="color:#2e53d1">]-&gt;</span>data <span style="color:#2e53d1">&lt;</span> <span style="color:#259241">1e-6</span><span style="color:#2e53d1">)</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout <span style="color:#2e53d1">&lt;&lt;</span> <span style="color:#259241">"Achieve 2012!\t"</span> <span style="color:#2e53d1">&lt;&lt;</span> node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">[</span>i<span style="color:#2e53d1">]-&gt;</span>data <span style="color:#2e53d1">&lt;&lt;</span> endl<span style="color:#2e53d1">;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Find<span style="color:#2e53d1">(</span>root<span style="color:#2e53d1">,</span> node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">[</span>i<span style="color:#2e53d1">]);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout <span style="color:#2e53d1">&lt;&lt;</span> result <span style="color:#2e53d1">&lt;&lt;</span> endl<span style="color:#2e53d1">;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result<span style="color:#2e53d1">.</span>clear<span style="color:#2e53d1">();</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#937a42">///*如果不retunr，则会一直计算下去&nbsp;&nbsp;&nbsp;*/</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#937a42">//return;</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q<span style="color:#2e53d1">.</span>push<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">[</span>i<span style="color:#2e53d1">]);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q<span style="color:#2e53d1">.</span>pop<span style="color:#2e53d1">();</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li>}</li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="color:#400080">bool</span> PuzzleGuessor<span style="color:#2e53d1">::</span>Find<span style="color:#2e53d1">(</span>TreeNode<span style="color:#2e53d1">*</span> node<span style="color:#2e53d1">,</span> TreeNode<span style="color:#2e53d1">*</span> objNode<span style="color:#2e53d1">)</span></li> <li style="background: #f3f3f3">{</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">if</span> <span style="color:#2e53d1">(</span>node <span style="color:#2e53d1">==</span> NULL<span style="color:#2e53d1">)</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">return</span> <span style="color:#400080">false</span><span style="color:#2e53d1">;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">if</span> <span style="color:#2e53d1">(</span>node <span style="color:#2e53d1">==</span> objNode<span style="color:#2e53d1">)</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result <span style="color:#2e53d1">=</span> OpTable<span style="color:#2e53d1">[</span>node<span style="color:#2e53d1">-&gt;</span>oper<span style="color:#2e53d1">];</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">return</span> <span style="color:#400080">true</span><span style="color:#2e53d1">;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">for</span> <span style="color:#2e53d1">(</span> <span style="color:#400080">int</span> i<span style="color:#2e53d1">=</span><span style="color:#259241">0</span><span style="color:#2e53d1">;</span> i<span style="color:#2e53d1">&lt;</span>node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>size<span style="color:#2e53d1">();</span> <span style="color:#2e53d1">++</span>i<span style="color:#2e53d1">)</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">if</span><span style="color:#2e53d1">(</span> Find<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">[</span>i<span style="color:#2e53d1">],</span> objNode<span style="color:#2e53d1">)</span> <span style="color:#2e53d1">)</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#937a42">//cout &lt;&lt; node-&gt;data &lt;&lt; endl;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">if</span> <span style="color:#2e53d1">(</span>OpNone <span style="color:#2e53d1">==</span> node<span style="color:#2e53d1">-&gt;</span>oper<span style="color:#2e53d1">)</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result<span style="color:#2e53d1">.</span>insert<span style="color:#2e53d1">(</span><span style="color:#259241">0</span><span style="color:#2e53d1">,</span> <span style="color:#259241">"2011"</span><span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">else</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result<span style="color:#2e53d1">.</span>insert<span style="color:#2e53d1">(</span><span style="color:#259241">0</span><span style="color:#2e53d1">,</span>OpTable<span style="color:#2e53d1">[</span>node<span style="color:#2e53d1">-&gt;</span>oper<span style="color:#2e53d1">]);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">return</span> <span style="color:#400080">true</span><span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li style="background: #f3f3f3">&nbsp;</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">return</span> <span style="color:#400080">false</span><span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">}</li> <li>&nbsp;</li> <li style="background: #f3f3f3"><span style="color:#400080">bool</span> PuzzleGuessor<span style="color:#2e53d1">::</span>CreateNewChildForNode<span style="color:#2e53d1">(</span>TreeNode<span style="color:#2e53d1">*</span> node<span style="color:#2e53d1">)</span></li> <li>{</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">if</span> <span style="color:#2e53d1">(</span>node <span style="color:#2e53d1">==</span> NULL<span style="color:#2e53d1">)</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">return</span> <span style="color:#400080">false</span><span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">switch</span> <span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>pos<span style="color:#2e53d1">)</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">case</span> Left<span style="color:#2e53d1">:</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CalcNextFromLeft<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">break</span><span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">case</span> Middle<span style="color:#2e53d1">:</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CalcNextFromMiddle<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">break</span><span style="color:#2e53d1">;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">case</span> Right<span style="color:#2e53d1">:</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CalcNextFromRight<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">break</span><span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li>}</li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="color:#400080">bool</span> PuzzleGuessor<span style="color:#2e53d1">::</span>CalcNextFromLeft<span style="color:#2e53d1">(</span>TreeNode<span style="color:#2e53d1">*</span> node<span style="color:#2e53d1">)</span></li> <li style="background: #f3f3f3">{</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">if</span> <span style="color:#2e53d1">(</span>node <span style="color:#2e53d1">==</span> NULL<span style="color:#2e53d1">)</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">return</span> <span style="color:#400080">false</span><span style="color:#2e53d1">;</span></li> <li>&nbsp;</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">switch</span> <span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>oper<span style="color:#2e53d1">)</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">case</span> AddSeven<span style="color:#2e53d1">:</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeNode<span style="color:#2e53d1">*</span> newNode <span style="color:#2e53d1">=</span> <span style="color:#400080">new</span> TreeNode<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>data <span style="color:#2e53d1">/</span> <span style="color:#259241">2</span><span style="color:#2e53d1">,</span> DivTwo<span style="color:#2e53d1">,</span> Middle<span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>push_back<span style="color:#2e53d1">(</span>newNode<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">break</span><span style="color:#2e53d1">;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">case</span> DivTwo<span style="color:#2e53d1">:</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeNode<span style="color:#2e53d1">*</span> newNode <span style="color:#2e53d1">=</span> <span style="color:#400080">new</span> TreeNode<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>data <span style="color:#2e53d1">+</span> <span style="color:#259241">7</span><span style="color:#2e53d1">,</span> AddSeven<span style="color:#2e53d1">,</span> Middle<span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>push_back<span style="color:#2e53d1">(</span>newNode<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">break</span><span style="color:#2e53d1">;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">default</span><span style="color:#2e53d1">:</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">return</span> <span style="color:#400080">false</span><span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li>&nbsp;</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">return</span> <span style="color:#400080">true</span><span style="color:#2e53d1">;</span></li> <li>}</li> <li style="background: #f3f3f3"><span style="color:#400080">bool</span> PuzzleGuessor<span style="color:#2e53d1">::</span>CalcNextFromMiddle<span style="color:#2e53d1">(</span>TreeNode<span style="color:#2e53d1">*</span> node<span style="color:#2e53d1">)</span></li> <li>{</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">if</span> <span style="color:#2e53d1">(</span>node <span style="color:#2e53d1">==</span> NULL<span style="color:#2e53d1">)</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">return</span> <span style="color:#400080">false</span><span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">if</span> <span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>oper <span style="color:#2e53d1">==</span> AddSeven <span style="color:#2e53d1">||</span> node<span style="color:#2e53d1">-&gt;</span>oper <span style="color:#2e53d1">==</span> DivTwo<span style="color:#2e53d1">)</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeNode<span style="color:#2e53d1">*</span> newNode <span style="color:#2e53d1">=</span> <span style="color:#400080">new</span> TreeNode<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>data <span style="color:#2e53d1">*</span> <span style="color:#259241">3</span><span style="color:#2e53d1">,</span> MultiThree<span style="color:#2e53d1">,</span> Right<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>push_back<span style="color:#2e53d1">(</span>newNode<span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newNode <span style="color:#2e53d1">=</span> <span style="color:#400080">new</span> TreeNode<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>data <span style="color:#2e53d1">-</span> <span style="color:#259241">5</span><span style="color:#2e53d1">,</span> SubFive<span style="color:#2e53d1">,</span> Right<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>push_back<span style="color:#2e53d1">(</span>newNode<span style="color:#2e53d1">);</span></li> <li>&nbsp;</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">if</span> <span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>oper <span style="color:#2e53d1">==</span> AddSeven<span style="color:#2e53d1">)</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeNode<span style="color:#2e53d1">*</span> newNode <span style="color:#2e53d1">=</span> <span style="color:#400080">new</span> TreeNode<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>data <span style="color:#2e53d1">/</span> <span style="color:#259241">2</span><span style="color:#2e53d1">,</span> DivTwo<span style="color:#2e53d1">,</span> Left<span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>push_back<span style="color:#2e53d1">(</span>newNode<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">else</span> <span style="color:#400080">if</span> <span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>oper <span style="color:#2e53d1">==</span> DivTwo<span style="color:#2e53d1">)</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeNode<span style="color:#2e53d1">*</span> newNode <span style="color:#2e53d1">=</span> <span style="color:#400080">new</span> TreeNode<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>data <span style="color:#2e53d1">+</span> <span style="color:#259241">7</span><span style="color:#2e53d1">,</span> AddSeven<span style="color:#2e53d1">,</span> Left<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>push_back<span style="color:#2e53d1">(</span>newNode<span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">else</span> <span style="color:#400080">if</span> <span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>oper <span style="color:#2e53d1">==</span> MultiThree <span style="color:#2e53d1">||</span> node<span style="color:#2e53d1">-&gt;</span>oper <span style="color:#2e53d1">==</span> SubFive<span style="color:#2e53d1">)</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeNode<span style="color:#2e53d1">*</span> newNode <span style="color:#2e53d1">=</span> <span style="color:#400080">new</span> TreeNode<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>data <span style="color:#2e53d1">+</span> <span style="color:#259241">7</span><span style="color:#2e53d1">,</span> AddSeven<span style="color:#2e53d1">,</span> Left<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>push_back<span style="color:#2e53d1">(</span>newNode<span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newNode <span style="color:#2e53d1">=</span> <span style="color:#400080">new</span> TreeNode<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>data <span style="color:#2e53d1">/</span> <span style="color:#259241">2</span><span style="color:#2e53d1">,</span> DivTwo <span style="color:#2e53d1">,</span>Left<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>push_back<span style="color:#2e53d1">(</span>newNode<span style="color:#2e53d1">);</span></li> <li>&nbsp;</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">if</span> <span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>oper <span style="color:#2e53d1">==</span> MultiThree<span style="color:#2e53d1">)</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeNode<span style="color:#2e53d1">*</span> newNode <span style="color:#2e53d1">=</span> <span style="color:#400080">new</span> TreeNode<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>data <span style="color:#2e53d1">-</span> <span style="color:#259241">5</span><span style="color:#2e53d1">,</span> DivTwo<span style="color:#2e53d1">,</span> Right<span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>push_back<span style="color:#2e53d1">(</span>newNode<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">else</span> <span style="color:#400080">if</span> <span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>oper <span style="color:#2e53d1">==</span> SubFive<span style="color:#2e53d1">)</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeNode<span style="color:#2e53d1">*</span> newNode <span style="color:#2e53d1">=</span> <span style="color:#400080">new</span> TreeNode<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>data <span style="color:#2e53d1">*</span> <span style="color:#259241">3</span><span style="color:#2e53d1">,</span> MultiThree<span style="color:#2e53d1">,</span> Right<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>push_back<span style="color:#2e53d1">(</span>newNode<span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">else</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">return</span> <span style="color:#400080">false</span><span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li>&nbsp;</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">return</span> <span style="color:#400080">true</span><span style="color:#2e53d1">;</span></li> <li>}</li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="color:#400080">bool</span> PuzzleGuessor<span style="color:#2e53d1">::</span>CalcNextFromRight<span style="color:#2e53d1">(</span>TreeNode<span style="color:#2e53d1">*</span> node<span style="color:#2e53d1">)</span></li> <li style="background: #f3f3f3">{</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">if</span> <span style="color:#2e53d1">(</span>node <span style="color:#2e53d1">==</span> NULL<span style="color:#2e53d1">)</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">return</span> <span style="color:#400080">false</span><span style="color:#2e53d1">;</span></li> <li>&nbsp;</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">switch</span> <span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>oper<span style="color:#2e53d1">)</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">case</span> MultiThree<span style="color:#2e53d1">:</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeNode<span style="color:#2e53d1">*</span> newNode <span style="color:#2e53d1">=</span> <span style="color:#400080">new</span> TreeNode<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>data <span style="color:#2e53d1">-</span> <span style="color:#259241">5</span><span style="color:#2e53d1">,</span> SubFive<span style="color:#2e53d1">,</span> Middle<span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>push_back<span style="color:#2e53d1">(</span>newNode<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">break</span><span style="color:#2e53d1">;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">case</span> SubFive<span style="color:#2e53d1">:</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeNode<span style="color:#2e53d1">*</span> newNode <span style="color:#2e53d1">=</span> <span style="color:#400080">new</span> TreeNode<span style="color:#2e53d1">(</span>node<span style="color:#2e53d1">-&gt;</span>data <span style="color:#2e53d1">*</span> <span style="color:#259241">3</span><span style="color:#2e53d1">,</span> MultiThree<span style="color:#2e53d1">,</span> Middle<span style="color:#2e53d1">);</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node<span style="color:#2e53d1">-&gt;</span>children<span style="color:#2e53d1">.</span>push_back<span style="color:#2e53d1">(</span>newNode<span style="color:#2e53d1">);</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">break</span><span style="color:#2e53d1">;</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">default</span><span style="color:#2e53d1">:</span></li> <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">return</span> <span style="color:#400080">false</span><span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;}</li> <li>&nbsp;</li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">return</span> <span style="color:#400080">true</span><span style="color:#2e53d1">;</span></li> <li>}</li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="color:#400080">int</span> main<span style="color:#2e53d1">()</span></li> <li style="background: #f3f3f3">{</li> <li>&nbsp;&nbsp;&nbsp;&nbsp;PuzzleGuessor guesser<span style="color:#2e53d1">;</span></li> <li style="background: #f3f3f3">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080">return</span> <span style="color:#259241">0</span><span style="color:#2e53d1">;</span></li> <li>}</li> </ol> </div> </div> </div> <p>部分输出：</p> <p>Achieve 2012!&nbsp;&nbsp;&nbsp; 2012<br />2011+7/2+7/2+7x3-5/2+7x3-5/2+7x3-5x3-5/2+7/2+7/2+7/2+7x3-5+7<br />Achieve 2012!&nbsp;&nbsp;&nbsp; 2012<br />2011+7/2+7/2+7x3-5/2+7x3-5/2+7-5x3+7/2+7/2+7/2+7/2x3-5x3-5+7<br />Achieve 2012!&nbsp;&nbsp;&nbsp; 2012<br />2011+7/2+7/2+7x3-5x3-5+7/2+7/2+7/2+7/2x3-5/2+7x3-5x3-5+7/2+7<br />Achieve 2012!&nbsp;&nbsp;&nbsp; 2012<br />2011+7/2+7/2+7x3-5x3-5+7/2+7/2+7/2+7/2x3-5/2+7-5x3/2+7x3-5+7<br />Achieve 2012!&nbsp;&nbsp;&nbsp; 2012<br />2011+7/2+7/2+7-5x3/2+7x3-5+7/2+7/2+7/2x3-5/2+7x3-5x3-5+7/2+7<br />Achieve 2012!&nbsp;&nbsp;&nbsp; 2012<br />2011+7/2+7/2+7-5x3/2+7x3-5+7/2+7/2+7/2x3-5/2+7-5x3/2+7x3-5+7<br />Achieve 2012!&nbsp;&nbsp;&nbsp; 2012<br />2011+7/2+7/2+7-5x3/2+7/2+7x3-5/2+7-5x3+7/2+7/2x3-5x3-5+7/2+7<br />Achieve 2012!&nbsp;&nbsp;&nbsp; 2012<br />2011+7/2+7/2+7-5x3/2+7/2+7x3-5/2+7-5x3+7/2+7/2-5x3/2+7x3-5+7<br />Achieve 2012!&nbsp;&nbsp;&nbsp; 2012<br />2011+7/2+7/2+7-5x3/2+7/2+7-5x3/2+7x3-5/2+7x3-5+7/2+7/2x3-5+7</p><img src ="http://www.cppblog.com/izualzhy/aggbug/195292.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-11-17 10:21 <a href="http://www.cppblog.com/izualzhy/archive/2012/11/17/195292.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]Knuth-Morris-Pratt algorithm</title><link>http://www.cppblog.com/izualzhy/archive/2012/09/15/190755.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Sat, 15 Sep 2012 06:07:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/09/15/190755.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/190755.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/09/15/190755.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/190755.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/190755.html</trackback:ping><description><![CDATA[<p>看到实在是觉得写得不错，就就转过来了。</p> <p>原文地址：</p> <p><a href="http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/kmpen.htm">http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/kmpen.htm</a></p> <p>以下为原文内容：</p> <h5><a name="section1">Idea</a></h5> <p>After a shift of the pattern, the naive algorithm has forgotten all information about previously matched symbols. So it is possible that it re-compares a text symbol with different pattern symbols again and again. This leads to its worst case complexity of Θ(<var>nm</var>) (<var>n</var>: length of the text, <var>m</var>: length of the pattern). <p>The algorithm of Knuth, Morris and Pratt <a>[KMP 77]</a> makes use of the information gained by previous symbol comparisons. It never re-compares a text symbol that has matched a pattern symbol. As a result, the complexity of the searching phase of the Knuth-Morris-Pratt algorithm is in <var>O</var>(<var>n</var>). <p>However, a preprocessing of the pattern is necessary in order to analyze its structure. The preprocessing phase has a complexity of<var>O</var>(<var>m</var>). Since <var>m</var><img alt="&lt;=" src="http://www.inf.fh-flensburg.de/lang/symbols/le.gif"><var>n</var>, the overall complexity of the Knuth-Morris-Pratt algorithm is in <var>O</var>(<var>n</var>). <h5><a name="section2">Basic definitions</a></h5> <p>Definition:&nbsp; Let <var>A</var> be an alphabet and&nbsp; <var>x</var> = <var>x</var><sub>0</sub> ... <var>x</var><sub><var>k</var>-1,</sub> <var>k</var> <img alt="element" src="http://www.inf.fh-flensburg.de/lang/symbols/elem.gif"> <img alt="natural numbers" src="http://www.inf.fh-flensburg.de/lang/symbols/n.gif">&nbsp;&nbsp; a string of length <var>k</var> over <var>A</var>. <p>A prefix of <var>x</var> is a substring <var>u</var> with <p><var>u</var>&nbsp; =&nbsp; <var>x</var><sub>0</sub> ... <var>x</var><sub><var>b</var>-1</sub>&nbsp;&nbsp; where&nbsp; <var>b</var> <img alt="element" src="http://www.inf.fh-flensburg.de/lang/symbols/elem.gif"> {0, ..., <var>k</var>} <p>i.e. <var>x</var> starts with <var>u</var>. <p>A suffix of <var>x</var> is a substring <var>u</var> with <p><var>u</var>&nbsp; =&nbsp; <var>x</var><sub><var>k</var>-<var>b</var></sub> ... <var>x</var><sub><var>k</var>-1</sub>&nbsp;&nbsp; where&nbsp; <var>b</var> <img alt="element" src="http://www.inf.fh-flensburg.de/lang/symbols/elem.gif"> {0, ..., <var>k</var>} <p>i.e. <var>x</var> ends with <var>u</var>. <p>A prefix <var>u</var> of <var>x</var> or a suffix <var>u</var> of <var>x</var> is called a proper prefix or suffix, respectively, if <var>u</var><img alt="not equal" src="http://www.inf.fh-flensburg.de/lang/symbols/ne.gif"><var>x</var>, i.e. if its length <var>b</var> is less than <var>k</var>. <p>A border of <var>x</var> is a substring <var>r</var> with <p><var>r</var>&nbsp; =&nbsp; <var>x</var><sub>0</sub> ... <var>x</var><sub><var>b</var>-1</sub>&nbsp; and&nbsp; <var>r</var>&nbsp; =&nbsp; <var>x</var><sub><var>k</var>-<var>b</var></sub> ... <var>x</var><sub><var>k</var>-1</sub>&nbsp;&nbsp; where&nbsp; <var>b</var> <img alt="element" src="http://www.inf.fh-flensburg.de/lang/symbols/elem.gif"> {0, ..., <var>k</var>-1} <p>A border of <var>x</var> is a substring that is both proper prefix and proper suffix of <var>x</var>. We call its length <var>b</var> the width of the border. <p>Example:&nbsp; Let <var>x</var> = abacab. The proper prefixes of <var>x</var> are <p>ε, a, ab, aba, abac, abaca <p>The proper suffixes of <var>x</var> are <p>ε, b, ab, cab, acab, bacab <p>The borders of <var>x</var> are <p>ε, ab <p>The border ε has width 0, the border ab has width 2. <p>The empty string ε is always a border of <var>x</var>, for all <var>x</var> <img alt="element" src="http://www.inf.fh-flensburg.de/lang/symbols/elem.gif"> <var>A</var><sup><small>+</small></sup>. The empty string ε itself has no border. <p>The following example illustrates how the shift distance in the Knuth-Morris-Pratt algorithm is determined using the notion of the border of a string. <p>Example: <p><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/a2e7d706822b_C45A/image_5.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/a2e7d706822b_C45A/image_thumb_1.png" width="243" height="80"></a> <p>The symbols at positions 0, ..., 4 have matched. Comparison c-d at position 5 yields a mismatch. The pattern can be shifted by 3 positions, and comparisons are resumed at position 5. <p>The shift distance is determined by the widest border of the matching prefix of <var>p</var>. In this example, the matching prefix is abcab, its length is <var>j</var> = 5. Its widest border is ab of width <var>b</var> = 2. The shift distance is <var>j</var> – <var>b</var>&nbsp; =&nbsp; 5 – 2&nbsp; =&nbsp; 3. <p>In the preprocessing phase, the width of the widest border of each prefix of the pattern is determined. Then in the search phase, the shift distance can be computed according to the prefix that has matched. <h5><a name="section3">Preprocessing</a></h5> <p>Theorem:&nbsp; Let <var>r</var>, <var>s</var> be borders of a string <var>x</var>, where |<var>r</var>| &lt; |<var>s</var>|. Then <var>r</var> is a border of <var>s</var>. <p>Proof:&nbsp; Figure 1 shows a string <var>x</var> with borders <var>r</var> and <var>s</var>. Since <var>r</var> is a prefix of <var>x</var>, it is also a proper prefix of <var>s</var>, because it is shorter than<var>s</var>. But <var>r</var> is also a suffix of <var>x</var> and, therefore, proper suffix of <var>s</var>. Thus <var>r</var> is a border of <var>s</var>. <p><img alt="Borders r, s of a string x" src="http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/rand.gif"> <p><small>Figure 1:&nbsp; <i>Borders r</i>, <i>s of a string x</i></small> <p>If <var>s</var> is the widest border of <var>x</var>, the next-widest border <var>r</var> of <var>x</var> is obtained as the widest border of <var>s</var> etc. <p>Definition:&nbsp; Let <var>x</var> be a string and <var>a</var> <img alt="element" src="http://www.inf.fh-flensburg.de/lang/symbols/elem.gif"> <var>A</var> a symbol. A border <var>r</var> of <var>x</var> can be extended by <var>a</var>, if <var>ra</var> is a border of <var>xa</var>. <p><img alt="Extension of a border" src="http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/rand2.gif"> <p><small>Figure 2:&nbsp; <i>Extension of a border</i></small> <p>Figure 2 shows that a border <var>r</var> of width <var>j</var> of <var>x</var> can be extended by <var>a</var>, if <var>x</var><sub><var>j</var></sub> = <var>a</var>. <p>In the preprocessing phase an array <var>b</var> of length <var>m</var><small>+</small>1 is computed. Each entry <var>b</var>[<var>i</var>] contains the width of the widest border of the prefix of length <var>i</var> of the pattern (<var>i</var> = 0, ..., <var>m</var>). Since the prefix ε of length <var>i</var> = 0 has no border, we set <var>b</var>[0] = -1. <p><img alt="Prefix of length i of the pattern with border of width b[i]" src="http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/rand4.gif"> <p><small>Figure 3:&nbsp; <i>Prefix of length i of the pattern with border of width b</i>[<i>i</i>]</small> <p>Provided that the values <var>b</var>[0], ..., <var>b</var>[<var>i</var>] are already known, the value of <var>b</var>[<var>i</var><small>+</small>1] is computed by checking if a border of the prefix <var>p</var><sub>0</sub> ... <var>p</var><sub><var>i</var>-1</sub>can be extended by symbol <var>p</var><sub><var>i</var></sub>. This is the case if <var>p</var><sub><var>b</var>[<var>i</var>]</sub> = <var>p</var><sub><var>i</var></sub> (Figure 3). The borders to be examined are obtained in decreasing order from the values <var>b</var>[<var>i</var>], <var>b</var>[<var>b</var>[<var>i</var>]] etc. <p>The preprocessing algorithm comprises a loop with a variable <var>j</var> assuming these values. A border of width <var>j</var> can be extended by <var>p</var><sub><var>i</var></sub>, if<var>p</var><sub><var>j</var></sub> = <var>p</var><sub><var>i</var></sub>. If not, the next-widest border is examined by setting <var>j</var> = <var>b</var>[<var>j</var>]. The loop terminates at the latest if no border can be extended (<var>j</var> = -1). <p>After increasing <var>j</var> by the statement <tt>j++</tt> in each case <var>j</var> is the width of the widest border of <var>p</var><sub>0</sub> ... <var>p</var><sub><var>i</var></sub>. This value is written to <var>b</var>[<var>i</var><small>+</small>1] (to <var>b</var>[<var>i</var>] after increasing <var>i</var> by the statement <tt>i++</tt>). <h6>Preprocessing algorithm</h6><pre>void kmpPreprocess()
{
    int i=0, j=-1;
    b[i]=j;
    while (i&lt;m)
    {
        while (j&gt;=0 &amp;&amp; p[i]!=p[j]) j=b[j];
        i++; j++;
        b[i]=j;
    }
}
</pre>
<p>Example:&nbsp; For pattern <var>p</var> = ababaa the widths of the borders in array <var>b</var> have the following values. For instance we have <var>b</var>[5] = 3, since the prefix ababa of length 5 has a border of width 3.
<p><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/a2e7d706822b_C45A/image_6.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/a2e7d706822b_C45A/image_thumb_2.png" width="244" height="87"></a>
<h5><a name="section4">Searching algorithm</a></h5>
<p>Conceptually, the above preprocessing algorithm could be applied to the string <var>pt</var> instead of <var>p</var>. If borders up to a width of <var>m</var> are computed only, then a border of width <var>m</var> of some prefix <var>x</var> of <var>pt</var> corresponds to a match of the pattern in <var>t</var> (provided that the border is not self-overlapping) (Figure 4).
<p><img alt="Border of length m of a prefix x of pt" src="http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/rand3.gif">
<p><small>Figure 4:&nbsp; <i>Border of length m of a prefix x of pt</i></small>
<p>This explains the similarity between the preprocessing algorithm and the following searching algorithm.
<h6>Searching algorithm</h6><pre>void kmpSearch()
{
    int i=0, j=0;
    while (i&lt;n)
    {
        while (j&gt;=0 &amp;&amp; t[i]!=p[j]) j=b[j];
        i++; j++;
        if (j==m)
        {
            report(i-j);
            j=b[j];
        }
    }
}
</pre>
<p>When in the inner while loop a mismatch at position <var>j</var> occurs, the widest border of the matching prefix of length <var>j</var> of the pattern is considered (Figure 5). Resuming comparisons at position <var>b</var>[<var>j</var>], the width of the border, yields a shift of the pattern such that the border matches. If again a mismatch occurs, the next-widest border is considered, and so on, until there is no border left (<var>j</var> = -1) or the next symbol matches. Then we have a new matching prefix of the pattern and continue with the outer while loop.
<p><img alt="Shift of the pattern when a mismatch at position j occurs" src="http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/rand5.gif">
<p><small>Figure 5:&nbsp; <i>Shift of the pattern when a mismatch at position j occurs</i></small>
<p>If all <var>m</var> symbols of the pattern have matched the corresponding text window (<var>j</var> = <var>m</var>), a function <i>report</i> is called for reporting the match at position <var>i</var>-<var>j</var>. Afterwards, the pattern is shifted as far as its widest border allows.
<p>In the following example the comparisons performed by the searching algorithm are shown, where matches are drawn in green and mismatches in red.
<p>Example:
<p><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/a2e7d706822b_C45A/image_8.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/a2e7d706822b_C45A/image_thumb_3.png" width="244" height="94"></a>
<h5><a name="section5">Analysis</a></h5>
<p>The inner while loop of the preprocessing algorithm decreases the value of <var>j</var> by at least 1, since <var>b</var>[<var>j</var>] &lt; <var>j</var>. The loop terminates at the latest when <var>j</var> = -1, therefore it can decrease the value of <var>j</var> at most as often as it has been increased previously by <tt>j++</tt>. Since <tt>j++</tt> is executed in the outer loop exactly <var>m</var> times, the overall number of executions of the inner while loop is limited to <var>m</var>. The preprocessing algorithm therefore requires <var>O</var>(<var>m</var>) steps.
<p>From similar arguments it follows that the searching algorithm requires <var>O</var>(<var>n</var>) steps. The above example illustrates this: the comparisons (green and red symbols) form "stairs". The whole staircase is at most as wide as it is high, therefore at most 2<var>n</var>comparisons are performed.
<p>Since <var>m</var><img alt="&lt;=" src="http://www.inf.fh-flensburg.de/lang/symbols/le.gif"><var>n</var> the overall complexity of the Knuth-Morris-Pratt algorithm is in <var>O</var>(<var>n</var>).
<h5><a name="section6">References</a></h5>
<p>[KMP 77]<br>D.E. Knuth, J.H. Morris, V.R. Pratt: Fast Pattern Matching in Strings. SIAM Journal of Computing 6, 2, 323-350 (1977)
<p>[1]<br><a href="http://www-igm.univ-mlv.fr/~lecroq/string/">http://www-igm.univ-mlv.fr/~lecroq/string/</a>
<p>[2]<br><a href="http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/stringmatchingclasses/KmpStringMatcher.java">http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/stringmatchingclasses/KmpStringMatcher.java</a>&nbsp;&nbsp; Knuth-Morris-Pratt algorithm as a Java class source file


<img src ="http://www.cppblog.com/izualzhy/aggbug/190755.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-09-15 14:07 <a href="http://www.cppblog.com/izualzhy/archive/2012/09/15/190755.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>KMP字符串搜索算法学习笔记</title><link>http://www.cppblog.com/izualzhy/archive/2012/09/11/190311.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Tue, 11 Sep 2012 12:26:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/09/11/190311.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/190311.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/09/11/190311.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/190311.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/190311.html</trackback:ping><description><![CDATA[写在前面：很多网站的文章里介绍到KMP算法时都会用简单，容易理解来形容，实际上，并不是这样的。<br />因此，对于要了解该算法，无论你接下来看不看这篇文章，都要给自己准备加起来至少有半天的时间来学习KMP算法，不要轻敌嘿嘿。<br />这里也只是根据我的理解做的笔记，希望了解的更多请直接移步《算法导论》或者参考资料里的链接。<br />我个人对于该方法的总结是：<br />通过关键字自身的比较进行预处理，得到当不匹配时的偏移量（朴素算法是1）。<br />一直要记住的就是，当两个字符串前n位相同时，如果第n+1位相同，则两个字符串前n+1位还是相同的...是不是太简单~~<br />KMP算法是对朴素的搜索算法的改进，因此，在学习KMP搜索算法前，先来介绍下朴素的搜索算法。<br />不妨设文本<br />S = "aaaaaaaaab"<br />关键字<br />T = "aaaab"<br />朴素的搜索算法，想法很简单，从s的下标0开始，逐个与T比较，成功则返回，否则s的下标+1，继续逐个与T比较，直到结束或返回。<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">for</span>(<span style="color: #0000FF; ">int</span>&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;Len[S];&nbsp;i++)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;j;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;Len[T]);&nbsp;j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>(S[i+j]&nbsp;!=&nbsp;T[j])&nbsp;<span style="color: #0000FF; ">break</span>;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>(j&nbsp;==&nbsp;Len[T])<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find&nbsp;T&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;S<br />}</div><br />如果不是很理解，看个直观的图，其中第一个行是S，第二行是T，可以看成一个T不断向后滑动的过程。<br />  <table border="1" cellspacing="0" cellpadding="0" style="background-color: #bfbfbf; border-collapse: collapse; border: none; ">  <tbody><tr>   <td width="57" valign="top" style="width: 42.6pt; border: 1pt solid windowtext; padding: 0cm 5.4pt; ">   <p>a</p>   </td>   <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>   <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>   <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>   <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>   <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>   <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>   <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>   <td width="57" valign="top" style="width: 42.65pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>   <td width="57" valign="top" style="width: 42.65pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>b</p>   </td>  </tr>  <tr>   <td width="57" valign="top" style="width: 42.6pt; border-style: none solid solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; padding: 0cm 5.4pt; ">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:red;padding:0cm 5.4pt 0cm 5.4pt">   <p><span style="color:white;">b</span></p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>  </tr>  <tr>   <td width="57" valign="top" style="width: 42.6pt; border-style: none solid solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; padding: 0cm 5.4pt; ">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:red;padding:0cm 5.4pt 0cm 5.4pt">   <p><span style="color:white;">b</span></p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>  </tr>  <tr>   <td width="57" valign="top" style="width: 42.6pt; border-style: none solid solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; padding: 0cm 5.4pt; ">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:red;padding:0cm 5.4pt 0cm 5.4pt">   <p><span style="color:white;">b</span></p>   </td>   <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>  </tr>  <tr>   <td width="57" valign="top" style="width: 42.6pt; border-style: none solid solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; padding: 0cm 5.4pt; ">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:red;padding:0cm 5.4pt 0cm 5.4pt">   <p><span style="color:white;">b</span></p>   </td>   <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>  </tr>  <tr>   <td width="57" valign="top" style="width: 42.6pt; border-style: none solid solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; padding: 0cm 5.4pt; ">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>b</p>   </td>  </tr></tbody></table>比较到第5列时，发现不匹配，于是倒回到上一个下标开始的地方+1处(第三行)，继续，如此循环往复。<br />从这里不难看出，这种情况下算法复杂度为O(S.len*T.len).<br />KMP算法的改进点就在这里：<br />第二行不匹配后是否我们应当倒回到第三行的状态继续比较？如果不用，那么应该怎么比较。<br />我们先用这个例子来表示一下，S[0,4]表示的是S字符串下标从0到4的闭区间。<br />提前提示一下： T[0,2] = T[1,3]<br />第二行里，我们比较到了第5列（即下标为4的地方，接下来我们开始只用下标描述，以免产生混乱），即前面的字符串是匹配的，因此有这个结果：<br />S[0,3] = T[0,3],S[1,3] = T[1,3],S[2,3] = T[2,3]...<br />同时根据前面的提示:<br />T[0,2] = T[1,3] = S[1,3]<br />可以看到T[0,2] = S[1,3]就是第三行里前面要比较的内容，也就是说实际上是不需要的。<br />第二行失败后，我们可以直接从S[4]与T[3]比较!<br />也就是说KMP要解决的就是：如果在S的某个下标处不匹配了，那么S的改下标不需要退回，而是选择T的一个下标，来继续比较（因为像前面的例子，T[0,2]=T[1,3]了已经)。<br />简单直观点的说法，就是，当不匹配时，T不是直接滑动回去再前进一步，而是选择一个可以前进的最大值，同时，直接进行与S当前不匹配下标的比较。<br />注意这个最大值不一定为1，S当前选择与T的哪个下标比较，会决定这样一个前进值。<br />再看个例子：<br /><table border="1" cellspacing="0" cellpadding="0" style="background-color: #bfbfbf; border-collapse: collapse; border: none; ">  <tbody><tr>   <td width="57" valign="top" style="width: 42.6pt; border: 1pt solid windowtext; padding: 0cm 5.4pt; ">   <p>a</p>   </td>      <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>b</p>   </td>      <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>      <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>d</p>   </td>      <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>e</p>   </td>      <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>      <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>b</p>   </td>      <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>      <td width="57" valign="top" style="width: 42.65pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>d</p>   </td>      <td width="57" valign="top" style="width: 42.65pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>f</p>   </td>     </tr>  <tr>   <td width="57" valign="top" style="width: 42.6pt; border-style: none solid solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>b</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>d</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:red;padding:0cm 5.4pt 0cm 5.4pt">   <p><span style="color:white;">f</span></p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>     </tr>  <tr>   <td width="57" valign="top" style="width: 42.6pt; border-style: none solid solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; padding: 0cm 5.4pt; ">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:red;padding:0cm 5.4pt 0cm 5.4pt">   <p><span style="color:white;">a</span></p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>     </tr>  <tr>   <td width="57" valign="top" style="width: 42.6pt; border-style: none solid solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; padding: 0cm 5.4pt; ">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:red;padding:0cm 5.4pt 0cm 5.4pt">   <p><span style="color:white;">b</span></p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>     </tr>  <tr>   <td width="57" valign="top" style="width: 42.6pt; border-style: none solid solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; padding: 0cm 5.4pt; ">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:red;padding:0cm 5.4pt 0cm 5.4pt">   <p><span style="color:white;">b</span></p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>      <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>     </tr></tbody></table>这是朴素的匹配算法的策略，其中S="abadeabadf",T="abadf"<br />而KMP的策略是：<br /><table border="1" cellspacing="0" cellpadding="0" style="background-color: #bfbfbf; border-collapse: collapse; border: none; ">  <tbody><tr>   <td width="57" valign="top" style="width: 42.6pt; border: 1pt solid windowtext; padding: 0cm 5.4pt; ">   <p>a</p>   </td>   <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>b</p>   </td>   <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>   <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>d</p>   </td>   <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>e</p>   </td>   <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>   <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>b</p>   </td>   <td width="57" valign="top" style="width: 42.6pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>   <td width="57" valign="top" style="width: 42.65pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>d</p>   </td>   <td width="57" valign="top" style="width: 42.65pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0cm 5.4pt; ">   <p>f</p>   </td>  </tr>  <tr>   <td width="57" valign="top" style="width: 42.6pt; border-style: none solid solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; padding: 0cm 5.4pt; ">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>b</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>d</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:red;padding:0cm 5.4pt 0cm 5.4pt">   <p><span style="color:white;">f</span></p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>  </tr>  <tr>   <td width="57" valign="top" style="width: 42.6pt; border-style: none solid solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; padding: 0cm 5.4pt; ">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>&nbsp;a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>b</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>a</p>   </td>   <td width="57" valign="top" style="width:42.6pt;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>d</p>   </td>   <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p>f</p>   </td>   <td width="57" valign="top" style="width:42.65pt;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt">   <p><br /></p>   </td>  </tr></tbody></table>为什么可以这样选择？<br />因此，kmp就是要计算这样一个数组next[], 当文本S在其下标i处与T不匹配时，来决定与T哪个下标处开始继续比较。<br />我们先看下程序：<br /><div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all; "><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000; ">/*</span><span style="color: #008000; "><br />&nbsp;*&nbsp;=====================================================================================<br />&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Filename:&nbsp;&nbsp;kmpsearch.c<br />&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;Description:&nbsp;&nbsp;kmp&nbsp;search<br />&nbsp;*<br />&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Version:&nbsp;&nbsp;1.0<br />&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Created:&nbsp;&nbsp;09/10/2012&nbsp;07:15:07&nbsp;PM<br />&nbsp;*<br />&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Author:&nbsp;&nbsp;zhy&nbsp;(),&nbsp;izualzhy@163.com<br />&nbsp;*&nbsp;=====================================================================================<br />&nbsp;</span><span style="color: #008000; ">*/</span><br /><br /><span style="color: #008000; ">/*</span><span style="color: #008000; ">*<br />&nbsp;*&nbsp;@brief&nbsp;KMPSearch&nbsp;kmp搜索字符串算法<br />&nbsp;*<br />&nbsp;*&nbsp;@param&nbsp;src&nbsp;待搜索的文本<br />&nbsp;*&nbsp;@param&nbsp;srcLen&nbsp;待搜索文本长度<br />&nbsp;*&nbsp;@param&nbsp;keyword&nbsp;搜索关键字<br />&nbsp;*&nbsp;@param&nbsp;keywordLen&nbsp;搜索关键字长度<br />&nbsp;*<br />&nbsp;*&nbsp;@return&nbsp;搜索到第一个结果停止，返回关键字在文本出现的索引，否则返回-1<br />&nbsp;</span><span style="color: #008000; ">*/</span><br /><span style="color: #0000FF; ">int</span>&nbsp;KMPSearch(<span style="color: #0000FF; ">const</span>&nbsp;<span style="color: #0000FF; ">char</span>*&nbsp;src,&nbsp;<span style="color: #0000FF; ">const</span>&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;srcLen,&nbsp;<span style="color: #0000FF; ">const</span>&nbsp;<span style="color: #0000FF; ">char</span>*&nbsp;keyword,&nbsp;<span style="color: #0000FF; ">const</span>&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;keywordLen)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">存储kmp需要的next数组</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">char</span>*&nbsp;next&nbsp;=&nbsp;(<span style="color: #0000FF; ">char</span>*)malloc(keywordLen*<span style="color: #0000FF; ">sizeof</span>(<span style="color: #0000FF; ">char</span>));<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(!next)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;-1;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">初始化next数组的值</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;CalcKMPNext(keyword,&nbsp;keywordLen,&nbsp;next);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;i&nbsp;=&nbsp;0,&nbsp;j&nbsp;=&nbsp;0;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;(&nbsp;;&nbsp;i&lt;keywordLen;&nbsp;++i)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("next:&nbsp;%d\n",&nbsp;next[i]);<br />&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;=&nbsp;0;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(i&nbsp;&lt;&nbsp;srcLen)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">如果比较结果不同，则需要回退关键字的索引，继续比较，如果回退至关键字开始处(j==0)时break，下次直接从当前i与关键字的第一个字符开始比较</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(j&nbsp;&gt;=&nbsp;0&nbsp;&amp;&amp;&nbsp;src[i]&nbsp;!=&nbsp;keyword[j])<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j&nbsp;=&nbsp;next[j];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("i=%d,j=%d\n",i,j);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i++;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j++;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(j&nbsp;==&nbsp;keywordLen)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;i&nbsp;-&nbsp;keywordLen;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;-1;<br />}<br /><br /><span style="color: #008000; ">/*</span><span style="color: #008000; ">*<br />&nbsp;*&nbsp;@brief&nbsp;CalcKMPNext&nbsp;计算kmp算法需要的next数组<br />&nbsp;*<br />&nbsp;*&nbsp;@param&nbsp;keyword&nbsp;关键字<br />&nbsp;*&nbsp;@param&nbsp;keywordLen&nbsp;关键字长度<br />&nbsp;*&nbsp;@param&nbsp;next&nbsp;存储next<br />&nbsp;</span><span style="color: #008000; ">*/</span><br /><span style="color: #0000FF; ">void</span>&nbsp;CalcKMPNext(<span style="color: #0000FF; ">const</span>&nbsp;<span style="color: #0000FF; ">char</span>*&nbsp;keyword,&nbsp;<span style="color: #0000FF; ">const</span>&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;keywordLen,&nbsp;<span style="color: #0000FF; ">char</span>*&nbsp;next)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;i&nbsp;=&nbsp;0,&nbsp;j&nbsp;=&nbsp;-1;<br />&nbsp;&nbsp;&nbsp;&nbsp;next[i]&nbsp;=&nbsp;j;<span style="color: #008000; ">//</span><span style="color: #008000; ">初始化next[0],没有什么意义</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(i&nbsp;&lt;&nbsp;keywordLen)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">如果不相等，回退直到相等，或者没有相等</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(j&nbsp;&gt;=&nbsp;0&nbsp;&amp;&amp;&nbsp;keyword[i]!=keyword[j])<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j&nbsp;=&nbsp;next[j];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++i;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++j;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(i&lt;keywordLen)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next[i]&nbsp;=&nbsp;j;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /><span style="color: #0000FF; ">int</span>&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">char</span>&nbsp;text[]&nbsp;=&nbsp;"abadeabadf";<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">char</span>&nbsp;keyword[]&nbsp;=&nbsp;"abadf";<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">/*</span><span style="color: #008000; "><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*char&nbsp;text[]&nbsp;=&nbsp;"this&nbsp;is&nbsp;a&nbsp;demo,&nbsp;is&nbsp;is&nbsp;not&nbsp;u&nbsp;,&nbsp;isnot&nbsp;it?";<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*char&nbsp;keyword[]&nbsp;=&nbsp;"is";<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">*/</span><br />&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;textLen&nbsp;=&nbsp;strlen(text);<br />&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;keywordLen&nbsp;=&nbsp;strlen(keyword);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;startIndex&nbsp;=&nbsp;0;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(1)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;foundIndex&nbsp;=&nbsp;KMPSearch(text&nbsp;+&nbsp;startIndex,&nbsp;textLen,&nbsp;keyword,&nbsp;keywordLen);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(foundIndex&nbsp;==&nbsp;-1)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">break</span>;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;startIndex&nbsp;+=&nbsp;foundIndex;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("find&nbsp;in&nbsp;%d\n",&nbsp;startIndex);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;startIndex&nbsp;+=&nbsp;keywordLen;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;0;<br />}</div><br /><br />参考资料：<br /><a href="http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/kmpen.htm">http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/kmpen.htm</a>&nbsp;<br /><a href="http://chaoswork.com/blog/2011/06/14/kmp%E7%AE%97%E6%B3%95%E5%B0%8F%E7%BB%93/">http://chaoswork.com/blog/2011/06/14/kmp%E7%AE%97%E6%B3%95%E5%B0%8F%E7%BB%93/</a>&nbsp;<br /><a href="http://www.matrix67.com/blog/archives/115">http://www.matrix67.com/blog/archives/115</a>&nbsp;<br /><img src ="http://www.cppblog.com/izualzhy/aggbug/190311.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-09-11 20:26 <a href="http://www.cppblog.com/izualzhy/archive/2012/09/11/190311.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>单调栈的介绍与应用</title><link>http://www.cppblog.com/izualzhy/archive/2012/08/25/188226.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Sat, 25 Aug 2012 03:31:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/08/25/188226.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/188226.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/08/25/188226.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/188226.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/188226.html</trackback:ping><description><![CDATA[<p><strong><font size="4">定义：</font></strong></p> <p><font color="#0000ff">单调栈，顾名思义就是说栈内的元素，按照某种方式排序下，必须是单调的。如果新入栈的元素破坏了单调性，就弹出栈内元素，知道满足单调性。</font></p> <p><font color="#0000ff">它可以很方便地求出某个数的左边或者右边第一个比它大或者小的元素，而且总时间复杂度O(N)。</font></p> <p>起源的题目：</p> <p>这道题目在网上稍微搜了下，感觉已经是泛滥了，我就拿过来再贴一遍O(&#8745;_&#8745;)O哈哈~</p> <p><strong>题目大意：</strong> </p><p>给出一个柱形统计图(histogram), 它的每个项目的宽度是1， 高度和具体问题有关。 现在编程求出在这个柱形图中的最大面积的长方形。 </p><p>例如： </p><pre>7 2 1 4 5 1 3 3</pre>
<p>7表示柱形图有7个数据，分别是 2 1 4 5 1 3 3， 对应的柱形图如下，最后求出来的面积最大的图如右图所示。
</p><p><img alt="" src="http://poj.org/images/2559_1.jpg" />
</p><p>看到这个题目我第一反应是，遍历每个矩形，左右寻找能扩展的最大距离，然后求面积，比较，得到结果。</p>
<p>太复杂，不是么？</p>
<p>主要是有很多重复的工作。</p>
<p>我是没有想到单调栈的，不过用了之后，发现代码比我想到的要好写，而且确实很容易。</p><pre class="code"> <div style="font-family: Consolas, 'Courier New', 'BitStream Vera Sans Mono', 'Lucida Console'; line-height: normal; white-space: normal; background-color: #fdfdfd; "><u>C++语言</u>:&nbsp;<a href="http://fayaa.com/code/" style="color: #0000cc; text-decoration: none; ">高亮代码由发芽网提供</a></div><div style="line-height: normal; white-space: normal; background-color: #f1feeb; font-family: '[object HTMLOptionElement]', Consolas, 'Lucida Console', 'Courier New'; "><span style="color: #008800; font-style: italic; ">01&nbsp;</span><span style="color: #008800; font-style: italic; ">/*</span><br /><span style="color: #008800; font-style: italic; ">02&nbsp;</span><span style="color: #008800; font-style: italic; ">* =====================================================================================</span><br /><span style="color: #008800; font-style: italic; ">03&nbsp;</span><span style="color: #008800; font-style: italic; ">* &nbsp; &nbsp; &nbsp; Filename: &nbsp;MonotoneStack.cpp</span><br /><span style="color: #008800; font-style: italic; ">04&nbsp;</span><span style="color: #008800; font-style: italic; ">* &nbsp; &nbsp;Description: &nbsp;</span><br /><span style="color: #f810b0; ">05&nbsp;</span><span style="color: #008800; font-style: italic; ">*</span><br /><span style="color: #008800; font-style: italic; ">06&nbsp;</span><span style="color: #008800; font-style: italic; ">* &nbsp; &nbsp; &nbsp; &nbsp;Version: &nbsp;1.0</span><br /><span style="color: #008800; font-style: italic; ">07&nbsp;</span><span style="color: #008800; font-style: italic; ">* &nbsp; &nbsp; &nbsp; &nbsp;Created: &nbsp;08/24/2012 04:37:27 PM</span><br /><span style="color: #008800; font-style: italic; ">08&nbsp;</span><span style="color: #008800; font-style: italic; ">*</span><br /><span style="color: #008800; font-style: italic; ">09&nbsp;</span><span style="color: #008800; font-style: italic; ">* &nbsp; &nbsp; &nbsp; &nbsp; Author: &nbsp;zhy (), izualzhy@163.com</span><br /><span style="color: #f810b0; ">10&nbsp;</span><span style="color: #008800; font-style: italic; ">* =====================================================================================</span><br /><span style="color: #008800; font-style: italic; ">11&nbsp;</span><span style="color: #008800; font-style: italic; ">*/</span><br /><span style="color: #008800; font-style: italic; ">12&nbsp;</span><span style="color: #008080; ">#include &lt;iostream&gt;</span><br /><span style="color: #008800; font-style: italic; ">13&nbsp;</span><span style="color: #008080; ">#include &lt;stack&gt;</span><br /><span style="color: #008800; font-style: italic; ">14&nbsp;</span><span style="color: #000080; font-weight: bold; ">using</span>&nbsp;<span style="color: #000080; font-weight: bold; ">namespace</span>&nbsp;std;<br /><span style="color: #f810b0; ">15&nbsp;</span><br /><span style="color: #008800; font-style: italic; ">16&nbsp;</span><span style="color: #000080; font-weight: bold; ">struct</span>&nbsp;Element<br /><span style="color: #008800; font-style: italic; ">17&nbsp;</span>{<br /><span style="color: #008800; font-style: italic; ">18&nbsp;</span>&nbsp; &nbsp;Element(<span style="color: #000080; font-weight: bold; ">int</span>&nbsp;h,&nbsp;<span style="color: #000080; font-weight: bold; ">int</span>&nbsp;index)<br /><span style="color: #008800; font-style: italic; ">19&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp;:&nbsp;height(h)<br /><span style="color: #f810b0; ">20&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp;,&nbsp;startIndex(index)<br /><span style="color: #008800; font-style: italic; ">21&nbsp;</span>&nbsp; &nbsp;{}<br /><span style="color: #008800; font-style: italic; ">22&nbsp;</span><br /><span style="color: #008800; font-style: italic; ">23&nbsp;</span>&nbsp; &nbsp;<span style="color: #000080; font-weight: bold; ">int</span>&nbsp;height;<span style="color: #008800; font-style: italic; ">//矩形高度</span><br /><span style="color: #008800; font-style: italic; ">24&nbsp;</span>&nbsp; &nbsp;<span style="color: #000080; font-weight: bold; ">int</span>&nbsp;startIndex;<span style="color: #008800; font-style: italic; ">//对应的索引位置</span><br /><span style="color: #f810b0; ">25&nbsp;</span>};<br /><span style="color: #008800; font-style: italic; ">26&nbsp;</span><br /><span style="color: #008800; font-style: italic; ">27&nbsp;</span><span style="color: #000080; font-weight: bold; ">int</span>&nbsp;GetMaxRect(<span style="color: #000080; font-weight: bold; ">int</span>&nbsp;rectHeights[],&nbsp;<span style="color: #000080; font-weight: bold; ">int</span>&nbsp;len)<br /><span style="color: #008800; font-style: italic; ">28&nbsp;</span>{<br /><span style="color: #008800; font-style: italic; ">29&nbsp;</span>&nbsp; &nbsp;stack&lt;Element&gt;&nbsp;s;<span style="color: #008800; font-style: italic; ">//使用std的stack</span><br /><span style="color: #f810b0; ">30&nbsp;</span>&nbsp; &nbsp;s.push(Element(-<span style="color: #0000ff; ">1</span>,&nbsp;<span style="color: #0000ff; ">0</span>));<span style="color: #008800; font-style: italic; ">//首先-1高度入栈，这样在之后的出栈策略上不会导致空栈，另外一种方法是可以在下文的while判断时加入对空栈的判断。</span><br /><span style="color: #008800; font-style: italic; ">31&nbsp;</span>&nbsp; &nbsp;<span style="color: #000080; font-weight: bold; ">int</span>&nbsp;maxArea&nbsp;=&nbsp;-<span style="color: #0000ff; ">1</span>;<br /><span style="color: #008800; font-style: italic; ">32&nbsp;</span>&nbsp; &nbsp;<span style="color: #000080; font-weight: bold; ">int</span>&nbsp;height&nbsp;=&nbsp;-<span style="color: #0000ff; ">1</span>;<br /><span style="color: #008800; font-style: italic; ">33&nbsp;</span>&nbsp; &nbsp;<br /><span style="color: #008800; font-style: italic; ">34&nbsp;</span>&nbsp; &nbsp;<span style="color: #000080; font-weight: bold; ">for</span>&nbsp;(&nbsp;<span style="color: #000080; font-weight: bold; ">int</span>&nbsp;i=<span style="color: #0000ff; ">0</span>;&nbsp;i&lt;len;&nbsp;++i)<br /><span style="color: #f810b0; ">35&nbsp;</span>&nbsp; &nbsp;{<br /><span style="color: #008800; font-style: italic; ">36&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000080; font-weight: bold; ">if</span>&nbsp;(i&nbsp;==&nbsp;len)<span style="color: #008800; font-style: italic; ">//扫描完毕时，要全部出栈，计算可能的矩形面积</span><br /><span style="color: #008800; font-style: italic; ">37&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp;{<br /><span style="color: #008800; font-style: italic; ">38&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;height&nbsp;=&nbsp;<span style="color: #0000ff; ">0</span>;<br /><span style="color: #008800; font-style: italic; ">39&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp;}<br /><span style="color: #f810b0; ">40&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000080; font-weight: bold; ">else</span><span style="color: #008800; font-style: italic; ">//扫描过程，则记录此时的矩形高度</span><br /><span style="color: #008800; font-style: italic; ">41&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp;{<br /><span style="color: #008800; font-style: italic; ">42&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;height&nbsp;=&nbsp;rectHeights[i];<br /><span style="color: #008800; font-style: italic; ">43&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp;}<br /><span style="color: #008800; font-style: italic; ">44&nbsp;</span><br /><span style="color: #f810b0; ">45&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000080; font-weight: bold; ">while</span>&nbsp;(height&nbsp;&lt;&nbsp;s.top().height)<span style="color: #008800; font-style: italic; ">//如果矩形高度比栈顶要小，则说明之前的矩形无法继续往右扩展，需要弹出，并计算面积</span><br /><span style="color: #008800; font-style: italic; ">46&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp;{<br /><span style="color: #008800; font-style: italic; ">47&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008800; font-style: italic; ">//计算面积的公式，核心部分</span><br /><span style="color: #008800; font-style: italic; ">48&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008800; font-style: italic; ">//根据我们的策略，新元素高度要高，则表明我们的矩形还可以继续往右扩展，因此栈内元素时满足单调性的，对此时栈内每个元素，都是可以根据自己的高度，扩展矩形到i-1的</span><br /><span style="color: #008800; font-style: italic; ">49&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000080; font-weight: bold; ">int</span>&nbsp;area&nbsp;=&nbsp;(i&nbsp;-&nbsp;s.top().startIndex)*s.top().height;<br /><span style="color: #f810b0; ">50&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;maxArea&nbsp;=&nbsp;maxArea&nbsp;&gt;&nbsp;area&nbsp;?&nbsp;maxArea&nbsp;:&nbsp;area;<br /><span style="color: #008800; font-style: italic; ">51&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s.pop();<br /><span style="color: #008800; font-style: italic; ">52&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp;}<br /><span style="color: #008800; font-style: italic; ">53&nbsp;</span>&nbsp; &nbsp; &nbsp; &nbsp;s.push(Element(rectHeights[i],&nbsp;i));<br /><span style="color: #008800; font-style: italic; ">54&nbsp;</span>&nbsp; &nbsp;}<br /><span style="color: #f810b0; ">55&nbsp;</span><br /><span style="color: #008800; font-style: italic; ">56&nbsp;</span>&nbsp; &nbsp;<span style="color: #000080; font-weight: bold; ">return</span>&nbsp;maxArea;<br /><span style="color: #008800; font-style: italic; ">57&nbsp;</span>}<br /><span style="color: #008800; font-style: italic; ">58&nbsp;</span><br /><span style="color: #008800; font-style: italic; ">59&nbsp;</span><span style="color: #000080; font-weight: bold; ">int</span>&nbsp;main()<br /><span style="color: #f810b0; ">60&nbsp;</span>{<br /><span style="color: #008800; font-style: italic; ">61&nbsp;</span>&nbsp; &nbsp;<span style="color: #000080; font-weight: bold; ">int</span>&nbsp;rects[]&nbsp;=&nbsp;{<span style="color: #0000ff; ">2</span>,&nbsp;<span style="color: #0000ff; ">1</span>,&nbsp;<span style="color: #0000ff; ">4</span>,&nbsp;<span style="color: #0000ff; ">5</span>,&nbsp;<span style="color: #0000ff; ">1</span>,&nbsp;<span style="color: #0000ff; ">3</span>,&nbsp;<span style="color: #0000ff; ">3</span>};<br /><span style="color: #008800; font-style: italic; ">62&nbsp;</span>&nbsp; &nbsp;cout&nbsp;&lt;&lt;&nbsp;GetMaxRect(rects,&nbsp;<span style="color: #0000ff; ">7</span>)&nbsp;&lt;&lt;&nbsp;endl;<br /><span style="color: #008800; font-style: italic; ">63&nbsp;</span>&nbsp; &nbsp;<span style="color: #000080; font-weight: bold; ">return</span>&nbsp;<span style="color: #0000ff; ">0</span>;<br /><span style="color: #008800; font-style: italic; ">64&nbsp;</span>}</div> <span style="color: #2e53d1">

</span></pre>
<p>至于现在的复杂度是多少，我没想出来，欢迎告诉我<img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="微笑" src="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/3d48dc9fbd5d_96C5/wlEmoticon-smile_2.png" /><br /><br />
</p><p>参考资料：
</p><p><a href="http://blog.csdn.net/hopeztm/article/details/7868581">http://blog.csdn.net/hopeztm/article/details/7868581</a></p><img src ="http://www.cppblog.com/izualzhy/aggbug/188226.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-08-25 11:31 <a href="http://www.cppblog.com/izualzhy/archive/2012/08/25/188226.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>字典树的介绍与实现</title><link>http://www.cppblog.com/izualzhy/archive/2012/08/22/187986.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Wed, 22 Aug 2012 11:56:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/08/22/187986.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/187986.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/08/22/187986.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/187986.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/187986.html</trackback:ping><description><![CDATA[<p>假若需要这样的实现，我有一个词典，词典内容如下：</p> <p>pad</p> <p>park</p> <p>panda</p> <p>pan</p> <p>apply</p> <p>apple</p> <p>append</p> <p>app</p> <p>application</p> <p>需要统计以p为前缀的单词个数，好的，最直观的办法，我遍历所有的字符串，记数，ok，作业完成了！</p> <p>A: 完成的很好！那现在我想要pa开始的~</p> <p>Me: 嗯~好的，遍历，记数，ok~</p> <p>A:我要app前缀的个数~</p> <p>Me: 嗯?~好吧。。</p> <p>A:appl的，pan的，前缀个数~</p> <p>Me: &#8230;.</p> <p>&nbsp;</p> <p>是不是很无奈，这个时候字典树就派上用场了。当然首先要明确的是，我们的目标是以空间换时间。</p> <p>字典树，从根节点开始，每个节点可能的child数是26（如果只有英文小写字母的话），当然是随着词汇添加而完善这棵树的。<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000; ">/*</span><span style="color: #008000; "><br />&nbsp;*&nbsp;=====================================================================================<br />&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Filename:&nbsp;&nbsp;Trie.cpp<br />&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;Description:&nbsp;&nbsp;字典数的定义和使用<br />&nbsp;*<br />&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Version:&nbsp;&nbsp;1.0<br />&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Created:&nbsp;&nbsp;08/22/2012&nbsp;07:01:05&nbsp;PM<br />&nbsp;*<br />&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Author:&nbsp;&nbsp;zhy&nbsp;(),&nbsp;izualzhy@163.com<br />&nbsp;*&nbsp;=====================================================================================<br />&nbsp;</span><span style="color: #008000; ">*/</span><br /><br />#include&nbsp;&lt;stdio.h&gt;<br />#include&nbsp;&lt;<span style="color: #0000FF; ">string</span>.h&gt;<br /><br /><span style="color: #0000FF; ">#define</span>&nbsp;MAX&nbsp;26<span style="color: #008000; ">//</span><span style="color: #008000; ">可以变化，比如只有数字则为10</span><br /><br /><span style="color: #008000; ">//</span><span style="color: #008000; ">字典数的基础结构</span><span style="color: #008000; "><br /></span><span style="color: #0000FF; ">struct</span>&nbsp;Trie<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;Trie()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;(&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;i=0;&nbsp;i&lt;MAX;&nbsp;++i)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next[i]&nbsp;=&nbsp;NULL;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count&nbsp;=&nbsp;1;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;Trie&nbsp;*next[MAX];<span style="color: #008000; ">//</span><span style="color: #008000; ">下一层的节点数目，初始时指针全是null，当添加词汇后会逐步添加</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;count;<span style="color: #008000; ">//</span><span style="color: #008000; ">前缀数目</span><span style="color: #008000; "><br /></span>};<br /><br />Trie*&nbsp;root&nbsp;=&nbsp;<span style="color: #0000FF; ">new</span>&nbsp;Trie;<span style="color: #008000; ">//</span><span style="color: #008000; ">根节点<br /><br /></span><span style="color: #008000; ">//</span><span style="color: #008000; ">根据str完善以root为根节点的词典树</span><span style="color: #008000; "><br /></span><span style="color: #0000FF; ">void</span>&nbsp;createTrie(<span style="color: #0000FF; ">const</span>&nbsp;<span style="color: #0000FF; ">char</span>&nbsp;*str)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;Trie*&nbsp;p&nbsp;=&nbsp;root;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(*str)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;index&nbsp;=&nbsp;*str&nbsp;-&nbsp;'a';<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(p-&gt;next[index]!=NULL)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p-&gt;next[index]-&gt;count++;<span style="color: #008000; ">//</span><span style="color: #008000; ">如果已经存在，前缀数目+1</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span style="color: #0000FF; ">else</span>&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p-&gt;next[index]&nbsp;=&nbsp;<span style="color: #0000FF; ">new</span>&nbsp;Trie;<span style="color: #008000; ">//</span><span style="color: #008000; ">不存在则建立</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;=&nbsp;p-&gt;next[index];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str++;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><span style="color: #008000; ">//</span><span style="color: #008000; ">查找str为前缀的词汇数目</span><span style="color: #008000; "><br /></span><span style="color: #0000FF; ">int</span>&nbsp;findTrie(<span style="color: #0000FF; ">const</span>&nbsp;<span style="color: #0000FF; ">char</span>*&nbsp;str)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;len&nbsp;=&nbsp;strlen(str);<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(len&nbsp;==&nbsp;0)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;0;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;Trie*&nbsp;p&nbsp;=&nbsp;root;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">逐个字符遍历str</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(*str)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;index&nbsp;=&nbsp;*str&nbsp;-&nbsp;'a';<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(p-&gt;next[index]&nbsp;!=&nbsp;NULL)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;=&nbsp;p-&gt;next[index];<span style="color: #008000; ">//</span><span style="color: #008000; ">该字符存在，试试下一个字符</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span style="color: #0000FF; ">else</span>&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;0;<span style="color: #008000; ">//</span><span style="color: #008000; ">如果不存在该字符，则不存在该前缀</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str++;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;p-&gt;count;<br />}<br /><br /><span style="color: #0000FF; ">void</span>&nbsp;destroyTrie(Trie*&nbsp;node)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(node&nbsp;==&nbsp;NULL)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;(&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;i=0;&nbsp;i&lt;MAX;&nbsp;++i)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(node-&gt;next[i]&nbsp;!=&nbsp;NULL)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;destroyTrie(node-&gt;next[i]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;node;<br />}<br /><br /><span style="color: #0000FF; ">int</span>&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">char</span>&nbsp;s[100];<br />&nbsp;&nbsp;&nbsp;&nbsp;printf("input&nbsp;the&nbsp;words&nbsp;for&nbsp;trie\n");<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(gets(s)&nbsp;&amp;&amp;&nbsp;strlen(s))<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;createTrie(s);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;printf("search&nbsp;what&nbsp;u&nbsp;want?:\n");<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(gets(s)&nbsp;&amp;&amp;&nbsp;strlen(s))<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%s:&nbsp;%d\n",&nbsp;s,&nbsp;findTrie(s));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;destroyTrie(root);<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;0;<br />}</div></p> <p>运行结果：</p> <p>y@y-VirtualBox:/mnt/mydocuments/Training$ ./Trie<br />input the words for trie<br />pad<br />park<br />panda<br />pan<br />apply<br />apple<br />append<br />app<br />application</p> <p>search what u want?:<br />p<br />p: 4<br />a<br />a: 5<br />pa<br />pa: 4<br />pan<br />pan: 2<br />par<br />par: 1<br />app<br />app: 5<br />appl<br />appl: 3<br />appe<br />appe: 1</p><img src ="http://www.cppblog.com/izualzhy/aggbug/187986.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-08-22 19:56 <a href="http://www.cppblog.com/izualzhy/archive/2012/08/22/187986.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>有一串首尾相连的珠子，共有m个，每一个珠子有一种颜色，并且颜色的总数不超过n(n&amp;lt;=10)，求连续的珠子的颜色总数为n时，长度最小的区间。</title><link>http://www.cppblog.com/izualzhy/archive/2012/03/20/168430.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Tue, 20 Mar 2012 12:28:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/03/20/168430.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/168430.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/03/20/168430.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/168430.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/168430.html</trackback:ping><description><![CDATA[<p>依稀记得见到此题是在校园招聘的笔试题目上，也记得当时的感觉是一头雾水，完全不懂这个题目在干什么。</p> <p>时间如流水，转眼已工作了一年，工作也阴差阳错的换了一个。偶然间看到这个题目，竟猛然间想起学校时的自己，背着书包，阳光，图书馆，那时自己也不喜欢研究这样的问题，天天在数学问题里泡着，什么算子，理想环的，拓扑的，可以那么没心没肺的只去学习自己喜欢的东西。</p> <p>扯远了，看到这个问题，仔细想了下，想了种解决的方法，google了一下，发现博客园里有篇文章类似，却又编译不过而且一点注释没有。。。</p> <p>贴下自己的代码，大家看下，哪里不对的地方请指出。</p> <p>想想真的挺感谢之前的公司的，接纳了我这个小菜鸟，同时让我成长，为一个大菜鸟^_^</p> <p>先考虑下简单情况并抽象下问题，珠子改为线性的而不是环，用数组表示，内容为其颜色（用数字表示），则表示为在该数组里找到最小的连续的区间，使得包含所有的数字。</p> <p>想法是这样的：</p> <p>先从index=0处搜索，每检查一颗珠子，响应的颜色数量+1，如果是新的颜色则总颜色数+1.</p> <p>当颜色总数为n时，找到第一个满足条件的连续序列。</p> <p>1.从该序列起始处搜索，若搜索处的颜色数量不为1，则表明该串还有别的珠子有该颜色，继续往前搜索并更新该序列，起始索引位置+1.</p> <blockquote> <p>若搜索处颜色数量为1，停止搜索。</p></blockquote> <p>2。比较最佳序列长与当前序列长，更新最佳序列。记录当前序列起始位置。</p> <p>从第一个满足条件的序列继续index++，并检查1,2条件。</p><pre class="code"><span style="color: green">//#######################################################################
//# Author: izualzhy@163.com
//# Created Time: 2012/3/15 14:15:46
//# File Name: pearls.cpp
//# Description: 
//#######################################################################
</span><span style="color: blue">#include </span><span style="color: #a31515">&lt;iostream&gt;
</span><span style="color: blue">using namespace </span>std;

<span style="color: blue">#define </span>MAXN 10
<span style="color: blue">int </span>colors[MAXN];<span style="color: green">//record the counter of one color
</span><span style="color: blue">int </span>colorsCounter;
<span style="color: blue">void </span>find(<span style="color: blue">int </span>arr[],<span style="color: blue">int </span>len, <span style="color: blue">int </span>colorsNeed)
{
    <span style="color: blue">int </span>bestStartIndex = 0;
    <span style="color: blue">int </span>bestLen = len;
    <span style="color: blue">int </span>lastStartIndex = 0;
    
    <span style="color: blue">for </span>( <span style="color: blue">int </span>i=0; i&lt;len; ++i) {
        <span style="color: blue">if </span>(!colors[arr[i]])
            colorsCounter++;
        colors[arr[i]]++;

        <span style="color: blue">if </span>(colorsCounter==colorsNeed) {
            <span style="color: blue">int </span>j = lastStartIndex;
            <span style="color: blue">while </span>(colors[arr[j]]&gt;1) {
                colors[arr[j]]--;
                ++j;
            }
            <span style="color: blue">if </span>(i-j+1&lt;bestLen) {
                bestStartIndex = j;
                bestLen = i-j+1;
                <span style="color: blue">if </span>(bestLen==colorsNeed)
                    <span style="color: blue">break</span>;
            }
            lastStartIndex = j;
        }
    }

    cout &lt;&lt; bestStartIndex &lt;&lt; endl;
    cout &lt;&lt; bestLen &lt;&lt; endl;
    <span style="color: blue">for </span>(<span style="color: blue">int </span>i=bestStartIndex; i&lt;bestLen+bestStartIndex; ++i)
        cout &lt;&lt; arr[i] &lt;&lt; <span style="color: #a31515">" "</span>;
    cout &lt;&lt; endl;
}

<span style="color: blue">int </span>main()
{
    <span style="color: blue">int </span>arr[] = {1,2,3,3,2,1,4,1,3,4,5,6,2,3,4,1,5,2,3,4};
    <span style="color: blue">int </span>m = <span style="color: blue">sizeof</span>(arr)/<span style="color: blue">sizeof</span>(arr[0]);

    <span style="color: blue">for </span>( <span style="color: blue">int </span>i=0; i&lt;m; ++i)
        cout &lt;&lt; arr[i] &lt;&lt;<span style="color: #a31515">" "</span>;
    cout &lt;&lt; endl;

    <span style="color: blue">int </span>n = 6;
    find(arr,m,n);

    <span style="color: blue">return </span>0;
}

输出：</pre>
<p>1 2 3 3 2 1 4 1 3 4 5 6 2 3 4 1 5 2 3 4<br>7<br>6<br>1 3 4 5 6 2</p>
<p>如果是环的话多了一遍循环：</p><pre class="code"><span style="color: green">//#######################################################################
//# Author: izualzhy@163.com
//# Created Time: 2012/3/15 14:15:46
//# File Name: pearls.cpp
//# Description: 
//#######################################################################
</span><span style="color: blue">#include </span><span style="color: #a31515">&lt;iostream&gt;
</span><span style="color: blue">using namespace </span>std;

<span style="color: blue">#define </span>MAXN 10
<span style="color: blue">int </span>colors[MAXN];<span style="color: green">//record the counter of one color
</span><span style="color: blue">int </span>colorsCounter;
<span style="color: blue">void </span>find(<span style="color: blue">int </span>arr[],<span style="color: blue">int </span>len, <span style="color: blue">int </span>colorsNeed)
{
    <span style="color: blue">int </span>bestStartIndex = 0;
    <span style="color: blue">int </span>bestLen = len;
    <span style="color: blue">int </span>lastStartIndex = 0;
    <span style="color: blue">int </span>firstFoundEnoughPearls = len;
    
    <span style="color: blue">for </span>( <span style="color: blue">int </span>i=0; i&lt;len; ++i) {
        <span style="color: blue">if </span>(!colors[arr[i]])
            colorsCounter++;
        colors[arr[i]]++;

        <span style="color: blue">if </span>(colorsCounter==colorsNeed) {
            firstFoundEnoughPearls = i;
        }
        
        <span style="color: blue">if </span>(firstFoundEnoughPearls!=len) {
            <span style="color: blue">int </span>j = lastStartIndex;
            <span style="color: blue">while </span>(colors[arr[j]]&gt;1) {
                colors[arr[j]]--;
                ++j;
            }
            <span style="color: blue">if </span>(i-j+1&lt;bestLen) {
                bestStartIndex = j;
                bestLen = i-j+1;
                <span style="color: blue">if </span>(bestLen==colorsNeed)
                    <span style="color: blue">break</span>;
            }
            lastStartIndex = j;
        }
    }

    <span style="color: blue">for </span>( <span style="color: blue">int </span>i=0; i&lt;firstFoundEnoughPearls; ++i) {
        <span style="color: blue">if </span>(!colors[arr[i]])
            colorsCounter++;
        colors[arr[i]]++;
        <span style="color: blue">int </span>j = lastStartIndex;
        <span style="color: blue">while </span>(colors[arr[j]]&gt;1) {
            colors[arr[j]]--;
            ++j;
        }
        <span style="color: blue">if </span>(i-j+1+len&lt;bestLen) {
            bestStartIndex = j;
            bestLen = i-j+1+len;
            <span style="color: blue">if </span>(bestLen==colorsNeed)
                <span style="color: blue">break</span>;
        }
        lastStartIndex = j;
    }

    <span style="color: blue">int </span>offset = bestStartIndex;
    <span style="color: blue">for </span>(<span style="color: blue">int </span>i=0; i&lt;bestLen;) {
        cout &lt;&lt; arr[i+offset] &lt;&lt; <span style="color: #a31515">" "</span>;
        ++i;
        <span style="color: blue">if </span>(i+offset&gt;=len)
            offset = 0-i;
    }
    cout &lt;&lt; endl;
}

<span style="color: blue">int </span>main()
{
    <span style="color: blue">int </span>arr[] = {1,2,3,3,2,1,4,1,2,3,2,6,4,5,2,6,2,3,4,1,2,5,2,3,4,5,6};
    <span style="color: blue">int </span>m = <span style="color: blue">sizeof</span>(arr)/<span style="color: blue">sizeof</span>(arr[0]);
    <span style="color: blue">int </span>n = 6;
    find(arr,m,n);

    <span style="color: blue">return </span>0;
}

</pre>
<p>输出：</p>
<p>2 3 4 5 6 1</p><img src ="http://www.cppblog.com/izualzhy/aggbug/168430.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-03-20 20:28 <a href="http://www.cppblog.com/izualzhy/archive/2012/03/20/168430.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]找出环的初始节点</title><link>http://www.cppblog.com/izualzhy/archive/2012/03/17/168181.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Sat, 17 Mar 2012 04:49:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/03/17/168181.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/168181.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/03/17/168181.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/168181.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/168181.html</trackback:ping><description><![CDATA[<div>[原帖在这里]：<a href="http://blog.csdn.net/nomad2/article/details/7362525">http://blog.csdn.net/nomad2/article/details/7362525</a><br />关于原帖的一些补充：<br />看了原帖的解释，还是有些不懂。<br />用数学公式稍微整理了一下，因为可能图示存在这样的问题，就是环的长度比前面链表长度要短的问题。<br />假设环之前链表（不包括环的开始节点）长度为n，两个指针相遇的地方距离环的开始点距离为m，环长为c，相遇时，n2指针绕环正在跑k2圈，n1正在跑k1圈，那么有：<br />n1指针跑的节点数*2=n2指针跑的节点数<br />即：<br />(n+k1*c-m)*2=(n+k2*c-m)<br />整理下有<br />n=m+(k2-2*k1)*c；<br />不妨设k2-2*k1=k3<br />图示里n=m，有可能n=m+k3*c,k3=0,1,2,...<br />感觉这样更好理解一些。<br />k3一定是&gt;=0的，为什么？<br /><br /><span style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span">&nbsp;
<p>Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.</p>
<p>refer to careerup</p>
<p>&nbsp;</p>
<p><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/izualzhy/0_1331899124YszD.gif" width="535" longdesc="" height="678" /><br /></p>
<p>分析的很形象，</p>
<p>If we move two pointers, one with speed 1 and another with speed 2, they will end up meeting if the linked list has a loop. Why? Think about two cars driving on a track&#8212;the faster car will always pass the slower one!<br />The tricky part here is finding the start of the loop. Imagine, as an analogy, two people racing around a track, one running twice as fast as the other. If they start off at the same place, when will they next meet? They will next meet at the start of the next lap.<br />Now, let&#8217;s suppose Fast Runner had a head start of k meters on an n step lap. When will they next meet? They will meet k meters before the start of the next lap. (Why? Fast Runner would have made k + 2(n - k) steps, including its head start, and Slow Runner would have made n - k steps. Both will be k steps before the start of the loop.)<br />Now, going back to the problem, when Fast Runner (n2) and Slow Runner (n1) are moving around our circular linked list, n2 will have a head start on the loop when n1 enters. Specifically, it will have a head start of k, where k is the number of nodes before the loop. Since n2 has a head start of k nodes, n1 and n2 will meet k nodes before the start of the loop.<br />So, we now know the following:<br />1. Head is k nodes from LoopStart (by definition).<br />2. MeetingPoint for n1 and n2 is k nodes from LoopStart (as shown above).<br />Thus, if we move n1 back to Head and keep n2 at MeetingPoint, and move them both at the same pace, they will meet at LoopStart.</p></span></div><img src ="http://www.cppblog.com/izualzhy/aggbug/168181.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-03-17 12:49 <a href="http://www.cppblog.com/izualzhy/archive/2012/03/17/168181.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>两道题目</title><link>http://www.cppblog.com/izualzhy/archive/2012/02/27/166652.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Mon, 27 Feb 2012 15:14:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/02/27/166652.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/166652.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/02/27/166652.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/166652.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/166652.html</trackback:ping><description><![CDATA[题目很简单，在csdn上看到的。<br />最近赋闲，难得在忙乱的生活里开始认真反思下自己捉襟见肘的技术水平，总结出的第一个问题就是不爱画流程图，虽然在敲代码这项体力劳动前也会有程序设计这项脑力劳动，但我很少写出来，写在纸上，结果编写的时候总会出错。<br />1.<span style="text-align: left; widows: 2; text-transform: none; background-color: #ffffff; text-indent: 0px; font: 14px/26px Arial, Helvetica, sans-serif, SimSun; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"><strong>在一50人的队列中，从左至右报数，为单数者出列，其他人再从左至右报数，为单数者出列，直至剩下最后一人，问此人是初始队列中的第多少号?
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /><br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img id="Codehighlighter1_53_179_Open_Image" onclick="this.style.display='none'; Codehighlighter1_53_179_Open_Text.style.display='none'; Codehighlighter1_53_179_Closed_Image.style.display='inline'; Codehighlighter1_53_179_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_53_179_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_53_179_Closed_Text.style.display='none'; Codehighlighter1_53_179_Open_Image.style.display='inline'; Codehighlighter1_53_179_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_53_179_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_53_179_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">50</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img id="Codehighlighter1_103_139_Open_Image" onclick="this.style.display='none'; Codehighlighter1_103_139_Open_Text.style.display='none'; Codehighlighter1_103_139_Closed_Image.style.display='inline'; Codehighlighter1_103_139_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_103_139_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_103_139_Closed_Text.style.display='none'; Codehighlighter1_103_139_Open_Image.style.display='inline'; Codehighlighter1_103_139_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(n</span><span style="color: #000000">!=</span><span style="color: #000000">1</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_103_139_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_103_139_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n</span><span style="color: #000000">&gt;&gt;=</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: #000000">&lt;&lt;=</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">12</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" /><br /></span><span style="color: #008080">14</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">16</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /></span></div><br />2.<span style="text-align: left; widows: 2; text-transform: none; background-color: #ffffff; text-indent: 0px; font: 14px/26px Arial, Helvetica, sans-serif, SimSun; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"><strong>1到9的9个数，排成三行三列的正方形，如何使其所有行、列、对角线上三个数字相加都相等
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">stdlib.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /><br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /><br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;numbers&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">9</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;obj&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">15</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img id="Codehighlighter1_141_159_Open_Image" onclick="this.style.display='none'; Codehighlighter1_141_159_Open_Text.style.display='none'; Codehighlighter1_141_159_Closed_Image.style.display='inline'; Codehighlighter1_141_159_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_141_159_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_141_159_Closed_Text.style.display='none'; Codehighlighter1_141_159_Open_Image.style.display='inline'; Codehighlighter1_141_159_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;resourceArray[numbers]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_141_159_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_141_159_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #000000">2</span><span style="color: #000000">,</span><span style="color: #000000">3</span><span style="color: #000000">,</span><span style="color: #000000">4</span><span style="color: #000000">,</span><span style="color: #000000">5</span><span style="color: #000000">,</span><span style="color: #000000">6</span><span style="color: #000000">,</span><span style="color: #000000">7</span><span style="color: #000000">,</span><span style="color: #000000">8</span><span style="color: #000000">,</span><span style="color: #000000">9</span><span style="color: #000000">}</span></span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img id="Codehighlighter1_183_185_Open_Image" onclick="this.style.display='none'; Codehighlighter1_183_185_Open_Text.style.display='none'; Codehighlighter1_183_185_Closed_Image.style.display='inline'; Codehighlighter1_183_185_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_183_185_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_183_185_Closed_Text.style.display='none'; Codehighlighter1_183_185_Open_Image.style.display='inline'; Codehighlighter1_183_185_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;array[numbers]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_183_185_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_183_185_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">0</span><span style="color: #000000">}</span></span><span style="color: #000000">;<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /><br /></span><span style="color: #008080">11</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;getResult(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;currentIndex)<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img id="Codehighlighter1_222_1406_Open_Image" onclick="this.style.display='none'; Codehighlighter1_222_1406_Open_Text.style.display='none'; Codehighlighter1_222_1406_Closed_Image.style.display='inline'; Codehighlighter1_222_1406_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_222_1406_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_222_1406_Closed_Text.style.display='none'; Codehighlighter1_222_1406_Open_Image.style.display='inline'; Codehighlighter1_222_1406_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_222_1406_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_222_1406_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img id="Codehighlighter1_249_468_Open_Image" onclick="this.style.display='none'; Codehighlighter1_249_468_Open_Text.style.display='none'; Codehighlighter1_249_468_Closed_Image.style.display='inline'; Codehighlighter1_249_468_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_249_468_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_249_468_Closed_Text.style.display='none'; Codehighlighter1_249_468_Open_Image.style.display='inline'; Codehighlighter1_249_468_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(currentIndex</span><span style="color: #000000">==</span><span style="color: #000000">9</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_249_468_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_249_468_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">array:\n</span><span style="color: #000000">"</span><span style="color: #000000">;<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img id="Codehighlighter1_319_421_Open_Image" onclick="this.style.display='none'; Codehighlighter1_319_421_Open_Text.style.display='none'; Codehighlighter1_319_421_Closed_Image.style.display='inline'; Codehighlighter1_319_421_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_319_421_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_319_421_Closed_Text.style.display='none'; Codehighlighter1_319_421_Open_Image.style.display='inline'; Codehighlighter1_319_421_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">numbers;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_319_421_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_319_421_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;array[i]&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">;<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(i</span><span style="color: #000000">%</span><span style="color: #000000">3</span><span style="color: #000000">==</span><span style="color: #000000">2</span><span style="color: #000000">)<br /></span><span style="color: #008080">18</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">19</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">20</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="color: #000000">"</span><span style="color: #000000">pause</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">21</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">;<br /></span><span style="color: #008080">22</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">23</span><span style="color: #000000"><img id="Codehighlighter1_505_1402_Open_Image" onclick="this.style.display='none'; Codehighlighter1_505_1402_Open_Text.style.display='none'; Codehighlighter1_505_1402_Closed_Image.style.display='inline'; Codehighlighter1_505_1402_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_505_1402_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_505_1402_Closed_Text.style.display='none'; Codehighlighter1_505_1402_Open_Image.style.display='inline'; Codehighlighter1_505_1402_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">numbers;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_505_1402_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_505_1402_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">24</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">25</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(;&nbsp;j</span><span style="color: #000000">&lt;</span><span style="color: #000000">currentIndex;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">j)<br /></span><span style="color: #008080">26</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(array[j]</span><span style="color: #000000">==</span><span style="color: #000000">resourceArray[i])<br /></span><span style="color: #008080">27</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /></span><span style="color: #008080">28</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(j</span><span style="color: #000000">==</span><span style="color: #000000">currentIndex)<br /></span><span style="color: #008080">29</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[currentIndex]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;resourceArray[i];<br /></span><span style="color: #008080">30</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000"><br /></span><span style="color: #008080">31</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">continue</span><span style="color: #000000">;<br /></span><span style="color: #008080">32</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;((currentIndex</span><span style="color: #000000">==</span><span style="color: #000000">2</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;(array[</span><span style="color: #000000">0</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">2</span><span style="color: #000000">])</span><span style="color: #000000">!=</span><span style="color: #000000">obj)<br /></span><span style="color: #008080">33</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">||</span><span style="color: #000000">&nbsp;(currentIndex</span><span style="color: #000000">==</span><span style="color: #000000">5</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;(array[</span><span style="color: #000000">3</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">4</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">5</span><span style="color: #000000">])</span><span style="color: #000000">!=</span><span style="color: #000000">obj)<br /></span><span style="color: #008080">34</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">||</span><span style="color: #000000">&nbsp;(currentIndex</span><span style="color: #000000">==</span><span style="color: #000000">6</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;(array[</span><span style="color: #000000">0</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">3</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">6</span><span style="color: #000000">])</span><span style="color: #000000">!=</span><span style="color: #000000">obj)<br /></span><span style="color: #008080">35</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">||</span><span style="color: #000000">&nbsp;(currentIndex</span><span style="color: #000000">==</span><span style="color: #000000">6</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;(array[</span><span style="color: #000000">2</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">4</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">6</span><span style="color: #000000">])</span><span style="color: #000000">!=</span><span style="color: #000000">obj)<br /></span><span style="color: #008080">36</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">||</span><span style="color: #000000">&nbsp;(currentIndex</span><span style="color: #000000">==</span><span style="color: #000000">7</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;(array[</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">4</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">7</span><span style="color: #000000">])</span><span style="color: #000000">!=</span><span style="color: #000000">obj)<br /></span><span style="color: #008080">37</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">||</span><span style="color: #000000">&nbsp;(currentIndex</span><span style="color: #000000">==</span><span style="color: #000000">8</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;(array[</span><span style="color: #000000">6</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">7</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">8</span><span style="color: #000000">])</span><span style="color: #000000">!=</span><span style="color: #000000">obj)<br /></span><span style="color: #008080">38</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">||</span><span style="color: #000000">&nbsp;(currentIndex</span><span style="color: #000000">==</span><span style="color: #000000">8</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;(array[</span><span style="color: #000000">2</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">5</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">8</span><span style="color: #000000">])</span><span style="color: #000000">!=</span><span style="color: #000000">obj)<br /></span><span style="color: #008080">39</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">||</span><span style="color: #000000">&nbsp;(currentIndex</span><span style="color: #000000">==</span><span style="color: #000000">8</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;(array[</span><span style="color: #000000">0</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">4</span><span style="color: #000000">]</span><span style="color: #000000">+</span><span style="color: #000000">array[</span><span style="color: #000000">8</span><span style="color: #000000">])</span><span style="color: #000000">!=</span><span style="color: #000000">obj)<br /></span><span style="color: #008080">40</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br /></span><span style="color: #008080">41</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">continue</span><span style="color: #000000">;<br /></span><span style="color: #008080">42</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getResult(currentIndex</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">);<br /></span><span style="color: #008080">43</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">44</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;<br /></span><span style="color: #008080">45</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">46</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /><br /></span><span style="color: #008080">47</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /></span><span style="color: #008080">48</span><span style="color: #000000"><img id="Codehighlighter1_1420_1454_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1420_1454_Open_Text.style.display='none'; Codehighlighter1_1420_1454_Closed_Image.style.display='inline'; Codehighlighter1_1420_1454_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_1420_1454_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1420_1454_Closed_Text.style.display='none'; Codehighlighter1_1420_1454_Open_Image.style.display='inline'; Codehighlighter1_1420_1454_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1420_1454_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_1420_1454_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">49</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;getResult(</span><span style="color: #000000">0</span><span style="color: #000000">);<br /></span><span style="color: #008080">50</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">51</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">52</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /></span></div><br /><br /></strong></span><br /></strong></span><img src ="http://www.cppblog.com/izualzhy/aggbug/166652.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-02-27 23:14 <a href="http://www.cppblog.com/izualzhy/archive/2012/02/27/166652.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]由二叉树的前序遍历序列和中序遍历序列求后序遍历序列</title><link>http://www.cppblog.com/izualzhy/archive/2012/02/24/166453.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Fri, 24 Feb 2012 13:48:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/02/24/166453.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/166453.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/02/24/166453.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/166453.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/166453.html</trackback:ping><description><![CDATA[<a href="http://blog.csdn.net/aegeaner/article/details/7289558">http://blog.csdn.net/aegeaner/article/details/7289558</a><br />写在转载前：<br />看到这篇题目时，思路和作者基本相同，不过后来看了下实现稍微复杂了些。于是大概看了遍又修改了自己的代码。<br />前序：中|左|右<br />中序：左|中|右<br /><br />要推出后序遍历的结果：<br />就是递归的得到：左|右|中的序列即可。<br />想到递归之后，题目就容易了。<br />关于更详细的思路，原作者写的比较清楚了。<br /><span style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; font: 14px/26px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span">
<p><span style="font-size: 18px">给出二叉树的前序遍历序列和中序遍历序列，求后序遍历序列。比如节点内存储的数据类型为char的二叉树，给出前序遍历序列"abdec"，中序遍历序列"dbeac"，则输出后序遍历序列。</span></p>
<p><span style="font-size: 18px">可以写一个递归函数实现，首先由前序遍历序列的第一个元素确定根节点，然后再中序遍历序列中找出等于该元素的位置索引，则根据中序遍历的定义，该位置以左为左子树，以右为右子树。根据前序遍历的定义，第一个元素之后跟着的也分别是左子树的前序遍历和右子树的前序遍历，由中序序列可以分别确定这两个子树序列的长度，从而可以分隔。对两个子树的前序遍历序列和中序遍历序列分别递归进行后序遍历，然后最终打印根节点，即为整棵树的后续遍历。</span></p>
<p><span style="font-size: 18px">转换算法不难，难点在于没有左子树和没有右子树的情况。如果没有左子树，则不对左子树递归遍历，右子树也一样。如果既没有左子树也没有右子树，则退化为叶子节点，这是递归处理的边界情况。</span></p></span><br />最后贴下我自己的代码,大概都差不多： 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;postOrder(</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">preStart,</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">preEnd,&nbsp;</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">inStart,&nbsp;</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">inEnd)<br /><img id="Codehighlighter1_138_484_Open_Image" onclick="this.style.display='none'; Codehighlighter1_138_484_Open_Text.style.display='none'; Codehighlighter1_138_484_Closed_Image.style.display='inline'; Codehighlighter1_138_484_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_138_484_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_138_484_Closed_Text.style.display='none'; Codehighlighter1_138_484_Open_Image.style.display='inline'; Codehighlighter1_138_484_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_138_484_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_138_484_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;root&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">preStart;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;len&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;preEnd</span><span style="color: #000000">-</span><span style="color: #000000">preStart,i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(&nbsp;;&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">(inStart</span><span style="color: #000000">+</span><span style="color: #000000">i)</span><span style="color: #000000">!=</span><span style="color: #000000">root&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">len;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(i</span><span style="color: #000000">!=</span><span style="color: #000000">0</span><span style="color: #000000">)<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;postOrder(preStart</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">,preStart</span><span style="color: #000000">+</span><span style="color: #000000">i,inStart,inStart</span><span style="color: #000000">+</span><span style="color: #000000">i</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">);</span><span style="color: #008000">//</span><span style="color: #008000">traverse&nbsp;left&nbsp;child&nbsp;tree</span><span style="color: #008000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(i</span><span style="color: #000000">!=</span><span style="color: #000000">len)<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;postOrder(preStart</span><span style="color: #000000">+</span><span style="color: #000000">i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">,preEnd,inStart</span><span style="color: #000000">+</span><span style="color: #000000">i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">,inEnd);</span><span style="color: #008000">//</span><span style="color: #008000">traverse&nbsp;right&nbsp;child&nbsp;tree</span><span style="color: #008000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;root;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_498_645_Open_Image" onclick="this.style.display='none'; Codehighlighter1_498_645_Open_Text.style.display='none'; Codehighlighter1_498_645_Closed_Image.style.display='inline'; Codehighlighter1_498_645_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_498_645_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_498_645_Closed_Text.style.display='none'; Codehighlighter1_498_645_Open_Image.style.display='inline'; Codehighlighter1_498_645_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_498_645_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_498_645_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;preOrder[]</span><span style="color: #000000">=</span><span style="color: #000000">"</span><span style="color: #000000">abdcef</span><span style="color: #000000">"</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;inOrder[]</span><span style="color: #000000">=</span><span style="color: #000000">"</span><span style="color: #000000">dbaecf</span><span style="color: #000000">"</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;postOrder(preOrder,preOrder</span><span style="color: #000000">+</span><span style="color: #000000">5</span><span style="color: #000000">,inOrder,inOrder</span><span style="color: #000000">+</span><span style="color: #000000">5</span><span style="color: #000000">);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span></div><br />note：<br />如果节点内容相同，算法不可用。比如：<br />pre:aca<br />in:caa<br /><br />则<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a<br />&nbsp;&nbsp;&nbsp; /&nbsp;&nbsp; \<br />&nbsp;&nbsp;&nbsp;c&nbsp;&nbsp;&nbsp;&nbsp; a<br /><br />与<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a<br />&nbsp;&nbsp;&nbsp;&nbsp;/<br />&nbsp;&nbsp;&nbsp;c<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a<br />都满足要求，<br />而后序遍历分别为：<br />caa，aca。 <img src ="http://www.cppblog.com/izualzhy/aggbug/166453.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-02-24 21:48 <a href="http://www.cppblog.com/izualzhy/archive/2012/02/24/166453.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>堆排序简单示例</title><link>http://www.cppblog.com/izualzhy/archive/2012/02/22/166291.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Wed, 22 Feb 2012 15:35:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/02/22/166291.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/166291.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/02/22/166291.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/166291.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/166291.html</trackback:ping><description><![CDATA[好久不写排序，快忘了，花了10分钟才写好，如果让直接在纸上写代码，估计难写对。。。
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;Len&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">10</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img id="Codehighlighter1_79_99_Open_Image" onclick="this.style.display='none'; Codehighlighter1_79_99_Open_Text.style.display='none'; Codehighlighter1_79_99_Closed_Image.style.display='inline'; Codehighlighter1_79_99_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_79_99_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_79_99_Closed_Text.style.display='none'; Codehighlighter1_79_99_Open_Image.style.display='inline'; Codehighlighter1_79_99_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;array[Len]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_79_99_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_79_99_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">9</span><span style="color: #000000">,</span><span style="color: #000000">2</span><span style="color: #000000">,</span><span style="color: #000000">6</span><span style="color: #000000">,</span><span style="color: #000000">4</span><span style="color: #000000">,</span><span style="color: #000000">3</span><span style="color: #000000">,</span><span style="color: #000000">7</span><span style="color: #000000">,</span><span style="color: #000000">8</span><span style="color: #000000">,</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #000000">5</span><span style="color: #000000">}</span></span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;leftChild(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;index)<br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img id="Codehighlighter1_128_154_Open_Image" onclick="this.style.display='none'; Codehighlighter1_128_154_Open_Text.style.display='none'; Codehighlighter1_128_154_Closed_Image.style.display='inline'; Codehighlighter1_128_154_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_128_154_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_128_154_Closed_Text.style.display='none'; Codehighlighter1_128_154_Open_Image.style.display='inline'; Codehighlighter1_128_154_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_128_154_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_128_154_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000">*</span><span style="color: #000000">index&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">11</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">12</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;print()<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img id="Codehighlighter1_170_254_Open_Image" onclick="this.style.display='none'; Codehighlighter1_170_254_Open_Text.style.display='none'; Codehighlighter1_170_254_Closed_Image.style.display='inline'; Codehighlighter1_170_254_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_170_254_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_170_254_Closed_Text.style.display='none'; Codehighlighter1_170_254_Open_Image.style.display='inline'; Codehighlighter1_170_254_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_170_254_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_170_254_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">Len;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;array[i]&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">;<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">18</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">19</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;percDown(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;heapSize)<br /></span><span style="color: #008080">20</span><span style="color: #000000"><img id="Codehighlighter1_292_650_Open_Image" onclick="this.style.display='none'; Codehighlighter1_292_650_Open_Text.style.display='none'; Codehighlighter1_292_650_Closed_Image.style.display='inline'; Codehighlighter1_292_650_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_292_650_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_292_650_Closed_Text.style.display='none'; Codehighlighter1_292_650_Open_Image.style.display='inline'; Codehighlighter1_292_650_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_292_650_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_292_650_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">21</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;temp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[i];<br /></span><span style="color: #008080">22</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;child&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;leftChild(i);<br /></span><span style="color: #008080">23</span><span style="color: #000000"><img id="Codehighlighter1_376_626_Open_Image" onclick="this.style.display='none'; Codehighlighter1_376_626_Open_Text.style.display='none'; Codehighlighter1_376_626_Closed_Image.style.display='inline'; Codehighlighter1_376_626_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_376_626_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_376_626_Closed_Text.style.display='none'; Codehighlighter1_376_626_Open_Image.style.display='inline'; Codehighlighter1_376_626_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(child</span><span style="color: #000000">&lt;</span><span style="color: #000000">heapSize)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_376_626_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_376_626_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">24</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(child</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">&lt;</span><span style="color: #000000">heapSize)</span><span style="color: #008000">//</span><span style="color: #008000">note&nbsp;here</span><span style="color: #008000"><br /></span><span style="color: #008080">25</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(array[child]</span><span style="color: #000000">&gt;</span><span style="color: #000000">array[child</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">])<br /></span><span style="color: #008080">26</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;child</span><span style="color: #000000">++</span><span style="color: #000000">;<br /></span><span style="color: #008080">27</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">28</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(array[child]</span><span style="color: #000000">&gt;</span><span style="color: #000000">temp)<br /></span><span style="color: #008080">29</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /></span><span style="color: #008080">30</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[child];<br /></span><span style="color: #008080">31</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;child;<br /></span><span style="color: #008080">32</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;child&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;leftChild(i);<br /></span><span style="color: #008080">33</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">34</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">35</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;array[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;temp;<br /></span><span style="color: #008080">36</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">37</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">38</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;buildHeap()<br /></span><span style="color: #008080">39</span><span style="color: #000000"><img id="Codehighlighter1_670_758_Open_Image" onclick="this.style.display='none'; Codehighlighter1_670_758_Open_Text.style.display='none'; Codehighlighter1_670_758_Closed_Image.style.display='inline'; Codehighlighter1_670_758_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_670_758_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_670_758_Closed_Text.style.display='none'; Codehighlighter1_670_758_Open_Image.style.display='inline'; Codehighlighter1_670_758_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_670_758_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_670_758_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">40</span><span style="color: #000000"><img id="Codehighlighter1_708_756_Open_Image" onclick="this.style.display='none'; Codehighlighter1_708_756_Open_Text.style.display='none'; Codehighlighter1_708_756_Closed_Image.style.display='inline'; Codehighlighter1_708_756_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_708_756_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_708_756_Closed_Text.style.display='none'; Codehighlighter1_708_756_Open_Image.style.display='inline'; Codehighlighter1_708_756_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">Len</span><span style="color: #000000">/</span><span style="color: #000000">2</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&gt;=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">i)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_708_756_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_708_756_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">41</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;percDown(i,Len);<br /></span><span style="color: #008080">42</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print();<br /></span><span style="color: #008080">43</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">44</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">45</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">46</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">47</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;deleteMax(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;curSize)<br /></span><span style="color: #008080">48</span><span style="color: #000000"><img id="Codehighlighter1_790_907_Open_Image" onclick="this.style.display='none'; Codehighlighter1_790_907_Open_Text.style.display='none'; Codehighlighter1_790_907_Closed_Image.style.display='inline'; Codehighlighter1_790_907_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_790_907_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_790_907_Closed_Text.style.display='none'; Codehighlighter1_790_907_Open_Image.style.display='inline'; Codehighlighter1_790_907_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_790_907_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_790_907_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">49</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;temp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[curSize</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">];<br /></span><span style="color: #008080">50</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;array[curSize</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[</span><span style="color: #000000">0</span><span style="color: #000000">];<br /></span><span style="color: #008080">51</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;array[</span><span style="color: #000000">0</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;temp;<br /></span><span style="color: #008080">52</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">53</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;percDown(</span><span style="color: #000000">0</span><span style="color: #000000">,curSize</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">);<br /></span><span style="color: #008080">54</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">55</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">56</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /></span><span style="color: #008080">57</span><span style="color: #000000"><img id="Codehighlighter1_921_1022_Open_Image" onclick="this.style.display='none'; Codehighlighter1_921_1022_Open_Text.style.display='none'; Codehighlighter1_921_1022_Closed_Image.style.display='inline'; Codehighlighter1_921_1022_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_921_1022_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_921_1022_Closed_Text.style.display='none'; Codehighlighter1_921_1022_Open_Image.style.display='inline'; Codehighlighter1_921_1022_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_921_1022_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_921_1022_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">58</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;buildHeap();<br /></span><span style="color: #008080">59</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">Len;&nbsp;i</span><span style="color: #000000">&gt;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">i)<br /></span><span style="color: #008080">60</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deleteMax(i);<br /></span><span style="color: #008080">61</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">62</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;print();<br /></span><span style="color: #008080">63</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">64</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">65</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">66</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div><br /><img src ="http://www.cppblog.com/izualzhy/aggbug/166291.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-02-22 23:35 <a href="http://www.cppblog.com/izualzhy/archive/2012/02/22/166291.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>约瑟夫问题</title><link>http://www.cppblog.com/izualzhy/archive/2012/02/12/165404.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Sun, 12 Feb 2012 02:51:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/02/12/165404.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/165404.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/02/12/165404.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/165404.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/165404.html</trackback:ping><description><![CDATA[题目很简单，就是循环链表的应用~主要是周末稍微动下手，要不静不下心来。。
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img id="Codehighlighter1_61_184_Open_Image" onclick="this.style.display='none'; Codehighlighter1_61_184_Open_Text.style.display='none'; Codehighlighter1_61_184_Closed_Image.style.display='inline'; Codehighlighter1_61_184_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_61_184_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_61_184_Closed_Text.style.display='none'; Codehighlighter1_61_184_Open_Image.style.display='inline'; Codehighlighter1_61_184_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif">typedef&nbsp;</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;Node</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_61_184_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_61_184_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Node(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n</span><span style="color: #000000">=</span><span style="color: #0000ff">int</span><span style="color: #000000">(),&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">ne</span><span style="color: #000000">=</span><span style="color: #000000">NULL)&nbsp;:<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;number(n)&nbsp;,<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next(ne)<br /><img id="Codehighlighter1_143_149_Open_Image" onclick="this.style.display='none'; Codehighlighter1_143_149_Open_Text.style.display='none'; Codehighlighter1_143_149_Closed_Image.style.display='inline'; Codehighlighter1_143_149_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_143_149_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_143_149_Closed_Text.style.display='none'; Codehighlighter1_143_149_Open_Image.style.display='inline'; Codehighlighter1_143_149_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_143_149_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_143_149_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;number;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">next;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000">Node;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Josephus(</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;personCounter,&nbsp;</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;luckyNumber)<br /><img id="Codehighlighter1_254_857_Open_Image" onclick="this.style.display='none'; Codehighlighter1_254_857_Open_Text.style.display='none'; Codehighlighter1_254_857_Closed_Image.style.display='inline'; Codehighlighter1_254_857_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_254_857_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_254_857_Closed_Text.style.display='none'; Codehighlighter1_254_857_Open_Image.style.display='inline'; Codehighlighter1_254_857_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_254_857_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_254_857_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">lastnode&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;Node(personCounter);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">node&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;lastnode;<br /><img id="Codehighlighter1_373_453_Open_Image" onclick="this.style.display='none'; Codehighlighter1_373_453_Open_Text.style.display='none'; Codehighlighter1_373_453_Closed_Image.style.display='inline'; Codehighlighter1_373_453_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_373_453_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_373_453_Closed_Text.style.display='none'; Codehighlighter1_373_453_Open_Image.style.display='inline'; Codehighlighter1_373_453_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">personCounter</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&gt;=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">i)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_373_453_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_373_453_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">currentNode&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;Node(i,node);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;currentNode;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;lastnode</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;node;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;counter&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;personCounter;<br /><img id="Codehighlighter1_538_855_Open_Image" onclick="this.style.display='none'; Codehighlighter1_538_855_Open_Text.style.display='none'; Codehighlighter1_538_855_Closed_Image.style.display='inline'; Codehighlighter1_538_855_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_538_855_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_538_855_Closed_Text.style.display='none'; Codehighlighter1_538_855_Open_Image.style.display='inline'; Codehighlighter1_538_855_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(counter</span><span style="color: #000000">&gt;</span><span style="color: #000000">0</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_538_855_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_538_855_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">prevNode&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;lastnode;<br /><img id="Codehighlighter1_608_678_Open_Image" onclick="this.style.display='none'; Codehighlighter1_608_678_Open_Text.style.display='none'; Codehighlighter1_608_678_Closed_Image.style.display='inline'; Codehighlighter1_608_678_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_608_678_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_608_678_Closed_Text.style.display='none'; Codehighlighter1_608_678_Open_Image.style.display='inline'; Codehighlighter1_608_678_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">6</span><span style="color: #000000">;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_608_678_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_608_678_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prevNode&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;node;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;node</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prevNode</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;node</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;node</span><span style="color: #000000">-&gt;</span><span style="color: #000000">number&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prevNode&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;node;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;node</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;prevNode;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">counter;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_871_907_Open_Image" onclick="this.style.display='none'; Codehighlighter1_871_907_Open_Text.style.display='none'; Codehighlighter1_871_907_Closed_Image.style.display='inline'; Codehighlighter1_871_907_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_871_907_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_871_907_Closed_Text.style.display='none'; Codehighlighter1_871_907_Open_Image.style.display='inline'; Codehighlighter1_871_907_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_871_907_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_871_907_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Josephus(</span><span style="color: #000000">10</span><span style="color: #000000">,</span><span style="color: #000000">6</span><span style="color: #000000">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img id="Codehighlighter1_970_1093_Open_Image" onclick="this.style.display='none'; Codehighlighter1_970_1093_Open_Text.style.display='none'; Codehighlighter1_970_1093_Closed_Image.style.display='inline'; Codehighlighter1_970_1093_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_970_1093_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_970_1093_Closed_Text.style.display='none'; Codehighlighter1_970_1093_Open_Image.style.display='inline'; Codehighlighter1_970_1093_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif">typedef&nbsp;</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;Node</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_970_1093_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_970_1093_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Node(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n</span><span style="color: #000000">=</span><span style="color: #0000ff">int</span><span style="color: #000000">(),&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">ne</span><span style="color: #000000">=</span><span style="color: #000000">NULL)&nbsp;:<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;number(n)&nbsp;,<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next(ne)<br /><img id="Codehighlighter1_1052_1058_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1052_1058_Open_Text.style.display='none'; Codehighlighter1_1052_1058_Closed_Image.style.display='inline'; Codehighlighter1_1052_1058_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1052_1058_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1052_1058_Closed_Text.style.display='none'; Codehighlighter1_1052_1058_Open_Image.style.display='inline'; Codehighlighter1_1052_1058_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1052_1058_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1052_1058_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;number;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">next;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000">Node;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Josephus(</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;personCounter,&nbsp;</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;luckyNumber)<br /><img id="Codehighlighter1_1163_1766_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1163_1766_Open_Text.style.display='none'; Codehighlighter1_1163_1766_Closed_Image.style.display='inline'; Codehighlighter1_1163_1766_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_1163_1766_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1163_1766_Closed_Text.style.display='none'; Codehighlighter1_1163_1766_Open_Image.style.display='inline'; Codehighlighter1_1163_1766_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1163_1766_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1163_1766_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">lastnode&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;Node(personCounter);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">node&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;lastnode;<br /><img id="Codehighlighter1_1282_1362_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1282_1362_Open_Text.style.display='none'; Codehighlighter1_1282_1362_Closed_Image.style.display='inline'; Codehighlighter1_1282_1362_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1282_1362_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1282_1362_Closed_Text.style.display='none'; Codehighlighter1_1282_1362_Open_Image.style.display='inline'; Codehighlighter1_1282_1362_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">personCounter</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&gt;=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">i)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1282_1362_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1282_1362_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">currentNode&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;Node(i,node);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;currentNode;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;lastnode</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;node;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;counter&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;personCounter;<br /><img id="Codehighlighter1_1447_1764_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1447_1764_Open_Text.style.display='none'; Codehighlighter1_1447_1764_Closed_Image.style.display='inline'; Codehighlighter1_1447_1764_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1447_1764_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1447_1764_Closed_Text.style.display='none'; Codehighlighter1_1447_1764_Open_Image.style.display='inline'; Codehighlighter1_1447_1764_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(counter</span><span style="color: #000000">&gt;</span><span style="color: #000000">0</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1447_1764_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1447_1764_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">prevNode&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;lastnode;<br /><img id="Codehighlighter1_1517_1587_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1517_1587_Open_Text.style.display='none'; Codehighlighter1_1517_1587_Closed_Image.style.display='inline'; Codehighlighter1_1517_1587_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1517_1587_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1517_1587_Closed_Text.style.display='none'; Codehighlighter1_1517_1587_Open_Image.style.display='inline'; Codehighlighter1_1517_1587_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">6</span><span style="color: #000000">;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1517_1587_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1517_1587_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prevNode&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;node;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;node</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prevNode</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;node</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;node</span><span style="color: #000000">-&gt;</span><span style="color: #000000">number&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prevNode&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;node;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;node</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;prevNode;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">counter;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_1780_1816_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1780_1816_Open_Text.style.display='none'; Codehighlighter1_1780_1816_Closed_Image.style.display='inline'; Codehighlighter1_1780_1816_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_1780_1816_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1780_1816_Closed_Text.style.display='none'; Codehighlighter1_1780_1816_Open_Image.style.display='inline'; Codehighlighter1_1780_1816_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1780_1816_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1780_1816_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Josephus(</span><span style="color: #000000">10</span><span style="color: #000000">,</span><span style="color: #000000">6</span><span style="color: #000000">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div>。<br /><img src ="http://www.cppblog.com/izualzhy/aggbug/165404.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-02-12 10:51 <a href="http://www.cppblog.com/izualzhy/archive/2012/02/12/165404.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>逆转含有n个元素的单链表</title><link>http://www.cppblog.com/izualzhy/archive/2012/02/09/165244.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Thu, 09 Feb 2012 13:58:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/02/09/165244.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/165244.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/02/09/165244.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/165244.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/165244.html</trackback:ping><description><![CDATA[<p>该题目是算法导论10.2-7的习题，拿出来做下，题目要求O（n）的非递归过程，且除了链表本身占用的空间外，该过程仅使用固定量的存储空间。 <p>代码如下：</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #008000">/********************************************************************************</span><br><span style="color: #008000"></span><br><span style="color: #008000">**</span><br><span style="color: #008000"></span><br><span style="color: #008000">** Filename: ReverseList.cpp</span><br><span style="color: #008000"></span><br><span style="color: #008000">**</span><br><span style="color: #008000"></span><br><span style="color: #008000">** Description: reverse a list by two methods: recursive and nonrecursive</span><br><span style="color: #008000"></span><br><span style="color: #008000">**</span><br><span style="color: #008000"></span><br><span style="color: #008000">** Version: 1.0</span><br><span style="color: #008000"></span><br><span style="color: #008000">** Created: 2012年02月09日 12时18分59秒</span><br><span style="color: #008000"></span><br><span style="color: #008000">*********************************************************************************/</span><br><br><span style="color: #cc6633">#include</span> &lt;iostream&gt;<br><br><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;<br><br><span style="color: #0000ff">typedef</span> <span style="color: #0000ff">struct</span> node{<br><br>node(<span style="color: #0000ff">int</span> d=<span style="color: #0000ff">int</span>(), node *n=NULL) :<br><br>data(d) ,<br><br>next(n)<br><br>{<br><br>}<br><br><span style="color: #0000ff">int</span> data;<br><br>node *next;<br><br>}node;<br><br>node *head;<br><br><span style="color: #0000ff">void</span> print(<span style="color: #0000ff">const</span> <span style="color: #0000ff">char</span> *s)<br><br>{<br><br>cout &lt;&lt; s &lt;&lt; endl;<br><br>node *temp = head;<br><br><span style="color: #0000ff">while</span> (temp!=NULL) {<br><br>cout &lt;&lt; temp-&gt;data &lt;&lt; <span style="color: #006080">" "</span>;<br><br>temp = temp-&gt;next;<br><br>}<br><br>cout &lt;&lt; endl;<br><br>}<br><br><span style="color: #0000ff">void</span> reverseListNonRecursive()<br><br>{<br><br>print(<span style="color: #006080">"before reverse list nonrecursive:"</span>);<br><br>node *temp = head;<br><br>node *next = temp-&gt;next;<br><br>node *prev = NULL;<br><br><span style="color: #0000ff">while</span> (temp!=NULL) {<br><br>next = temp-&gt;next;<br><br>temp-&gt;next = prev;<br><br>prev = temp;<br><br>temp = next;<br><br>}<br><br>head = prev;<br><br>print(<span style="color: #006080">"after reverse list nonrecursive:"</span>);<br><br>}<br><br><span style="color: #0000ff">void</span> reverseListRecursive(node* n)<br><br>{<br><br>node* nextNode = n-&gt;next;<br><br><span style="color: #0000ff">if</span> (nextNode==NULL) {<br><br>head = n;<br><br><span style="color: #0000ff">return</span>;<br><br>}<br><br>reverseListRecursive(nextNode);<br><br>nextNode-&gt;next = n;<br><br>}<br><br><span style="color: #0000ff">void</span> reverseListRecursive()<br><br>{<br><br>print(<span style="color: #006080">"before reverse list recursive:"</span>);<br><br>node* temp = head;<br><br>reverseListRecursive(head);<br><br>temp-&gt;next = NULL;<br><br>print(<span style="color: #006080">"after reverse list recursive:"</span>);<br><br>}<br><br><span style="color: #0000ff">int</span> main()<br><br>{<br><br>node n5(5);<br><br>node n4(4,&amp;n5);<br><br>node n3(3,&amp;n4);<br><br>node n2(2,&amp;n3);<br><br>node n1(1,&amp;n2);<br><br>head = &amp;n1;<br><br>reverseListNonRecursive();<br><br>reverseListRecursive();<br><br><span style="color: #0000ff">return</span> 0;<br><br>}<br><br></pre><br></div>
<p>输出：
<p>zhy@desktop:~/doublemint/factory$ ./a.out 
<p>before reverse list nonrecursive:
<p>1 2 3 4 5
<p>after reverse list nonrecursive:
<p>5 4 3 2 1
<p>before reverse list recursive:
<p>5 4 3 2 1
<p>after reverse list recursive:
<p>1 2 3 4 5
<p><strong>个人感觉非递归和递归过程都有一点要注意的地方：</strong>
<p><strong>1. 非递归要记录previous,current,next三个结点，这样代码编写会很方便。如果只用两个指针应该也行，但我没想出来怎么做。</strong>
<p><strong>2. 递归。。。真心难懂，想了很久，发现代码写起来很短。。。</strong><img src ="http://www.cppblog.com/izualzhy/aggbug/165244.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-02-09 21:58 <a href="http://www.cppblog.com/izualzhy/archive/2012/02/09/165244.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>找循环节的问题</title><link>http://www.cppblog.com/izualzhy/archive/2012/01/05/163660.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Thu, 05 Jan 2012 14:27:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/01/05/163660.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/163660.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/01/05/163660.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/163660.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/163660.html</trackback:ping><description><![CDATA[<p>该问题是下述问题的子问题： <p>如何将一个无限循环小数转化为分数？ <p>比如3.12345234234234 = 3+12345/100000+（找到循环节，等比序列之和求之）。 <p>循环节为234，怎么找到呢? <p>例子是我想到的一个办法：</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #cc6633">#include</span> &lt;iostream&gt;<br><br><span style="color: #cc6633">#include</span> &lt;string&gt;<br><br><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;<br><br><span style="color: #008000">/** return true if the two substrs,started at position left1(left2) ,has a length of len characters, have no differences*/</span><br><br><span style="color: #0000ff">bool</span> compare(<span style="color: #0000ff">const</span> string &amp;str, <span style="color: #0000ff">int</span> left1, <span style="color: #0000ff">int</span> left2, <span style="color: #0000ff">int</span> len)<br><br>{<br><br><span style="color: #0000ff">int</span> i = 0;<br><br><span style="color: #0000ff">for</span> ( ; i&lt;len; ++i)<br><br><span style="color: #0000ff">if</span> (str[left1+i]!=str[left2+i])<br><br><span style="color: #0000ff">break</span>;<br><br><span style="color: #0000ff">return</span> (i==len);<br><br>}<br><br><span style="color: #0000ff">int</span> main(<span style="color: #0000ff">int</span> argc, <span style="color: #0000ff">char</span> *argv[])<br><br>{<br><br>string str(argv[1]);<br><br>size_t len = str.size();<br><br><span style="color: #0000ff">int</span> i=len-1;<br><br><span style="color: #0000ff">int</span> repetendLen = 0;<br><br><span style="color: #0000ff">int</span> repetendCount = 0;<br><br><span style="color: #008000">//这个条件是我自己给的，即何时结束搜索判断有无循环节,假定为循环的字符数出现一半以上才判断为真</span><br><br><span style="color: #0000ff">while</span> (i&gt;=repetendLen*repetendCount) {<br><br>i--;<span style="color: #008000">//逐个向前搜索</span><br><br><span style="color: #0000ff">if</span> (str[i]==str[len-1]) {<span style="color: #008000">//如果当前char与最后一个相同，接下来判断是否为循环节</span><br><br>repetendLen = len-1-i;<span style="color: #008000">//记录循环节（可能是）长度</span><br><br>repetendCount = 1;<span style="color: #008000">//循环节出现次数，刚开始为1.</span><br><br><span style="color: #0000ff">while</span> (i&gt;=repetendLen*repetendCount) {<span style="color: #008000">//判断该循环节是否循环出现了足够多的次数</span><br><br><span style="color: #0000ff">if</span> (compare(str,i+1,i+1-repetendLen,repetendLen)) {<span style="color: #008000">//为真，则继续向前判断</span><br><br>repetendCount++;<br><br>i-=repetendLen;<br><br>}<br><br><span style="color: #0000ff">else</span> {<span style="color: #008000">//没有的话返回主循环继续搜索</span><br><br><span style="color: #0000ff">break</span>;<br><br>}<br><br>}<br><br>}<br><br>}<br><br><span style="color: #0000ff">if</span> (repetendCount&gt;1)<br><br>cout &lt;&lt; str.substr(len-repetendLen) &lt;&lt; endl;<br><br><span style="color: #0000ff">return</span> 0;<br><br>}<br></pre><br></div><img src ="http://www.cppblog.com/izualzhy/aggbug/163660.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-01-05 22:27 <a href="http://www.cppblog.com/izualzhy/archive/2012/01/05/163660.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>大整数相乘</title><link>http://www.cppblog.com/izualzhy/archive/2012/01/05/163659.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Thu, 05 Jan 2012 14:26:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/01/05/163659.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/163659.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/01/05/163659.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/163659.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/163659.html</trackback:ping><description><![CDATA[<p>&nbsp; <p>翻到之前自己写的大整数相乘的例子，有很多出错的地方。最近碰到这个问题，别人指点了下，自己又重新整理了一遍思路，重写了一遍。代码里都写上了注释，想到了两种方法。第一种要逐个求目标数的每一位，第二种方法是类似于我们手动算乘法时的思想。我想到的是第一种方法，但是似乎temp的值会变的比较大，如果超出了int的范围就出错了。不过两个方法一般情况下都是正确的。</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #cc6633">#include</span> &lt;iostream&gt;<br><br><span style="color: #cc6633">#include</span> &lt;string.h&gt;<br><br><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;<br><br><span style="color: #0000ff">class</span> BigNumber {<br><br><span style="color: #0000ff">public</span>:<br><br>BigNumber(<span style="color: #0000ff">const</span> string &amp;str=string())<br><br>{<br><br><span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=str.size()-1; i&gt;=0; --i)<br><br>mDigit.push_back(str[i]);<br><br>}<br><br><span style="color: #0000ff">const</span> BigNumber <span style="color: #0000ff">operator</span>*(<span style="color: #0000ff">const</span> BigNumber&amp; other) <span style="color: #0000ff">const</span>;<br><br>size_t digitNums() <span style="color: #0000ff">const</span> { <span style="color: #0000ff">return</span> mDigit.length(); }<br><br><span style="color: #0000ff">int</span> <span style="color: #0000ff">operator</span>[](<span style="color: #0000ff">int</span> i) <span style="color: #0000ff">const</span><br><br>{<br><br><span style="color: #008000">//assert(i&gt;=0&amp;&amp;i&lt;mDigit.size());</span><br><br><span style="color: #0000ff">return</span> mDigit[i] - <span style="color: #006080">'0'</span>;<br><br>}<br><br><span style="color: #0000ff">friend</span> ostream &amp;<span style="color: #0000ff">operator</span>&lt;&lt;(ostream &amp;out, <span style="color: #0000ff">const</span> BigNumber&amp; bn)<br><br>{<br><br><span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=bn.mDigit.size()-1; i&gt;=0; --i)<br><br>out &lt;&lt; bn.mDigit[i];<br><br><span style="color: #0000ff">return</span> out;<br><br>}<br><br><span style="color: #0000ff">private</span>:<br><br>string mDigit;<br><br>};<br><br><span style="color: #0000ff">const</span> BigNumber BigNumber::<span style="color: #0000ff">operator</span>*(<span style="color: #0000ff">const</span> BigNumber &amp;other) <span style="color: #0000ff">const</span><br><br>{<br><br>BigNumber obj;<br><br>size_t thisSize = <span style="color: #0000ff">this</span>-&gt;digitNums();<br><br>size_t otherSize = other.digitNums();<br><br>size_t maxSize = thisSize + otherSize;<br><br><span style="color: #0000ff">int</span> temp = 0;<br><br><span style="color: #008000">/** </span><br><span style="color: #008000"></span><br><span style="color: #008000">* 逐个计算结果每一位的值，</span><br><span style="color: #008000"></span><br><span style="color: #008000">* 例如：</span><br><span style="color: #008000"></span><br><span style="color: #008000">* 个位是由两个数的个位乘积mod10(如果比10大，则用temp记录下来供接下来的位数计算时使用),</span><br><span style="color: #008000"></span><br><span style="color: #008000">* 十位则是由第一个数的个位乘以第一个数的十位</span><br><span style="color: #008000"></span><br><span style="color: #008000">* 与 第一个数的十位乘以第二个数的个位</span><br><span style="color: #008000"></span><br><span style="color: #008000">* 与 之前一位剩余的数字之和mod10</span><br><span style="color: #008000"></span><br><span style="color: #008000">* 其他位数类推</span><br><span style="color: #008000"></span><br><span style="color: #008000">* */</span><br><br><span style="color: #0000ff">for</span> ( size_t i=0; i&lt;maxSize; i++) {<br><br><span style="color: #0000ff">for</span> ( size_t j=0; j&lt;i+1 , j&lt;thisSize; j++) {<br><br><span style="color: #0000ff">if</span> ((i-j)&lt;otherSize)<br><br>temp += (*<span style="color: #0000ff">this</span>)[j]*other[i-j];<br><br>}<br><br>obj.mDigit.push_back(temp%10 + <span style="color: #006080">'0'</span>);<br><br>temp/=10;<br><br>}<br><br><span style="color: #008000">/** </span><br><span style="color: #008000"></span><br><span style="color: #008000">* 与乘法的基本原理非常类似</span><br><span style="color: #008000"></span><br><span style="color: #008000">* 最后结果可以这么获得：</span><br><span style="color: #008000"></span><br><span style="color: #008000">* 1.第二个数的个位逐个乘以第一个数的所有位,记录该结果</span><br><span style="color: #008000"></span><br><span style="color: #008000">* (注意跟前一方法类似，比如当十位数字大于10时需要加到百位上来)</span><br><span style="color: #008000"></span><br><span style="color: #008000">* 2.第二个数的十位逐个乘以第一个数的所有位，但所有的位都提高一位存储（因为是十位乘的）</span><br><span style="color: #008000"></span><br><span style="color: #008000">* 同时计算时先与前一次结果相加,依次类推</span><br><span style="color: #008000"></span><br><span style="color: #008000">* */</span><br><br><span style="color: #008000">//obj.mDigit = string(maxSize,'0');</span><br><br><span style="color: #008000">//for ( size_t i=0; i&lt;thisSize; ++i) {</span><br><br><span style="color: #008000">//for ( size_t j=0; j&lt;otherSize || temp!=0; ++j) {</span><br><br><span style="color: #008000">//temp += (obj.mDigit[i+j] - '0');</span><br><br><span style="color: #008000">//if (j&lt;otherSize)</span><br><br><span style="color: #008000">//temp += (*this)[i]*other[j];</span><br><br><span style="color: #008000">//obj.mDigit[i+j] = temp%10 + '0';</span><br><br><span style="color: #008000">//temp /=10;</span><br><br><span style="color: #008000">//}</span><br><br><span style="color: #008000">//}</span><br><br><span style="color: #0000ff">return</span> obj;<br><br>}<br><br><span style="color: #0000ff">int</span> main(<span style="color: #0000ff">int</span> argc, <span style="color: #0000ff">char</span>*argv[])<br><br>{<br><br>BigNumber bn1(argv[1]);<br><br>BigNumber bn2(argv[2]);<br><br>cout &lt;&lt; bn1 &lt;&lt; endl;<br><br>cout &lt;&lt; bn2 &lt;&lt; endl;<br><br>BigNumber bn3 = bn1*bn2;<br><br>cout &lt;&lt; bn3 &lt;&lt; endl;<br><br><span style="color: #0000ff">return</span> 0;<br><br>}<br></pre><br></div><img src ="http://www.cppblog.com/izualzhy/aggbug/163659.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-01-05 22:26 <a href="http://www.cppblog.com/izualzhy/archive/2012/01/05/163659.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>24点游戏续</title><link>http://www.cppblog.com/izualzhy/archive/2011/12/25/162800.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Sun, 25 Dec 2011 14:45:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/12/25/162800.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/162800.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/12/25/162800.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/162800.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/162800.html</trackback:ping><description><![CDATA[<p>之前写过自己的解法：<a title="http://www.cppblog.com/izualzhy/archive/2011/11/29/161187.html" href="http://www.cppblog.com/izualzhy/archive/2011/11/29/161187.html">http://www.cppblog.com/izualzhy/archive/2011/11/29/161187.html</a></p> <p>后来别人指点了下，于是修改了一番。</p> <p>主要是几个方面：</p> <p>1.界面先暂时去掉了，主要是算法本身增加了些判断。不过基本的想法没有改。</p> <p>2.现在可以计算任意的输入了，不过我试了下，超过6个数就非常慢。。。不知道怎么修改。。。</p> <p>3.写了一个新的类记录中间过程得到的数，重写了加减乘除等方法，这样就可以不像用之前ABS(a-b）&lt;1e-5的方法判断两个数相等。</p> <p>以四个数为例，记录下主要想法：</p> <p>首先输入的数假定为 A B C D</p> <p>排列共有24种情况，设一种为 a b c d</p> <p>在肯定为该排列方式的情况下，有3种加括号的方式,同时设@为加减乘除的任意一种，于是有</p> <p>(<a href="mailto:a@b">a@b</a>) c d</p> <p>a (<a href="mailto:b@c">b@c</a>) d</p> <p>a b (<a href="mailto:c@d">c@d</a>)</p> <p>共3x4=12种情况，于是之后得到三个数x y z,同样的加括号的方式有2种</p> <p>(<a href="mailto:x@y">x@y</a>) z</p> <p>x (<a href="mailto:y@z">y@z</a>)</p> <p>得到两个数m n,一种加括号的方式：</p> <p><a href="mailto:m@n">m@n</a></p> <p>于是得到一个数</p> <p>M</p> <p>检测这个M是否是目标数值。</p> <p>最近开始学习数据结构与算法，想尽快补上之前计算机系学的基本课程来。感觉这个有点像回溯算法。</p> <p>如果你又兴趣同时看完了代码的话，希望能告诉我一下哪里在复杂度需要改进的。</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #cc6633">#include</span> &lt;iostream&gt;<br><span style="color: #cc6633">#include</span> &lt;stdlib.h&gt;<br><span style="color: #cc6633">#include</span> &lt;time.h&gt;<br><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;<br><span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> cardCounter = 4;<br><span style="color: #0000ff">const</span> <span style="color: #0000ff">double</span> obj = 24;<br><span style="color: #008000">//#define DEBUG</span><br><br><span style="color: #0000ff">void</span> calTwoCards(<span style="color: #0000ff">double</span> a[], <span style="color: #0000ff">int</span> lenA, <span style="color: #0000ff">double</span> b[], <span style="color: #0000ff">int</span> lenB, <span style="color: #0000ff">double</span> src[lenA][lenB][4])<br>{<br>    <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=0; i&lt;lenA; ++i)<br>        <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> j=0; j&lt;lenB; ++j) {<br>            calTwoCards(a[i], b[j], src[i][j]);<br>        }<br>}<br><br><span style="color: #0000ff">void</span> calTwoCards(<span style="color: #0000ff">double</span> a, <span style="color: #0000ff">double</span> b, <span style="color: #0000ff">int</span> lenB, <span style="color: #0000ff">double</span> src[lenB][4])<br>{<br>    <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=0; i&lt;lenB; ++i)<br>        calTwoCards(a, b[i], src[i]);<br>}<br><br><span style="color: #0000ff">void</span> calTwoCards(<span style="color: #0000ff">double</span> a, <span style="color: #0000ff">double</span> b, <span style="color: #0000ff">double</span> src[])<br>{<br>    <span style="color: #008000">//a opeartor src[i] = b</span><br>    src[0]=b+a;<br>    src[1]=b-a;<br>    src[2]=b*a;<br>    src[3]=b/a;<br>}<br><br><span style="color: #0000ff">char</span> getOperator(<span style="color: #0000ff">int</span> i)<br>{<br>    <span style="color: #0000ff">if</span> (i==0) <span style="color: #0000ff">return</span> <span style="color: #006080">'+'</span>;<br>    <span style="color: #0000ff">if</span> (i==1) <span style="color: #0000ff">return</span> <span style="color: #006080">'-'</span>;<br>    <span style="color: #0000ff">if</span> (i==2) <span style="color: #0000ff">return</span> <span style="color: #006080">'*'</span>;<br>    <span style="color: #0000ff">if</span> (i==3) <span style="color: #0000ff">return</span> <span style="color: #006080">'/'</span>;<br>}<br><br><span style="color: #0000ff">void</span> parseM(<span style="color: #0000ff">int</span> m)<br>{<br>    <span style="color: #0000ff">char</span> c = getOperator(m%6);<br>    <span style="color: #0000ff">char</span> b = getOperator(m/6%6);<br>    <span style="color: #0000ff">char</span> a = getOperator(m/6/6%6);<br><br>    cout &lt;&lt; <span style="color: #006080">" "</span> &lt;&lt; a &lt;&lt; <span style="color: #006080">" "</span> &lt;&lt; b &lt;&lt; <span style="color: #006080">" "</span> &lt;&lt; c &lt;&lt; endl;<br>}<br><br><span style="color: #0000ff">void</span> copy(<span style="color: #0000ff">double</span> a[], <span style="color: #0000ff">double</span> b[], <span style="color: #0000ff">int</span> lenB)<br>{<br>    <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=0; i&lt;lenB; ++i)<br>        a[i] = b[i];<br>}<br><br><span style="color: #0000ff">void</span> calCorrentExpression(<span style="color: #0000ff">double</span> src[], <span style="color: #0000ff">double</span> obj)<br>{<br>    <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=0; i&lt;cardCounter; ++i) {<br>        <span style="color: #0000ff">double</span> srcFirst[cardCounter];<br>        calTwoCards(src[i], obj, srcFirst);<br>        <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> j=0; j&lt;cardCounter; ++j) {<br>            <span style="color: #0000ff">if</span> (j==i) <span style="color: #0000ff">continue</span>;<br>            <span style="color: #0000ff">double</span> srcSecond[cardCounter][cardCounter];<br>            calTwoCards(src[j], srcFirst, cardCounter, srcSecond);<br>            <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> p=0; p&lt;cardCounter; ++p) {<br>                <span style="color: #0000ff">if</span> (p==j || p==i) <span style="color: #0000ff">continue</span>;<br>                <span style="color: #0000ff">double</span> srcThird[cardCounter*cardCounter][cardCounter];<br>                calTwoCards(src[p], srcSecond, cardCounter*cardCounter, srcThird);<br>                <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> q=0; q&lt;cardCounter; ++q) {<br>                    <span style="color: #0000ff">if</span> (q==i || q==j || q==p) <span style="color: #0000ff">continue</span>;<br>                    <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> m=0; m&lt;cardCounter*cardCounter; ++m)<br>                        <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> n=0; n&lt;cardCounter; ++n)<br>                            <span style="color: #0000ff">if</span> (src[q]-srcThird[m][n])<br>                }<br>            }<br>        }<br>    }<br><br>}<br><br><span style="color: #0000ff">int</span> main()<br>{<br>    <span style="color: #0000ff">double</span> baseDigit[4] = {5,5,5,1};<br>    calCorrentExpression(baseDigit, 24);<br>    <span style="color: #0000ff">return</span> 0;<br>}<br></pre><br></div><img src ="http://www.cppblog.com/izualzhy/aggbug/162800.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-12-25 22:45 <a href="http://www.cppblog.com/izualzhy/archive/2011/12/25/162800.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>赫夫曼算法Huffman algorithm构造赫夫曼树</title><link>http://www.cppblog.com/izualzhy/archive/2011/12/20/162492.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Tue, 20 Dec 2011 14:29:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/12/20/162492.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/162492.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/12/20/162492.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/162492.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/162492.html</trackback:ping><description><![CDATA[<p>这篇笔记主要为了解决文件包含不同频率的字符时构造其赫夫曼编码的问题。 <p>具体可参考附录练习10.3 <p><strong>基本介绍：</strong> <p>贪心算法的一个应用，称为文件压缩file compression. <p>具体就不说了，主要是为了根据字符出现频率而采取不同长度的编码。同时保证解析后的结果是唯一的（只要没有任何字符编码是别的字符编码的前缀就行）。这样的编码称为前缀码prefix code。即要将所有字符放在叶子节点上。赫夫曼给出了一个算法，因此，这种编码系统通常称为赫夫曼编码Huffmancode。 <p>Huffman algorithm可以描述如下：假设字符的个数为C，维护一个由树组成的森林。一棵树的权等于它的叶子的频率的和。任一选取最小权的两棵树T1和T2，并任意形成以T1和T2为子树的新树，将这样的过程进行C-1次。在算法的开始，存在C棵单结点树—每个字符一棵。在算法结束时得到一棵树，这棵树就是最优赫夫曼编码树。新树的总的权正是那些老树的权的和。 <p><strong>具体代码如下</strong>：</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #008000">/********************************************************************************</span><br><span style="color: #008000"></span><br><span style="color: #008000">**</span><br><span style="color: #008000"></span><br><span style="color: #008000">** Filename: HuffmanCode.cpp</span><br><span style="color: #008000"></span><br><span style="color: #008000">**</span><br><span style="color: #008000"></span><br><span style="color: #008000">** Description: Huffman algorithm,generating prefix code.Note of learning greedy algorithm</span><br><span style="color: #008000"></span><br><span style="color: #008000">**</span><br><span style="color: #008000"></span><br><span style="color: #008000">** Version: 1.0</span><br><span style="color: #008000"></span><br><span style="color: #008000">** Created: 2011年12月16日 14时35分24秒</span><br><span style="color: #008000"></span><br><span style="color: #008000">** Revision: none</span><br><span style="color: #008000"></span><br><span style="color: #008000">** Compiler: gcc</span><br><span style="color: #008000"></span><br><span style="color: #008000">**</span><br><span style="color: #008000"></span><br><span style="color: #008000">** Author: zhy (),izualzhy@163.com </span><br><span style="color: #008000"></span><br><span style="color: #008000">*********************************************************************************/</span><br><br><span style="color: #cc6633">#include</span> &lt;iostream&gt;<br><br><span style="color: #cc6633">#include</span> &lt;string&gt;<br><br><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;<br><br><span style="color: #0000ff">struct</span> HuffmanNode {<br><br><span style="color: #0000ff">char</span> nodeChar;<br><br><span style="color: #0000ff">int</span> counter;<br><br>HuffmanNode *leftChild;<br><br>HuffmanNode *rightChild;<br><br>string code;<br><br>HuffmanNode(<span style="color: #0000ff">char</span> c=<span style="color: #006080">'?'</span>, <span style="color: #0000ff">int</span> t=0, HuffmanNode *l=NULL, HuffmanNode *r=NULL, string ce=string()) :<br><br>nodeChar(c) ,<br><br>counter(t) ,<br><br>leftChild(l) ,<br><br>rightChild(r) ,<br><br>code(ce)<br><br>{}<br><br><span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span>&lt;(<span style="color: #0000ff">const</span> HuffmanNode&amp; node) { <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>-&gt;counter&lt;node.counter; }<br><br>};<br><br><span style="color: #0000ff">template</span>&lt;<span style="color: #0000ff">typename</span> T&gt;<br><br><span style="color: #0000ff">class</span> PriorityQueue {<br><br><span style="color: #0000ff">public</span>:<br><br>PriorityQueue(T a[], <span style="color: #0000ff">int</span> size)<br><br>{<br><br>array = a;<br><br>currentSize = size;<br><br><span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=size/2; i&gt;=0; --i)<br><br>percolateDown(i);<br><br>}<br><br><span style="color: #0000ff">void</span> deleteMin(T &amp;minItem)<br><br>{<br><br>minItem = array[0];<br><br>array[0] = array[--currentSize];<br><br>percolateDown(0);<br><br>}<br><br><span style="color: #0000ff">void</span> insert(<span style="color: #0000ff">const</span> T &amp;item)<br><br>{<br><br>array[currentSize++] = item;<br><br><span style="color: #008000">//percolate Up</span><br><br>T temp = item;<br><br><span style="color: #0000ff">int</span> curhole = currentSize-1;<br><br><span style="color: #0000ff">while</span> (curhole&gt;0 &amp;&amp; temp&lt;array[(curhole-1)/2]) {<br><br>array[curhole] = array[(curhole-1)/2];<br><br>curhole = (curhole-1)/2;<br><br>}<br><br>array[curhole] = temp;<br><br>}<br><br><span style="color: #0000ff">private</span>:<br><br><span style="color: #0000ff">int</span> leftChild(<span style="color: #0000ff">int</span> i) { <span style="color: #0000ff">return</span> 2*i+1; }<br><br><span style="color: #0000ff">void</span> percolateDown(<span style="color: #0000ff">int</span> hole)<br><br>{<br><br><span style="color: #0000ff">int</span> child;<br><br>T temp = array[hole];<br><br><span style="color: #0000ff">while</span> (leftChild(hole)&lt;currentSize) {<br><br>child = leftChild(hole);<br><br><span style="color: #0000ff">if</span> (child!=currentSize-1 &amp;&amp; array[child+1]&lt;array[child])<br><br>child++;<br><br><span style="color: #0000ff">if</span> (array[child]&lt;temp)<br><br>array[hole] = array[child];<br><br><span style="color: #0000ff">else</span><br><br><span style="color: #0000ff">break</span>;<br><br>hole = child;<br><br>}<br><br>array[hole] = temp;<br><br>}<br><br><span style="color: #0000ff">void</span> percolateUp(<span style="color: #0000ff">int</span> hole)<br><br>{<br><br>T temp = array[hole];<br><br><span style="color: #0000ff">while</span> (hole&gt;0 &amp;&amp; temp&lt;array[(hole-1)/2]) {<br><br>array[hole] = array[(hole-1)/2];<br><br>hole = (hole-1)/2;<br><br>}<br><br>array[hole] = temp;<br><br>}<br><br><span style="color: #0000ff">public</span>:<br><br><span style="color: #0000ff">int</span> currentSize;<br><br>T *array;<br><br>};<br><br><span style="color: #0000ff">int</span> totalCodeNum;<br><br><span style="color: #0000ff">void</span> postorderTraverse(HuffmanNode *node,<span style="color: #0000ff">char</span> left,string base)<br><br>{<br><br><span style="color: #0000ff">if</span> (!node) <span style="color: #0000ff">return</span>;<br><br>base.append(1,left);<br><br>node-&gt;code = base;<br><br><span style="color: #0000ff">if</span> (node-&gt;leftChild) postorderTraverse(node-&gt;leftChild,<span style="color: #006080">'0'</span>,base);<br><br><span style="color: #0000ff">if</span> (node-&gt;rightChild) postorderTraverse(node-&gt;rightChild,<span style="color: #006080">'1'</span>,base);<br><br><span style="color: #0000ff">if</span> (node-&gt;nodeChar!=<span style="color: #006080">'?'</span>) cout &lt;&lt; node-&gt;nodeChar &lt;&lt; <span style="color: #006080">"\t"</span> &lt;&lt; node-&gt;code &lt;&lt; <span style="color: #006080">"\t"</span> &lt;&lt; (base.size()-1)*node-&gt;counter &lt;&lt; endl;<br><br>totalCodeNum+=(base.size()-1)*node-&gt;counter;<br><br>}<br><br><span style="color: #0000ff">int</span> main()<br><br>{<br><br>HuffmanNode node[14];<br><br>node[0].nodeChar = <span style="color: #006080">':'</span>;node[0].counter = 100;<br><br>node[1].nodeChar = <span style="color: #006080">' '</span>;node[1].counter = 605;<br><br>node[2].nodeChar = <span style="color: #006080">'/'</span>;node[2].counter = 100;<br><br>node[3].nodeChar = <span style="color: #006080">','</span>;node[3].counter = 705;<br><br>node[4].nodeChar = <span style="color: #006080">'0'</span>;node[4].counter = 431;<br><br>node[5].nodeChar = <span style="color: #006080">'1'</span>;node[5].counter = 242;<br><br>node[6].nodeChar = <span style="color: #006080">'2'</span>;node[6].counter = 176;<br><br>node[7].nodeChar = <span style="color: #006080">'3'</span>;node[7].counter = 59;<br><br>node[8].nodeChar = <span style="color: #006080">'4'</span>;node[8].counter = 185;<br><br>node[9].nodeChar = <span style="color: #006080">'5'</span>;node[9].counter = 250;<br><br>node[10].nodeChar = <span style="color: #006080">'6'</span>;node[10].counter = 174;<br><br>node[11].nodeChar = <span style="color: #006080">'7'</span>;node[11].counter = 199;<br><br>node[12].nodeChar = <span style="color: #006080">'8'</span>;node[12].counter = 205;<br><br>node[13].nodeChar = <span style="color: #006080">'9'</span>;node[13].counter = 217;<br><br>PriorityQueue&lt;HuffmanNode&gt; queue(node,14);<br><br>HuffmanNode *temp1,*temp2,*t;<br><br><span style="color: #0000ff">while</span> (queue.currentSize&gt;1) {<br><br>temp1 = <span style="color: #0000ff">new</span> HuffmanNode;<br><br>temp2 = <span style="color: #0000ff">new</span> HuffmanNode;<br><br>queue.deleteMin(*temp1);<br><br>queue.deleteMin(*temp2);<br><br>t = <span style="color: #0000ff">new</span> HuffmanNode(<span style="color: #006080">'?'</span>,temp1-&gt;counter+temp2-&gt;counter,temp1,temp2);<br><br>cout &lt;&lt; t-&gt;counter &lt;&lt; endl;<br><br>queue.insert(*t);<br><br>}<br><br>postorderTraverse(t,0,<span style="color: #006080">""</span>);<br><br>cout &lt;&lt; <span style="color: #006080">"total: "</span> &lt;&lt; totalCodeNum &lt;&lt; endl;<br><br><span style="color: #0000ff">return</span> 0;<br><br>}<br><br></pre><br></div>
<p><strong>问题：</strong>
<p>还有一个问题就是感觉空间的问题，优先队列里存储的是对象本身，感觉存储指针会节省不少空间。因为最初的基本思路是，构造树的结点都重新new出来，如果跟优先队列里使用相同的结点应该会好很多。
<p>附录：
<p>数据结构与算法分析—C++描述
<img src ="http://www.cppblog.com/izualzhy/aggbug/162492.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-12-20 22:29 <a href="http://www.cppblog.com/izualzhy/archive/2011/12/20/162492.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>选择问题---堆的应用</title><link>http://www.cppblog.com/izualzhy/archive/2011/12/12/161990.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Mon, 12 Dec 2011 14:09:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/12/12/161990.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/161990.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/12/12/161990.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/161990.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/161990.html</trackback:ping><description><![CDATA[<p>设有一组N个数，要确定其中第k个最大者。这称为选择问题（selection problem）。 <p>题目很简单，解决这个问题主要是为了加深对二叉堆应用的印象。 <p>解法： <p>1A：将这N个数读进一个数组中，再通过某种简单的算法，比如冒泡排序法，已递减的顺序将数组排序，然后返回位置k上的元素。 <p>1B：先把前k个元素读入数组并（以递减的顺序）对其排序。接着，将剩下的元素读入。当读到一个新元素时，如果该元素小于数组中的第k各元素则忽略之，否则将其放到数组中的正确位置上，同时将数组中的一个元素挤出数组。当算法结束时，返回第k个位置上的元素作为答案。 <p>6A: 假设我们只考虑找出k个最小元素。该算法很简单。先将N个元素读入一个数组，然后对数组应用buildHeap算法。最后，执行k次deleteMin操作。最后从该堆提取的元素就是正确答案。显然，只要改变堆序性质，就可以求解原始问题：找出第k个最大的元素。 <p>6B：跟1B相似，不同的是数组替换为堆。 <p>1A如果使用的是简单的排序算法，则运行时间为O(N<sup>2</sup>). <p>1B为O(N*k),如果k=N/2，那么还是O(N<sup>2</sup>). <p>6A如果使用buildHeap，构造堆的最坏时间为O(N)时间而每次执行deleteMin是O(logN)时间。由于有k次deleteMin，因此我们得到总运行时间为O(N+klogN)。如果k=O(N/logN)，那么运行时间取决于buildHeap操作，即O(N)。对于大的k值，运行时间为O(klogN)。如果k=N/2，那么运行时间为O(NlogN)。 <p>注意如果令k=N，并在元素离开堆时记录这些元素的值，那么实际上就是对输入文件以时间O(NlogN)排序，即为堆排序。 <p>6B buildHeap以总时间O(k)放入堆中。处理其余的每个元素的时间为O(1)，还要加上O(logk)时间，用来检测元素是否放入S，并在需要时删除sk并插入新元素。因此，总的时间是O(k+(N-k)logk) = O(Nlogk)。 <p>还有其他一些更好的方法，不过我没有写。 <p>看下输出： <p>$ ./a.out 5 &amp;&amp; ./a.out 4 &amp;&amp; ./a.out 3 &amp;&amp; ./a.out 2 &amp;&amp; ./a.out 1 <p>method6B: <p>using time: 0.261759 <p>4987030 <p>method6A: <p>using time: 0.419211 <p>4987030 <p>method1B: <p>using time: 2276.7 <p>4987030 <p>method1A2(insert sort): <p>using time: 3327.25 <p>4987030 <p>method1A1(bubble sort): <p>using time: 9631.47 <p>4987030 <p>相关代码：</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #cc6633">#include</span> &lt;iostream&gt;<br><br><span style="color: #cc6633">#include</span> &lt;fstream&gt;<br><br><span style="color: #cc6633">#include</span> &lt;vector&gt;<br><br><span style="color: #cc6633">#include</span> &lt;sys/time.h&gt;<br><br><span style="color: #cc6633">#include</span> <span style="color: #006080">"BinaryHeap.h"</span><br><br><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;<br><br><span style="color: #0000ff">typedef</span> <span style="color: #0000ff">unsigned</span> <span style="color: #0000ff">int</span> uint;<br><br><span style="color: #0000ff">struct</span> timeval startTime;<br><br><span style="color: #0000ff">struct</span> timeval endTime;<br><br><span style="color: #0000ff">double</span> usedTime;<br><br><span style="color: #0000ff">void</span> swap(uint &amp;a, uint &amp;b)<br><br>{<br><br>uint temp = a;<br><br>a = b;<br><br>b = temp;<br><br>}<br><br><span style="color: #0000ff">void</span> method1A1(vector&lt;uint&gt; &amp;vec, <span style="color: #0000ff">int</span> k)<br><br>{<br><br>gettimeofday(&amp;startTime,NULL);<br><br>size_t len = vec.size();<br><br><span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=0; i&lt;len; ++i)<br><br><span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> j=len-1; j&gt;i; --j) {<br><br><span style="color: #0000ff">if</span> (vec[j]&lt;vec[j-1])<br><br>swap(vec[j], vec[j-1]);<br><br>}<br><br>gettimeofday(&amp;endTime,NULL);<br><br>usedTime = (endTime.tv_sec-startTime.tv_sec)+(endTime.tv_usec-startTime.tv_usec)*1e-6;<br><br>cout &lt;&lt; <span style="color: #006080">"using time: "</span> &lt;&lt; usedTime &lt;&lt; endl;<br><br>cout &lt;&lt; vec[k] &lt;&lt; endl;<span style="color: #008000">//第k+1个最小的,即1000000-k个最大的</span><br><br>}<br><br><span style="color: #0000ff">void</span> method1A2(vector&lt;uint&gt; &amp;vec, <span style="color: #0000ff">int</span> k)<br><br>{<br><br>gettimeofday(&amp;startTime,NULL);<br><br>size_t len = vec.size();<br><br><span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=1; i&lt;len; ++i) {<br><br>uint temp = vec[i];<br><br><span style="color: #0000ff">int</span> j = i;<br><br><span style="color: #0000ff">while</span> (j&gt;0 &amp;&amp; vec[j-1]&gt;temp) {<br><br>vec[j] = vec[--j];<br><br>}<br><br>vec[j] = temp;<br><br>}<br><br>gettimeofday(&amp;endTime,NULL);<br><br>usedTime = (endTime.tv_sec-startTime.tv_sec)+(endTime.tv_usec-startTime.tv_usec)*1e-6;<br><br>cout &lt;&lt; <span style="color: #006080">"using time: "</span> &lt;&lt; usedTime &lt;&lt; endl;<br><br>cout &lt;&lt; vec[k] &lt;&lt; endl;<span style="color: #008000">//第k+1个最小的,即1000000-k个最大的</span><br><br>}<br><br><span style="color: #0000ff">void</span> method1B(vector&lt;uint&gt; &amp;vec, <span style="color: #0000ff">int</span> k)<br><br>{<br><br>gettimeofday(&amp;startTime,NULL);<br><br>size_t len = vec.size();<br><br><span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=1; i&lt;=k; ++i) {<br><br>uint temp = vec[i];<br><br><span style="color: #0000ff">int</span> j = i;<br><br><span style="color: #0000ff">while</span> (j&gt;0 &amp;&amp; vec[j-1]&gt;temp) {<br><br>vec[j] = vec[--j];<br><br>}<br><br>vec[j] = temp;<br><br>}<br><br><span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=k+1; i&lt;len; ++i) {<br><br><span style="color: #0000ff">if</span> (vec[i]&lt;vec[k]) {<br><br><span style="color: #0000ff">int</span> j = k;<br><br><span style="color: #0000ff">while</span> (j&gt;0 &amp;&amp; vec[j-1]&gt;vec[i]) {<br><br>vec[j] = vec[--j];<br><br>}<br><br>vec[j] = vec[i];<br><br>}<br><br>}<br><br>gettimeofday(&amp;endTime,NULL);<br><br>usedTime = (endTime.tv_sec-startTime.tv_sec)+(endTime.tv_usec-startTime.tv_usec)*1e-6;<br><br>cout &lt;&lt; <span style="color: #006080">"using time: "</span> &lt;&lt; usedTime &lt;&lt; endl;<br><br>cout &lt;&lt; vec[k] &lt;&lt; endl;<span style="color: #008000">//第k+1个最小的,即1000000-k个最大的</span><br><br>}<br><br><span style="color: #0000ff">void</span> method6A(vector&lt;uint&gt; &amp;vec, <span style="color: #0000ff">int</span> k)<br><br>{<br><br>gettimeofday(&amp;startTime,NULL);<br><br>BinaryHeap&lt;uint&gt; bh(vec);<br><br><span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=0; i&lt;k; ++i)<br><br>bh.deleteMin();<span style="color: #008000">//删除k次，每次删除的都是最小值</span><br><br>gettimeofday(&amp;endTime,NULL);<br><br>usedTime = (endTime.tv_sec-startTime.tv_sec)+(endTime.tv_usec-startTime.tv_usec)*1e-6;<br><br>cout &lt;&lt; <span style="color: #006080">"using time: "</span> &lt;&lt; usedTime &lt;&lt; endl;<br><br>cout &lt;&lt; bh.findMin() &lt;&lt; endl;<span style="color: #008000">//之前删除的k个都比改数值小，剩下的数值都比改数值大，因此这是第k+1个最小的.</span><br><br>}<br><br><span style="color: #0000ff">void</span> method6B(vector&lt;uint&gt; &amp;vec, <span style="color: #0000ff">int</span> k)<br><br>{<br><br>gettimeofday(&amp;startTime,NULL);<br><br>BinaryHeap&lt;uint&gt; bh(vector&lt;uint&gt;(vec.begin(),vec.begin()+k));<span style="color: #008000">//读取前k个元素构建堆</span><br><br>size_t len = vec.size();<br><br><span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> j=k; j&lt;len-1; ++j) {<span style="color: #008000">//从第k+1个元素开始再次读取，与堆比较</span><br><br><span style="color: #0000ff">if</span> (vec[j]&gt;bh.findMin()) {<span style="color: #008000">//如果比最小值大，则加入</span><br><br>bh.deleteMin();<br><br>bh.insert(vec[j]);<br><br>}<br><br>}<span style="color: #008000">//此时得到的实际上是第k个最大值</span><br><br>gettimeofday(&amp;endTime,NULL);<br><br>usedTime = (endTime.tv_sec-startTime.tv_sec)+(endTime.tv_usec-startTime.tv_usec)*1e-6;<br><br>cout &lt;&lt; <span style="color: #006080">"using time: "</span> &lt;&lt; usedTime &lt;&lt; endl;<br><br>cout &lt;&lt; bh.findMin() &lt;&lt; endl;<br><br>}<br><br><span style="color: #0000ff">int</span> main(<span style="color: #0000ff">int</span> argc, <span style="color: #0000ff">char</span> *argv[])<br><br>{<br><br><span style="color: #0000ff">if</span> (argc==1) {<br><br>cout &lt;&lt; <span style="color: #006080">"Usage:$exe argument\n"</span><br><br>&lt;&lt; <span style="color: #006080">"argument: 1.method1A1.\n"</span><br><br>&lt;&lt; <span style="color: #006080">"argument: 2.method1A2.\n"</span><br><br>&lt;&lt; <span style="color: #006080">"argument: 3.Use BinaryHeap.\n"</span>;<br><br><span style="color: #0000ff">return</span> 1;<br><br>}<br><br>vector&lt;uint&gt; vec;<br><br><span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> k = 500000;<br><br>fstream inFile;<br><br><span style="color: #0000ff">int</span> data;<br><br>inFile.open(<span style="color: #006080">"data.txt"</span>);<br><br><span style="color: #0000ff">while</span> (inFile) {<br><br>inFile &gt;&gt; data;<br><br>vec.push_back(data);<br><br>}<span style="color: #008000">//data.txt数据共1000000个</span><br><br><span style="color: #0000ff">double</span> usedTime;<br><br><span style="color: #0000ff">switch</span>(argv[1][0]) {<br><br><span style="color: #0000ff">case</span> <span style="color: #006080">'1'</span>:<br><br>cout &lt;&lt; <span style="color: #006080">"method1A1(bubble sort):"</span> &lt;&lt; endl;<br><br>method1A1(vec,k);<br><br><span style="color: #0000ff">break</span>;<br><br><span style="color: #0000ff">case</span> <span style="color: #006080">'2'</span>:<br><br>cout &lt;&lt; <span style="color: #006080">"method1A2(insert sort):"</span> &lt;&lt; endl;<br><br>method1A2(vec,k);<br><br><span style="color: #0000ff">break</span>;<br><br><span style="color: #0000ff">case</span> <span style="color: #006080">'3'</span>:<br><br>cout &lt;&lt; <span style="color: #006080">"method1B:"</span> &lt;&lt; endl;<br><br>method1B(vec,k);<br><br><span style="color: #0000ff">break</span>;<br><br><span style="color: #0000ff">case</span> <span style="color: #006080">'4'</span>:<br><br>cout &lt;&lt; <span style="color: #006080">"method6A:"</span> &lt;&lt; endl;<br><br>method6A(vec,k);<br><br><span style="color: #0000ff">break</span>;<br><br><span style="color: #0000ff">case</span> <span style="color: #006080">'5'</span>:<br><br>cout &lt;&lt; <span style="color: #006080">"method6B:"</span> &lt;&lt; endl;<br><br>method6B(vec,k);<br><br><span style="color: #0000ff">break</span>;<br><br>}<br><br><span style="color: #0000ff">return</span> 0;<br><br>}<br><br></pre><br></div>
<p>参考资料：
<p>数据结构与算法分析—c++描述
<img src ="http://www.cppblog.com/izualzhy/aggbug/161990.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-12-12 22:09 <a href="http://www.cppblog.com/izualzhy/archive/2011/12/12/161990.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>不相交集类以及应用迷宫生成</title><link>http://www.cppblog.com/izualzhy/archive/2011/12/11/161913.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Sun, 11 Dec 2011 02:23:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/12/11/161913.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/161913.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/12/11/161913.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/161913.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/161913.html</trackback:ping><description><![CDATA[<p><b>简单介绍：</b> <p>考虑一个迷宫的生成，一个简单算法就是从各处的墙壁开始（除入口和出口之外）。此时，不断地随机选择一面墙，如果被该墙分割的单元彼此不联通，那么就把这面墙拆掉。重复这个过程直到开始单元和终止单元联通，那么就得到一个迷宫。实际上不断的拆掉墙壁直到每个单元都可以从其他单元到达更好（这会使迷宫产生更多误导的路径）。 <p>因此主要是两个问题： <p>1. 拆掉墙如果两个单元不连通。 <p>2. 保留墙如果两个单元已经联通。 <p>这里用于判断不同单元是否在同一集合的数据结构即为不相交集类。 <p><b>基本知识：</b> <p>即我们要建立多个等价类，联通的元素放在同一个等价类里面。 <p>对应的，需要两个操作： <p>1. 将两个元素放到一个等价类里的话，采用union操作。 <p>2. 查找某个元素在哪个等价类里，采用find操作。根据find的返回值可以判断两个元素是否在一个等价类里。 <p>可以用数组来记录，程序里我使用的是vector（仿照书里的）。 <p>举个例子看可能比较清楚： <p>假设元素ID刚开始为 0 1 2 3 4 5 6 7 8 9 <p>建立相同大小的数组s，数组初始化值均为-1。 <p>最直观的处理，比如union(4,5)即使s[5] = 4。（约定union(x,y)使得s[y]=x，效果是相同的）。 <p>find(4)会直接返回4本身，因为s[4]&lt;0。 <p>而find(5)则递归调用，实际上为:find(5) = find(s[5]) = 4，因此判断出4，5属于同一个等价类（我觉得可以理解为4这个结点即为等价类的代表）。 <p>find最坏情形运行时间为O（N），因为对N个元素，有可能建立一个深度为N-1的树。 <p>因此对union和find操作都进行了改进。 <p>为了控制树的深度，可以重写union的方式，比如unionBySize,unionByHeight，书上都介绍，这里就不说了。此时find运行时间为O(N)。 <p>另外对于find巧妙的改进是路径压缩，即当find（x）调用时，修改递归找到的结点直接指向等价类的代表结点，使得路径上的结点移近根结点，这样下次调用时就很快了。 <p>正如书上所说： <p>这种数据结构实现起来很简单，每个例程只需要几行代码，而且可以使用一个简单的数组。 <p><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/WindowsLiveWriter/f2b1f2014bbe_9250/clip_image002_2.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://www.cppblog.com/images/cppblog_com/izualzhy/WindowsLiveWriter/f2b1f2014bbe_9250/clip_image002_thumb.jpg" width="540" height="553"></a></p> <p><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/WindowsLiveWriter/f2b1f2014bbe_9250/clip_image004_2.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://www.cppblog.com/images/cppblog_com/izualzhy/WindowsLiveWriter/f2b1f2014bbe_9250/clip_image004_thumb.jpg" width="564" height="559"></a></p> <p>可以看到迷宫差别很大，我只是使得入口出口两个单元连通起来即结束循环。 <p>效率什么的没有考虑。 <p>所有的代码：数据集合类，迷宫生成类</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #008000">/********************************************************************************</span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **       Filename:  DisjSets.h</span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **    Description:  </span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **        Version:  1.0</span><br><span style="color: #008000"> **        Created:  2011年12月06日 11时43分46秒</span><br><span style="color: #008000"> **       Revision:  none</span><br><span style="color: #008000"> **       Compiler:  gcc</span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **         Author:  zhy (), izualzhy@163.com</span><br><span style="color: #008000"> **        Company:  </span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> *********************************************************************************/</span><br><span style="color: #cc6633">#ifndef</span> DISJSETS_H<br><span style="color: #cc6633">#define</span> DISJSETS_H<br><span style="color: #cc6633">#include</span> &lt;vector&gt;<br><span style="color: #cc6633">#include</span> &lt;iostream&gt;<br><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;<br><br><span style="color: #0000ff">class</span> DisjSets {<br><br><span style="color: #0000ff">public</span>:<br>    <span style="color: #0000ff">explicit</span> DisjSets(<span style="color: #0000ff">int</span> numElements);<br><br>    <span style="color: #0000ff">int</span> find(<span style="color: #0000ff">int</span> x);<br>    <span style="color: #0000ff">void</span> unionSets(<span style="color: #0000ff">int</span> root1, <span style="color: #0000ff">int</span> root2);<br><br><span style="color: #0000ff">private</span>:<br>    vector&lt;<span style="color: #0000ff">int</span>&gt; s;<br>};<br>#endif<span style="color: #008000">//DISJSETS_H</span><br></pre><br></div>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #008000">/********************************************************************************</span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **       Filename:  DisjSets.cpp</span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **    Description:  </span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **        Version:  1.0</span><br><span style="color: #008000"> **        Created:  2011年12月06日 11时45分09秒</span><br><span style="color: #008000"> **       Revision:  none</span><br><span style="color: #008000"> **       Compiler:  gcc</span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **         Author:  zhy (), izualzhy@163.com</span><br><span style="color: #008000"> **        Company:  </span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> *********************************************************************************/</span><br><span style="color: #cc6633">#include</span> <span style="color: #006080">"DisjSets.h"</span><br><br>DisjSets::DisjSets(<span style="color: #0000ff">int</span> numElements) : s(numElements)<br>{<br>    <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=0; i&lt;s.size(); ++i)<br>        s[i] = -1;<br>}<br><br><span style="color: #0000ff">int</span> DisjSets::find(<span style="color: #0000ff">int</span> x)<br>{<br>    <span style="color: #008000">//cout &lt;&lt; "DisjSets::find x= " &lt;&lt; x &lt;&lt; "\ts[x] = " &lt;&lt; s[x] &lt;&lt; endl;</span><br>    <span style="color: #0000ff">if</span> (s[x]&lt;0)<br>        <span style="color: #0000ff">return</span> x;<br>    <span style="color: #0000ff">else</span> {<br>        s[x] = find(s[x]);<br>        <span style="color: #0000ff">return</span> s[x];<br>    }<br>}<br><br><span style="color: #0000ff">void</span> DisjSets::unionSets(<span style="color: #0000ff">int</span> root1, <span style="color: #0000ff">int</span> root2)<br>{<br>    <span style="color: #0000ff">if</span> (s[root1]&gt;s[root2]) {<br>        s[root1] = root2;<br>    } <span style="color: #0000ff">else</span> {<br>        <span style="color: #0000ff">if</span> (s[root1]==s[root2])<br>            s[root1]--;<br>        s[root2] = root1;<br>    }<br>}<br></pre><br></div>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #008000">/********************************************************************************</span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **       Filename:  MazeDataGenerator.h</span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **    Description:  generator the data of maze</span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **        Version:  1.0</span><br><span style="color: #008000"> **        Created:  2011年12月06日 14时49分09秒</span><br><span style="color: #008000"> **       Revision:  none</span><br><span style="color: #008000"> **       Compiler:  gcc</span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **         Author:  zhy (), izualzhy@163.com</span><br><span style="color: #008000"> **        Company:  </span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> *********************************************************************************/</span><br><span style="color: #cc6633">#ifndef</span> MAZEDATAGENERATOR_H<br><span style="color: #cc6633">#define</span> MAZEDATAGENERATOR_H<br><span style="color: #cc6633">#include</span> &lt;vector&gt;<br><span style="color: #cc6633">#include</span> <span style="color: #006080">"DisjSets.h"</span><br><span style="color: #0000ff">class</span> Room{<br><br><span style="color: #0000ff">public</span>:<br>    Room(<span style="color: #0000ff">int</span> row, <span style="color: #0000ff">int</span> col) :<br>        i(row) ,<br>        j(col)<br>    {<br>        <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=0; i&lt;4; ++i)<br>            wall[i] = true;<br>    }<br>    <span style="color: #0000ff">int</span> i;<br>    <span style="color: #0000ff">int</span> j;<br>    <span style="color: #0000ff">bool</span> wall[4];<br><br>    <span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span>==(<span style="color: #0000ff">const</span> Room &amp;room2)<br>    {<br>        <span style="color: #0000ff">return</span> ((<span style="color: #0000ff">this</span>-&gt;i)==(room2.i) &amp;&amp; (<span style="color: #0000ff">this</span>-&gt;j==room2.j));<br>    }<br>};<br><br><span style="color: #0000ff">class</span> MazeDataGenerator {<br><br><span style="color: #0000ff">public</span>:<br>    <span style="color: #0000ff">enum</span> Direction{<br>        NORTH,<br>        EAST,<br>        SOUTH,<br>        WEST<br>    };<br><br>    MazeDataGenerator(<span style="color: #0000ff">int</span> row, <span style="color: #0000ff">int</span> column);<br>    ~MazeDataGenerator()<br>    {<br>        <span style="color: #0000ff">delete</span> mData;<br>        mData = NULL;<br>        <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=0; i&lt;mRows; ++i)<br>            <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> j=0; j&lt;mColumns; ++j)<br>                <span style="color: #0000ff">delete</span> mRoom[i][j];<br>        <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=0; i&lt;mRows; ++i)<br>            <span style="color: #0000ff">delete</span> [] mRoom[i];<br>        <span style="color: #0000ff">delete</span> [] mRoom;<br>    }<br><br>    <span style="color: #0000ff">void</span> print();<br><br><span style="color: #0000ff">private</span>:<br>    <span style="color: #0000ff">bool</span> isConnected(Room *room1, Room *room2) {<br>        <span style="color: #0000ff">return</span> (mData-&gt;find(room1-&gt;i*mColumns+room1-&gt;j)==mData-&gt;find(room2-&gt;i*mColumns+room2-&gt;j));<br>    }<br>    <span style="color: #0000ff">void</span> initRoad();<br>    <span style="color: #0000ff">bool</span> connectTworoom(Room *room1, Room *room2, Direction d);<br><br><span style="color: #0000ff">private</span>:<br>    DisjSets *mData;<br>    Room ***mRoom;<br>    <span style="color: #0000ff">int</span> mColumns;<br>    <span style="color: #0000ff">int</span> mRows;<br>};<br>#endif<span style="color: #008000">//MAZEDATAGENERATOR_H</span><br></pre><br></div>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #008000">/********************************************************************************</span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **       Filename:  MazeDataGenerator.cpp</span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **    Description:  </span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **        Version:  1.0</span><br><span style="color: #008000"> **        Created:  2011年12月06日 14时51分42秒</span><br><span style="color: #008000"> **       Revision:  none</span><br><span style="color: #008000"> **       Compiler:  gcc</span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> **         Author:  zhy (), izualzhy@163.com</span><br><span style="color: #008000"> **        Company:  </span><br><span style="color: #008000"> **</span><br><span style="color: #008000"> *********************************************************************************/</span><br><span style="color: #cc6633">#include</span> &lt;stdlib.h&gt;<br><span style="color: #cc6633">#include</span> &lt;time.h&gt;<br><span style="color: #cc6633">#include</span> &lt;stdio.h&gt;<br><span style="color: #cc6633">#include</span> <span style="color: #006080">"MazeDataGenerator.h"</span><br><br>MazeDataGenerator::MazeDataGenerator(<span style="color: #0000ff">int</span> row, <span style="color: #0000ff">int</span> column) :<br>    mRows(row) ,<br>    mColumns(column) <br>{<br>    srand(time(NULL));<br>    mData = <span style="color: #0000ff">new</span> DisjSets(row*column);<br>    mRoom = <span style="color: #0000ff">new</span> Room**[row];<br>    <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=0; i&lt;row; ++i)<br>        mRoom[i] = <span style="color: #0000ff">new</span> Room*[column];<br>    <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=0; i&lt;row; ++i)<br>        <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> j=0; j&lt;column; ++j)<br>            mRoom[i][j] = <span style="color: #0000ff">new</span> Room(i,j);<br>    mRoom[0][0]-&gt;wall[NORTH] = mRoom[0][0]-&gt;wall[EAST] = false;<br>    mRoom[mRows-1][mColumns-1]-&gt;wall[SOUTH] = mRoom[0][0]-&gt;wall[WEST] = false;<br>    initRoad();<br>}<br><br><span style="color: #0000ff">void</span> MazeDataGenerator::initRoad()<br>{<br>    printf(<span style="color: #006080">"%s\n"</span>,__FUNCTION__);<br>    Room *room = mRoom[0][0];<br>    <span style="color: #0000ff">while</span> (!isConnected(mRoom[0][0],mRoom[mRows-1][mColumns-1])) {<br>        Direction side = (Direction)(rand()%4);<br>        <span style="color: #0000ff">int</span> i = room-&gt;i;<br>        <span style="color: #0000ff">int</span> j = room-&gt;j;<br>        <span style="color: #0000ff">switch</span>(side) {<br>            <span style="color: #0000ff">case</span> NORTH:<br>                i = room-&gt;i-1;<br>                <span style="color: #0000ff">break</span>;<br>            <span style="color: #0000ff">case</span> SOUTH:<br>                i = room-&gt;i+1;<br>                <span style="color: #0000ff">break</span>;<br>            <span style="color: #0000ff">case</span> EAST:<br>                j = room-&gt;j-1;<br>                <span style="color: #0000ff">break</span>;<br>            <span style="color: #0000ff">case</span> WEST:<br>                j = room-&gt;j+1;<br>                <span style="color: #0000ff">break</span>;<br>        }<br><br>        <span style="color: #0000ff">if</span> (i&lt;0 || i&gt;=mRows || j&lt;0 || j&gt;=mColumns)<br>            <span style="color: #0000ff">continue</span>;<br>        <span style="color: #0000ff">if</span> (!isConnected(mRoom[room-&gt;i][room-&gt;j],mRoom[i][j]))<br>            connectTworoom(mRoom[room-&gt;i][room-&gt;j],mRoom[i][j],side);<br>        room = mRoom[i][j];<br>        <span style="color: #008000">//print();</span><br>    }<br>}<br><br><span style="color: #0000ff">bool</span> MazeDataGenerator::connectTworoom(Room *room1, Room *room2, Direction d)<br>{<br>    <span style="color: #008000">//printf("%s\n",__FUNCTION__);</span><br>    <span style="color: #008000">//printf("(%d,%d)--&gt;(%d,%d)\n",room1.i,room1.j,room2.i,room2.j);</span><br>    <span style="color: #0000ff">if</span> (room1==room2)<br>        <span style="color: #0000ff">return</span> false;<br><br>    <span style="color: #0000ff">int</span> index1 = room1-&gt;i*mColumns+room1-&gt;j;<br>    <span style="color: #0000ff">int</span> index2 = room2-&gt;i*mColumns+room2-&gt;j;<br>    <span style="color: #0000ff">int</span> root1 = mData-&gt;find(index1);<br>    <span style="color: #0000ff">int</span> root2 = mData-&gt;find(index2);<br><br>    <span style="color: #0000ff">if</span> (root1==root2) <span style="color: #0000ff">return</span> false;<br>    mData-&gt;unionSets(root1,root2);<br>    room1-&gt;wall[d] = false;<br>    room2-&gt;wall[(d+2)%4] = false;<br>    <span style="color: #0000ff">return</span> true;<br>}<br><br><span style="color: #0000ff">void</span> MazeDataGenerator::print()<br>{<br>    printf(<span style="color: #006080">"%s\n"</span>,__FUNCTION__);<br>    <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=0; i&lt;mRows; ++i) {<br>        <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> j=0; j&lt;mColumns; ++j)<br>            printf(<span style="color: #006080">"%2d "</span>,mData-&gt;find(mRows*i+j));<br>        printf(<span style="color: #006080">"\n"</span>);<br>    }<br>   <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=0; j&lt;mColumns; ++j)<br>        printf(<span style="color: #006080">"__"</span>);<br>    printf(<span style="color: #006080">"\n"</span>);<br>    <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=0; i&lt;mRows; ++i) {<br>        <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> j=0; j&lt;mColumns; ++j) {<br>            <span style="color: #0000ff">char</span> s[3] = <span style="color: #006080">"  "</span>;<br>            <span style="color: #0000ff">if</span> (((j-1&lt;0) &amp;&amp; i!=0) || mRoom[i][j]-&gt;wall[1]) s[0]=<span style="color: #006080">'|'</span>;<br>            <span style="color: #0000ff">if</span> ((i+1)&gt;=mRows || mRoom[i][j]-&gt;wall[2]) s[1]=<span style="color: #006080">'_'</span>;<br>            printf(<span style="color: #006080">"%s"</span>,s);<br>            <span style="color: #0000ff">if</span> ((j+1)==mColumns &amp;&amp; i!=mRows-1) printf(<span style="color: #006080">"|"</span>);<br>        }<br>        printf(<span style="color: #006080">"\n"</span>);<br>    }<br>}<br></pre><br></div>
<p>参考资料：
<p>数据结构与算法分析—C++描述</p><img src ="http://www.cppblog.com/izualzhy/aggbug/161913.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-12-11 10:23 <a href="http://www.cppblog.com/izualzhy/archive/2011/12/11/161913.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>优先队列--二叉堆</title><link>http://www.cppblog.com/izualzhy/archive/2011/12/02/161332.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Fri, 02 Dec 2011 14:51:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/12/02/161332.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/161332.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/12/02/161332.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/161332.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/161332.html</trackback:ping><description><![CDATA[<p>关于二叉堆的学习笔记，如果改进或不对的地方请指出。 <p>之前学习线程的时候，我们直到线程有解锁队列，条件队列等。这些队列并不完全按照先进先出来执行的，如果某个线程优先级较高，可能就会优先执行。这种特殊的应用需要的特殊队列，称之为优先队列（priority queue） <p><b>模型：</b> <p>至少允许下列两种操作： <p>insert(插入)，deleteMin删除最小项（比如优先级最高的线程优先级值最小）。 <p>insert操作等价于enqueue（入队）。 <p>deleteMin则是队列操作dequeue（出队）在优先队列中的等价操作。 <p><b>一些简单的实现：</b> <p>链表？插入为O（1），但删除最小值需要遍历,为O(N).如果始终让表有序，则插入花费O（N），而删除花费O（1）。 <p>二叉查找树？两种操作均为O（logN）。同时二叉查找树支持大量并不需要的操作。 <p><b>二叉堆(binary heap)</b><b>：</b> <p>二叉堆是一棵完全填满的二叉树，可能的例外是在底层，底层上的元素从左到右插入。这样的树称为完全二叉树(complete binary tree)。 <p>完全二叉树完全可以用一个数组表示，而不需要使用链。 <p>根节点数组下标为1，由上往下，由左到右，依次递增。 <p>对于数组任一位置i上的元素，其左儿子在位置2i上，其右儿子在左儿子后的单元2i+1上，它的父亲在位置i/2上(取整)。 <p>这种实现方法唯一的问题是：堆的大小是固定的，不过一般情况下这不成问题，需要时我们可以重新调整。 <p><b>性质：</b> <p>跟之前的AVL树一样，插入，删除都可能破坏其性质，因此需要重新调整堆到满足其性质。 <p>使操作可以快速执行的性质是堆序性质(heap-order property)。 <p>我们是想快速找到最小元，因此将其放在根上。如果考虑到任意子树也应该是堆，那么可以得到堆序性质： <p>对于每一个结点（X），X的父亲中的键小于（或等于）X中的键，根结点最小。 <p><b>基本操作：</b> <p>1. 插入 <p>跟AVL树处理相同，我们先尝试在数组下一位置创建一个空穴（hole，注意先不填入要插入的数值）。否则，该堆将不是完全二叉树。 <p>如果该数值比父结点大，并不破坏堆序性质，插入完成。 <p>如果该数值比父结点小，则该空穴的值修改为父结点的值。这样，空穴就朝着根的方向上行一步，继续该过程直到x能被放到空穴。 <p>这种一般的策略叫做上滤(percolate up). <p>注意，这里编程时不用使用递归完成。同时没有交换的操作。 <p>，类似于插入排序时的操作。 <p>插入的时间为O（logN）。 <p>2. 删除 <p>删除最小元后，根节点形成为空穴。因此移动最后一个元素x到恰当的地方即可。 <p>这里一般的策略叫做下滤（percolate down） <p>如果x可以被放到空穴，操作完成。 <p>反之，先比较空穴的两个子结点,取到最小的一个（如果只有一个则直接取，如果0个则删除完成）。空穴的值修改为子结点较小的那个的值，空穴下移一层。直到X可以放入空穴。 <p>注意，判断第i个结点的子结点数目： <p>2*i&gt;当前结点数目，则0个子结点。 <p>2*i=当前结点数目，则1个子结点。 <p>2*i&lt;当前结点数目，则2个子结点。 <p>比较与交换都只与较小的结点进行即可。 <p>删除的时间为O（logN） <p>堆还是很简单的，书上说： <p>我们将看到的这类数据结构属于计算机科学中最雅致的一种。 <p><b>参考资料</b>： <p>数据结构与算法分析—C++描述</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #cc6633">#ifndef</span>    BINARYHEAP_H<br><span style="color: #cc6633">#define</span> BINARYHEAP_H<br><br><span style="color: #cc6633">#include</span> &lt;iostream&gt;<br><span style="color: #cc6633">#include</span> &lt;vector&gt;<br><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;<br><br><span style="color: #0000ff">template</span>&lt;<span style="color: #0000ff">typename</span> Comparable&gt;<br><span style="color: #0000ff">class</span> BinaryHeap {<br><br><span style="color: #0000ff">public</span>:<br>    <span style="color: #0000ff">explicit</span> BinaryHeap( <span style="color: #0000ff">int</span> capacity=100 )<br>    {<br>        array.resize(capacity);<br>    }<br>    <span style="color: #0000ff">explicit</span> BinaryHeap( <span style="color: #0000ff">const</span> vector&lt;Comparable&gt; &amp;items);<br><br>    <span style="color: #0000ff">bool</span> isEmpty() <span style="color: #0000ff">const</span> { <span style="color: #0000ff">return</span> currentSize==0; }<br>    <span style="color: #0000ff">const</span> Comparable &amp;findMin() <span style="color: #0000ff">const</span> { <span style="color: #0000ff">return</span> array[1]; }<br><br>    <span style="color: #0000ff">void</span> insert(<span style="color: #0000ff">const</span> Comparable &amp;x);<br>    <span style="color: #0000ff">void</span> deleteMin();<br>    <span style="color: #0000ff">void</span> deleteMin( Comparable &amp;minItem);<br><br>    <span style="color: #0000ff">void</span> print()<br>    {<br>        <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=1; i&lt;=currentSize; ++i) {<br>            cout &lt;&lt; array[i] &lt;&lt; <span style="color: #006080">" "</span>;<br>            <span style="color: #0000ff">if</span> (i%5==0)<br>                cout &lt;&lt; endl;<br>        }<br>        cout &lt;&lt; endl;<br>    }<br><br><span style="color: #0000ff">private</span>:<br>    <span style="color: #0000ff">int</span> currentSize; <span style="color: #008000">//Number of elements in heap</span><br>    vector&lt;Comparable&gt; array;<span style="color: #008000">//The heap array</span><br><br>    <span style="color: #0000ff">void</span> buildHeap();<br>    <span style="color: #0000ff">void</span> percolateDown(<span style="color: #0000ff">int</span> hole);<br>};<br><br><span style="color: #0000ff">template</span>&lt;<span style="color: #0000ff">typename</span> Comparable&gt;<br><span style="color: #0000ff">void</span> BinaryHeap&lt;Comparable&gt;::insert(<span style="color: #0000ff">const</span> Comparable &amp;x)<br>{<br>    <span style="color: #0000ff">if</span> (currentSize==(array.size()-1))<br>        array.resize(currentSize*2);<br><br>    <span style="color: #0000ff">int</span> hole = ++currentSize;<br>    <span style="color: #0000ff">while</span> (hole&gt;1 &amp;&amp; array[hole/2]&gt;x) {<br>        array[hole] = array[hole/2];<br>        hole/=2;<br>    }<br>    array[hole] = x;<br>}<br><br><span style="color: #0000ff">template</span>&lt;<span style="color: #0000ff">typename</span> Comparable&gt;<br><span style="color: #0000ff">void</span> BinaryHeap&lt;Comparable&gt;::deleteMin()<br>{<br>    <span style="color: #0000ff">if</span> (isEmpty())<br>        <span style="color: #0000ff">return</span>;<br><br>    array[1] = array[currentSize--];<br>    percolateDown(1);<br>}<br><br><span style="color: #0000ff">template</span>&lt;<span style="color: #0000ff">typename</span> Comparable&gt;<br><span style="color: #0000ff">void</span> BinaryHeap&lt;Comparable&gt;::deleteMin(Comparable &amp;minItem)<br>{<br>    <span style="color: #0000ff">if</span> (isEmpty())<br>        <span style="color: #0000ff">return</span>;<br>    minItem = array[1];<br>    array[1] = array[currentSize--];<br>    percolateDown(1);<br>}<br><br><span style="color: #0000ff">template</span>&lt;<span style="color: #0000ff">typename</span> Comparable&gt;<br><span style="color: #0000ff">void</span> BinaryHeap&lt;Comparable&gt;::percolateDown(<span style="color: #0000ff">int</span> hole)<br>{<br>    <span style="color: #0000ff">int</span> child;<br>    Comparable tmp = array[hole];<br>    <br>    <span style="color: #0000ff">while</span> (hole*2&lt;=currentSize) {<br>        child = hole*2;<br>        <span style="color: #0000ff">if</span> (child!=currentSize &amp;&amp; array[child+1]&lt;array[child])<br>            child++;<br>        <span style="color: #0000ff">if</span> (array[child]&lt;tmp)<br>            array[hole] = array[child];<br>        <span style="color: #0000ff">else</span><br>            <span style="color: #0000ff">break</span>;<br>        hole = child;<br>    }<br>    array[hole] = tmp;<br>}<br><br><span style="color: #0000ff">template</span>&lt;<span style="color: #0000ff">typename</span> Comparable&gt;<br>BinaryHeap&lt;Comparable&gt;::BinaryHeap(<span style="color: #0000ff">const</span> vector&lt;Comparable&gt; &amp;items) : <br>    array(items.size()+10) ,<br>    currentSize(items.size())<br>{<br>    <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=0; i&lt;currentSize; i++)<br>        array[i+1] = items[i];<br>    buildHeap();<br>}<br><br><span style="color: #0000ff">template</span>&lt;<span style="color: #0000ff">typename</span> Comparable&gt;<br><span style="color: #0000ff">void</span> BinaryHeap&lt;Comparable&gt;::buildHeap()<br>{<br>    <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i=currentSize/2; i&gt;0; --i)<br>        percolateDown(i);<br>}<br>    <br>#endif<span style="color: #008000">//BINARYHEAP_H</span><br></pre><br></div><img src ="http://www.cppblog.com/izualzhy/aggbug/161332.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-12-02 22:51 <a href="http://www.cppblog.com/izualzhy/archive/2011/12/02/161332.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数据结构------AVL树</title><link>http://www.cppblog.com/izualzhy/archive/2011/11/21/160664.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Mon, 21 Nov 2011 14:40:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/11/21/160664.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/160664.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/11/21/160664.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/160664.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/160664.html</trackback:ping><description><![CDATA[<p>数据结构------AVL树 
<p><strong>学习数据结构的笔记，有不对或者需要改进的地方请指出。</strong> 
<p><strong>为什么要用AVL</strong><strong>树？</strong> 
<p>插入一组有序的数据到树中，实际上树变成了链表。 
<p><strong>基本知识：</strong> 
<p>AVL(Adelson-Velskii and Landis)树是带有平衡条件的二叉搜索树。 
<p>一棵AVL树是其每个结点的左子树和右子树的高度最多差1的二叉查找树（空子树的高度定义为-1）。 
<p>在高度为h的AVL树中，最少结点树S(h)=S(h-1)+S(h-2)+1，对于h=0,S(h)=1,h=1,S(h)=2。函数S(h)与斐波那契数密切相关。 
<p>例如下面两个图，只有右边的是AVL树。 
<p><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/WindowsLiveWriter/AVL_13ED5/clip_image002_2.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://www.cppblog.com/images/cppblog_com/izualzhy/WindowsLiveWriter/AVL_13ED5/clip_image002_thumb.jpg" width="244" height="150" /></a><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/WindowsLiveWriter/AVL_13ED5/clip_image004_2.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://www.cppblog.com/images/cppblog_com/izualzhy/WindowsLiveWriter/AVL_13ED5/clip_image004_thumb.jpg" width="244" height="123" /></a> 
<p><strong>旋转的定义，图示（重要）:</strong> 
<p>感觉这一部分文字描述起来很难，看图的话相对容易理解多了。而且印象会深刻一些。而且代码实现总是有差别的，例如我看到有的例子用的返回结点指针，而我从书上学习的例子则是传递指针的引用。 
<p>两者共同的地方就是在方法的使用上没有区别。因此，理解旋转很重要，理解清楚了以后，代码就好写的多了。因此，为了篇幅不至于过长，旋转我另写了一篇笔记。 
<p><strong>代码编写注意：</strong> 
<p><strong>AVLTree</strong><strong>从BinarySearchTree</strong><strong>继承而来，不得不重新修改了BinarySearchTree</strong><strong>，以及BinaryTree</strong><strong>的一些函数。</strong> 
<p>1. 结点高度 
<p>空子树的高度定义为-1.我在之前的BinaryTree里又加了一个函数getDepth求结点高度。<strong>刚开始用的返回值是</strong><strong>size_t</strong><strong>，因此就出现了问题</strong>。 
<p>2. 关于单旋转，双旋转看明白了。可还是不知道怎么编程去实现。 
<p>总之我是这样子的。不知道从哪里入手。其实过程是这样的： 
<p>仍然按照二叉搜索树的插入过程插入结点。这时有部分结点的高度发生了变化，因此可能不满足AVL性质了。书上是这么说的： 
<p><strong>在插入之后，只有那些从插入点到根节点的路径上的结点的平衡可能被改变，因为只有这些结点的子树可能发生变化。</strong>当我们沿着这条路径上行到根并更新平衡信息时，可以发现一个结点，它的新平衡破坏了AVL条件。我们将指出如何在第一个这样的结点（即最深的结点）重新平衡这棵树，并证明这一重新平衡保证整个树满足AVL性质。因此，我又对前面的二叉搜索树代码做了改动 
<p>virtual void BinarySearchTree::insert 
<p>virtual void BinarySearchTree::remove，因为此时我们要重新实现这两个函数。在路径上检查每个结点左子树和右子树的高度，由于递归的性质，第一个检查到的不平衡的结点即是最深的结点。（可能不容易想明白，书上讲的仔细些，自己编下代码就容易理解了，我的代码附在最下面）。当检查到第一个节点后，就去判断该使用哪一种旋转的方式。传入该结点指针（我传递的指针的引用），修改相应的结点关系就可以了。例如书上左子树单旋转是这么写的： 
<p>void rotateWithLeftChild(AvlNode *&amp;k2)//k2为检查到的第一个结点，k1为其左子树的根节点 
<p>{ 
<p>AvlNode *k1 = k2-&gt;left;//取k2左子树的根节点 
<p>k2-&gt;left = k1-&gt;right;//修改k2左子树为k1的右子树 
<p>k1-&gt;right = k2;//k1的右子树现在是k2了！ 
<p>k2 = k1;//提升k1的高度，之前k2的位置是k1了，因为这里是对k2的引用。 
<p>} 
<p>双旋转实际上可以用两次单旋转来完成，编程实现过程可以由此而来。 
<p>双旋转的效果与现在第一个不平衡的结点node的儿子和孙子之间单旋转，然后再在node和它的新儿子之间单旋转效果相同。 
<p><strong>问题：</strong> 
<p>1. 递归对效率的影响，例如每个结点的高度是否写在结点struct里。书上结点类型重新定义，为了使用之前的GUI程序直观的看下，没有修改结点的结构体定义，我重写了一个获得结点高度的函数。 
<p>2. 从解决大型的实际问题编程考虑，程序哪部分还存在问题？ 
<p>3. 我写的删除过程与插入是不可逆的，不知道怎么修改插入删除如何才能是可逆的？ <br /><a href="/Files/izualzhy/AVLTree.rar">/Files/izualzhy/AVLTree.rar</a><br />
<p>参考资料： 
<p>《数据结构与算法分析------C++描述》中文版 Mark Allen Weiss著 </p><img src ="http://www.cppblog.com/izualzhy/aggbug/160664.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-11-21 22:40 <a href="http://www.cppblog.com/izualzhy/archive/2011/11/21/160664.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数据结构------二叉查找树</title><link>http://www.cppblog.com/izualzhy/archive/2011/11/17/160380.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Thu, 17 Nov 2011 13:50:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/11/17/160380.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/160380.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/11/17/160380.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/160380.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/160380.html</trackback:ping><description><![CDATA[<p>数据结构------二叉查找树 
<p><strong>学习数据结构的笔记，有不对或者需要改进的地方请指出。</strong> 
<p><strong>基本概念：</strong> 
<p>使二叉树称为二叉查找树的性质是，对于树中的每个结点X，它的左子树中所有项的值小于X中的项，而它的右子树中所有项的值大于X中的项。 
<p>左边的树是二叉查找树，而右边不是。 
<p>6 6 
<p>/ \ / \ 
<p>2 8 2 8 
<p>/ \ / \ 
<p>1 4 1 4 
<p>/ / \ 
<p>3 3 7 
<p>有了这个性质，查找就方便多了，最大值只要从root一直往右查找，最小值只要一直往左查找就可以了。 
<p><strong>编写代码心得：</strong> 
<p>由于树的递归定义，通常是递归地编写关于树的操作。因为二叉查找树的平均深度是O(logN),所以我们一般不必担心栈空间被用尽。 
<p>public的成员函数使用private递归函数的常规定义。因此我们只讨论private的函数。 
<p>分别写了以下几个： 
<p>1. contains：判断是否包含该数值。 
<p><strong>首先判断树是否为空。</strong>注意该函数里的两个递归都是尾递归（尾递归什么意思？）。该函数实现也可以用用一个while来实现。 
<p>2. FindMin，FindMax：寻找最大值最小值 
<p>从根开始只要有左儿子就向左进行。终止点就是最小的元素。 
<p>递归编写FindMin，非递归编写FindMax。注意这只是实现方式上的区别就行了。 
<p>FindMax里对t的改变是安全的，因为我们只用指针的副本来工作。 
<p>3. insert：插入数值到树中。 
<p>如果数值本来就在树中，我们什么都不做。 
<p>程序里插入时不会改变原来结点的位置，比如我们的树现在只有一个根节点1，插入2实际上结果有多种可能： 
<p>1 2 
<p>\ / 
<p>2 1 
<p>即程序里我采用的是第一种，也是书上的做法。因此相同的一组数值插入过程的不同生成的树就不相同，稍后我们打印会看一下。 
<p><strong></strong><strong>代码本身传入的是指针的引用。</strong> 
<p>注意，在递归例程中，只有当一个新树叶生成的时候，t才改变。当这种情况发生的时候，就说明递归例程被其他结点p调用了。该节点p是树叶的父亲。调用将是insert(x,p-&gt;left),or insert(x,p-&gt;right)。 
<p>在任何一种方法中，t是到p-&gt;left or p-&gt;right的引用。这意味着p-&gt;left or p-&gt;right将会改变为指向新节点。总而言之，这是一个老套的技巧。 
<p>4. remove:删除某个数值的结点 
<p>同许多数据结构一样，最困难的操作是删除。一旦发现要被删除的结点，就需要考虑集中可能的情况。 
<p>针对二叉搜索树，有三种情况： 
<p>1. 如果结点是一片树叶，那么它可以被立即删除。 
<p>2. 如果结点有一个儿子，那么该节点的父节点绕过它后链到其子节点即可。 
<p>3. 如果结点有两个儿子，一般的删除策略是用其右子树的最小的数据（很容易找到）代替该节点的数据并递归的删除那个结点（现在它是空的）。因为右子树的最小的结点不可能有左儿子，所以第二次remove会很容易（1or2的情况）。挺抽象的，没办法。。。画个树的图或者看下代码就容易理解了！ 
<p>如前面提到的，不同的插入顺序，即使数值相同，树的形状也会有差别，你能猜到我插入这几个字符的顺序么？ 
<p><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/WindowsLiveWriter/e8fb5453538f_132FF/clip_image002_2.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://www.cppblog.com/images/cppblog_com/izualzhy/WindowsLiveWriter/e8fb5453538f_132FF/clip_image002_thumb.jpg" width="244" height="116" /></a><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/WindowsLiveWriter/e8fb5453538f_132FF/clip_image004_2.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://www.cppblog.com/images/cppblog_com/izualzhy/WindowsLiveWriter/e8fb5453538f_132FF/clip_image004_thumb.jpg" width="244" height="95" /></a> 
<p>附件是源文件，画图的话只要修改前面上传的gui的main.cpp就行了，有兴趣的可以看下insert remove是不是的确按照我们的策略进行的。 
<p>关于第一张图，是我们开始AVL树的原因。 
<p><strong>平均情况分析：</strong> 
<p>上面提到的删除算法有助于使得左子树比右子树深度深，因为我们总是用右子树的一个结点来代替删除的结点。 
<p>在删除操作中，可以通过随即选取右子树的最小元素或者左子树的最大元素来代替被删除的元素以消除这种不平衡问题。 
<p>如果插入是有序的，那么此时的树将只由那些没有左儿子（右儿子）的结点组成（第一张图）。 
<p>一种解决办法就是要有一个称为<strong>平衡</strong><strong>(balance)</strong>的附加的结构条件：任何结点的深度均不得过深。 
<p>有许多一般的算法实现平衡树。但是，大部分算法都要比标准的二叉搜索树复杂得多，更新要平均花费更长的时间。不过，它们确实防止了处理起来非常麻烦的一些简单问题。AVL树是最老的一种平衡二叉搜索树。 
<p>另外，较新的办法是放弃平衡条件，允许树有任意的深度，但是在每次操作之后要使用一个调整规则进行调整，使得后面的操作效率更高。这种类型的数据结构一般属于自调整(self-adjusting)类结构。 
<p>下篇笔记开始学习<strong>AVL</strong><strong>树。</strong> <br />程序源码：<a href="/Files/izualzhy/binarySearchTree.rar">/Files/izualzhy/binarySearchTree.rar</a><br />
<p>参考资料： 
<p>《数据结构与算法分析------C++描述》中文版 Mark Allen Weiss著 </p><img src ="http://www.cppblog.com/izualzhy/aggbug/160380.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-11-17 21:50 <a href="http://www.cppblog.com/izualzhy/archive/2011/11/17/160380.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数据结构------利用Qt画出树的结构</title><link>http://www.cppblog.com/izualzhy/archive/2011/11/17/160379.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Thu, 17 Nov 2011 13:48:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/11/17/160379.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/160379.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/11/17/160379.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/160379.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/160379.html</trackback:ping><description><![CDATA[<p>数据结构------利用Qt画出树的结构 
<p>这两天学习二叉树的时候，总感觉不够直观，又在csdn上看到了一篇利用Qt画出一棵树的结构。试着自己写了下，总没人家画的好看。不过有了这个经验，正好就用在了正在学习的数据结构上。 
<p><strong>基本介绍：</strong> 
<p>TreeWidget类负责绘制，构造该类的时候需要传入一个BinaryTreeNode（在之前的数据结构笔记BinaryTree里使用的节点类，我在学习二叉搜索树等时也用了这个类作为树的结点，因此也可以用来画其他类型的树），当然该结点必须已经是构造好的树的根节点（该类只负责根据结点关系绘制），然后根据该结点的leftChild,rightChild去不断的递归绘制结点间的线段。 
<p>主要是重写了paintEvent函数 
<p>virtual void paintEvent(QPaintEvent *e); 
<p>里面负责递归绘制的是： 
<p>void draw(BinaryTreeNode&lt;T&gt; *node, int x, int y, int angle, bool isLeft, int depth, QPainter *p);// 其实跟树的遍历是一个想法 
<p>其中 
<p>x,y是该结点坐标,angle表示边的角度，isLeft表示是否是左儿子，传入这个值主要是想是树画得别两条边交叉到一块了，不过结果并不理想，知道如何设置参数才能使树的边不交叉的同学教我一下，谢谢:-D。depth是深度，深度越大时，画得线相应短一些。 
<p>如上篇笔记里的，二叉树画出来的样子： 
<p><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/WindowsLiveWriter/Qt_132C7/clip_image002_2.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://www.cppblog.com/images/cppblog_com/izualzhy/WindowsLiveWriter/Qt_132C7/clip_image002_thumb.jpg" width="244" height="132" /></a> 
<p>二叉树的文件我放在了binaryTree文件夹下，该gui程序源文件放在了gui文件夹下，两个文件夹是同级的。所以TreeWidget.h有这一句： 
<p>#include "../binaryTree/binaryTree.h"，如果不这么放需要修改这里。 
<p>对应的，#include "../binarySearchTree/binarySearchTree.h"是为了画二叉搜索树，没有这个文件就先注掉，等学习并写好二叉搜索树的代码后，我会放上来。 
<p>问题： 
<p>1. 之前提到的，因为角度都用的随机数，需要设置上其他合适的参数，否则树的边会有交叉。 
<p>2. 模板的实现必须放在h文件里，而Qt实现放在h文件里好像容易出问题，所以写了这么一句typedef char T;本来想写成template的。发现自己懂得太少，写不通。。。。 
<p><strong>程序源代码：</strong> <a href="/Files/izualzhy/gui.rar">/Files/izualzhy/gui.rar</a></p><img src ="http://www.cppblog.com/izualzhy/aggbug/160379.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-11-17 21:48 <a href="http://www.cppblog.com/izualzhy/archive/2011/11/17/160379.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>