<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type='text/xsl' href='http://shitfromthemouth.spaces.live.com/mmm2008-05-17_13.22/rsspretty.aspx?rssquery=en-US;http%3a%2f%2fshitfromthemouth.spaces.live.com%2fcategory%2f%e8%ae%a1%e7%ae%97%e6%9c%ba%e4%b8%8e%2bInternet%2ffeed.rss' version='1.0'?><rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:msn="http://schemas.microsoft.com/msn/spaces/2005/rss" xmlns:live="http://schemas.microsoft.com/live/spaces/2006/rss" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>追忆似水流年: 计算机与 Internet</title><description /><link>http://shitfromthemouth.spaces.live.com/?_c11_BlogPart_BlogPart=blogview&amp;_c=BlogPart&amp;partqs=cat%25E8%25AE%25A1%25E7%25AE%2597%25E6%259C%25BA%25E4%25B8%258E%2bInternet</link><language>en-US</language><pubDate>Mon, 09 Jun 2008 06:28:34 GMT</pubDate><lastBuildDate>Mon, 09 Jun 2008 06:28:34 GMT</lastBuildDate><generator>Microsoft Spaces v1.1</generator><docs>http://www.rssboard.org/rss-specification</docs><ttl>60</ttl><cf:parentRSS>http://shitfromthemouth.spaces.live.com/blog/feed.rss</cf:parentRSS><live:type>blogcategory</live:type><live:identity><live:id>7830210819026847498</live:id><live:alias>shitfromthemouth</live:alias></live:identity><cf:listinfo><cf:group ns="http://schemas.microsoft.com/live/spaces/2006/rss" element="typelabel" label="Type" /><cf:group ns="http://schemas.microsoft.com/live/spaces/2006/rss" element="tag" label="Tag" /><cf:group element="category" label="Category" /><cf:sort element="pubDate" label="Date" data-type="date" default="true" /><cf:sort element="title" label="Title" data-type="string" /><cf:sort ns="http://purl.org/rss/1.0/modules/slash/" element="comments" label="Comments" data-type="number" /></cf:listinfo><item><title>MS interview question</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!471.entry</link><description>&lt;p&gt;Give a interger array of size n, how to find a contiguous subarray whose 
&lt;p&gt;sum is equal to k.if there is, return 1, else 0.&lt;br&gt;&lt;br&gt;e.g.  array[n]={1, 3, -4, 8, -1}    k=10&lt;br&gt;because 3+8-1=10, then return 1;&lt;br&gt;&lt;br&gt;What is the best time complexity?? 
&lt;div&gt;=================================================================&lt;/div&gt;
&lt;div&gt; &lt;/div&gt;
&lt;div&gt;I think the following is O(nlogn).&lt;br&gt;&lt;br&gt;Given an array A in which we need to find the contiguous sub-array of sum k, create a new Array B such that&lt;br&gt;&lt;br&gt;B[i] = Sum j = 1 to i A[i]&lt;br&gt;&lt;br&gt;i.e B[m] = A[1] + A[2] + ...  A[m] is the prefix sum array of A.&lt;br&gt;&lt;br&gt;Now create a third array C such that C[i] = (B[i], i) (ordered pairs)&lt;br&gt;&lt;br&gt;Now sort array C using the values B only (i.e ignore the second co-ordinate of the ordered pair).&lt;br&gt;&lt;br&gt;Now for each element C[p] =  (B[q],q) do a binary search for B[q] + K in C. Say you find the element(s) C[r] = (B[s], s)&lt;br&gt;&lt;br&gt;if q &amp;lt; s, then we are done, else continue.&lt;br&gt;&lt;br&gt;(Note, we might have to create a new array C' by merging sub-arrays with same value of B and also store the set of indices of B which take that value i.e. instead of just using the ordered pair (B,i) we use (B, {i1, i2, ..., ik}) and do a binary search on C')&lt;/div&gt;
&lt;div&gt;+++++++++++++++++++++++++++++++++++++++++++++++++++&lt;/div&gt;
&lt;div&gt;这个题目不同于找最大之和，最大之和的解能在O(1)空间下,在O(n)时间内完成&lt;br&gt;&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+MS+interview+question&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!471.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!471.entry</guid><pubDate>Wed, 07 May 2008 23:09:13 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!471/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!471.entry#comment</wfw:comment><dcterms:modified>2008-05-07T23:12:14Z</dcterms:modified></item><item><title>finding the single number in a 2n+1 numbers array</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!470.entry</link><description>&lt;div&gt;Digest from &lt;a href="http://discuss.techinterview.org/?interview"&gt;http://discuss.techinterview.org/?interview&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;----------------------------------------------------------&lt;/div&gt;
&lt;div&gt;I have an array consisting of 2n+1 elements. n elements in it are&lt;br&gt;married, i.e they occur twice in the array, however there is one element&lt;br&gt;which only appears once in the array. I need to find that number in a&lt;br&gt;single pass using constant memory. {assume all are positive numbers}&lt;br&gt;Eg : 3 4 1 3 1 7 2 2 4&lt;br&gt;Ans: 7 &lt;/div&gt;
&lt;div&gt;===============================================&lt;/div&gt;
&lt;div&gt;var array = [3, 4, 1, 3, 1, 7, 2, 2, 4]&lt;br&gt;var remaining = 0;&lt;br&gt;&lt;br&gt;for(var i = 0; i &amp;lt; array.length; i++)&lt;br&gt;{&lt;br&gt;  remaining ^= array[i];&lt;br&gt;}&lt;br&gt;&lt;br&gt;return remaining; &lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+finding+the+single+number+in+a+2n%2b1+numbers+array&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!470.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!470.entry</guid><pubDate>Sun, 04 May 2008 03:09:58 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!470/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!470.entry#comment</wfw:comment><dcterms:modified>2008-05-04T03:09:58Z</dcterms:modified></item><item><title>tech interview</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!469.entry</link><description>&lt;h3&gt;sort the array...&lt;/h3&gt;&lt;a&gt;&lt;/a&gt;
&lt;div&gt;
&lt;div&gt;given an array of size n.. sub arrays of a[0:k] and a[k+1:n] are sorted..&lt;/div&gt;
&lt;div&gt; sort the array in O(n) time and O(1) space &lt;/div&gt;&lt;/div&gt;
&lt;div&gt; &lt;/div&gt;
&lt;div&gt;==========================================================&lt;/div&gt;
&lt;div&gt;首先，应该用原地置换的思想。2，用两个指针，分别指向0和k＋1位置。两个比较，&lt;/div&gt;
&lt;div&gt;小的换到0的那个指针(表示全局的较小)，后面那个指针得到的换过来的元素要和它的后续比较一下，&lt;/div&gt;
&lt;div&gt;要确定这个指针所指是后半部分的最小。这样两指针一直从左到右移动，就排序完了。&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+tech+interview&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!469.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!469.entry</guid><pubDate>Sun, 04 May 2008 02:55:32 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!469/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!469.entry#comment</wfw:comment><dcterms:modified>2008-05-04T02:55:32Z</dcterms:modified></item><item><title>dive into python</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!468.entry</link><description>&lt;div&gt;最近在公司忙里偷闲，装了python，开始玩了。批评一句，那个shell太不友好了，虽然能调颜色。但是它也是很好玩的，现在基本上是语法和语言特性的体验阶段。公司一天要损失20%的劳动力了，哈哈。&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+dive+into+python&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!468.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!468.entry</guid><pubDate>Tue, 29 Apr 2008 23:46:11 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!468/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!468.entry#comment</wfw:comment><dcterms:modified>2008-04-29T23:46:11Z</dcterms:modified></item><item><title>尾递归</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!460.entry</link><description>&lt;div&gt;转载啊&lt;/div&gt;
&lt;div&gt;================================================&lt;/div&gt;
&lt;div&gt;其实，对一些如树的递归结构，递归算法是又自然又好用。 &lt;br&gt;如果看看一些用来代替递归的技术，（汉诺塔的迭代算法不去说它，那是真正的算法的革命，除了佩服没啥好说的），一般来说只不过是自己模拟堆栈，编起来费劲，读起来费劲，维护起来更费劲。而模拟堆栈的效果，相比于简单的递归，好处在哪里呢？ &lt;br&gt;1。不使用进程堆栈，不会耗尽堆栈空间（虽然可能耗尽堆空间） &lt;br&gt;2。可以有选择地把真正有用的东西压栈，而不用什么pc, sp, 所有的局部变量都压栈。这样节省了一些内存（不过，仍然在一个数量级，递归是O(N)，模拟递归仍然是O(N))。不过这也不绝对，编译器的优化可以识别一些不需要保存的局部变量的。（这叫变量生存期分析） 那么这样做又没有坏处呢？除了上面的代码复杂度的问题（我想很多搞嵌入式系统，实时系统的C高手都对此不屑一顾），还有一个效率上的缺点： 模拟的递归使用堆空间，它的new/delete都比直接在堆栈上分配空间慢得多。而且很容易产生内存碎片。 所以说，模拟堆栈只不过是牺牲了一定的效率来换取了一部分空间而已。是否值得，嘿嘿，就得看具体应用了。 好，闲话说完，下面言归正传。 &lt;br&gt;&lt;br&gt;话说大家都知道函数调用要压栈。不过这是有几个例外的。 &lt;br&gt;1。函数是inline的。 &lt;br&gt;2。语言采用的函数调用方式是continuation, 而不是activation record. 这种模式中语言可以使用堆而不是栈来完成函数调用。 &lt;br&gt;3。尾调用。也就是说，函数调用出现在调用者函数的尾部，因为是尾部，所以根本没有必要去保存任何局部变量，sp, pc的信息。直接让被调用的函数返回时越过调用者，返回到调用者的调用者去。 &lt;br&gt;尾调用优化不是什么很复杂的优化，实际上几乎所有的现代的高级语言编译器都支持尾调用这个很基本的优化。 &lt;br&gt;实现层面上，只需要把汇编代码call改成jmp, 并放弃所有局部变量压栈处理，就可以了。所以，很简单。 &lt;br&gt;&lt;br&gt;于是，就剩下递三条：尾调用。而一个对自己本身的递归尾调用，就叫做尾递归。 &lt;br&gt;&lt;br&gt;那么，当尾递归时，我们就没有前面分析的递归调用的占用堆栈的缺点，因为每次调用都是尾调用，所以堆栈根本就没有被占用，每次调用都是重新使用调用者的堆栈。 &lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+%e5%b0%be%e9%80%92%e5%bd%92&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!460.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!460.entry</guid><pubDate>Sun, 16 Mar 2008 06:58:07 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!460/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!460.entry#comment</wfw:comment><dcterms:modified>2008-03-16T06:58:07Z</dcterms:modified></item><item><title>求中位数</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!438.entry</link><description>&lt;div&gt;
&lt;div&gt;一个&lt;img alt="n\times n" src="http://zhiqiang.org/bbs/bb-cache/tex_607acaa73c762411b20745149a11e90b.gif" align=absMiddle&gt;的矩阵数列，若此序列每一列都是增序的，求这&lt;img alt="n^2" src="http://zhiqiang.org/bbs/bb-cache/tex_6595d679e306a127a3fe53268bcaddb2.gif" align=absMiddle&gt;个数的中位数&lt;/div&gt;
&lt;div&gt; &lt;/div&gt;
&lt;div&gt;================================================================&lt;/div&gt;
&lt;div&gt;
&lt;p&gt;其实对于任意的N个数求中位数，已知算法可以在&lt;img alt="O(N)" src="http://zhiqiang.org/bbs/bb-cache/tex_33697ce7dfa48ba80980d298c8089378.gif" align=absMiddle&gt;时间内完成，因此，对这个问题&lt;img alt="O(n^2)" src="http://zhiqiang.org/bbs/bb-cache/tex_9f84a66d88d24c3b1bc91df5b5346a13.gif" align=absMiddle&gt;完成是没有问题的。
&lt;p&gt;&lt;img alt="O(n\log^3n)" src="http://zhiqiang.org/bbs/bb-cache/tex_2aa5a5173c74ed81d2dff20a23d82ac7.gif" align=absMiddle&gt;的算法基本思路是这样的：&lt;br&gt;1.先取每个序列的中位数组成集合&lt;img alt="A=\{a_1,...,a_n\}" src="http://zhiqiang.org/bbs/bb-cache/tex_6ab6af6d3b29917fa077422bcdbe8917.gif" align=absMiddle&gt;，用已知的算法在&lt;img alt="O(n)" src="http://zhiqiang.org/bbs/bb-cache/tex_7ba55e7c64a9405a0b39a1107e90ca94.gif" align=absMiddle&gt;时间内找到&lt;img alt=A src="http://zhiqiang.org/bbs/bb-cache/tex_7fc56270e7a70fa81a5935b72eacbe29.gif" align=absMiddle&gt;的中位数，计为&lt;img alt=a src="http://zhiqiang.org/bbs/bb-cache/tex_0cc175b9c0f1b6a831c399e269772661.gif" align=absMiddle&gt;,&lt;br&gt;2.在所有序列中，用折半查找找出&lt;img alt=a src="http://zhiqiang.org/bbs/bb-cache/tex_0cc175b9c0f1b6a831c399e269772661.gif" align=absMiddle&gt;所处的位置，从而算出&lt;img alt=a src="http://zhiqiang.org/bbs/bb-cache/tex_0cc175b9c0f1b6a831c399e269772661.gif" align=absMiddle&gt;在所有数中的位置，&lt;br&gt;3.如果&lt;img alt=a src="http://zhiqiang.org/bbs/bb-cache/tex_0cc175b9c0f1b6a831c399e269772661.gif" align=absMiddle&gt;恰好是中位数，搞定，输出&lt;img alt=a src="http://zhiqiang.org/bbs/bb-cache/tex_0cc175b9c0f1b6a831c399e269772661.gif" align=absMiddle&gt;；&lt;br&gt;如果&lt;img alt="a &amp;gt; n^2/2" src="http://zhiqiang.org/bbs/bb-cache/tex_054f1cb3c296505d3a8b40218d3ac9ea.gif" align=absMiddle&gt;，将所有&lt;img alt="\{i|a_i \geq a}" src="http://zhiqiang.org/bbs/bb-cache/tex_b6e7c3f80885290eaf67ca9b295b6d8d.gif" align=absMiddle&gt;所对应的序列长度减半（因为这些序列中有一半的数&lt;img alt="\geq a_i \geq a" src="http://zhiqiang.org/bbs/bb-cache/tex_adfb68fea022da95d24564aed6b549a5.gif" align=absMiddle&gt;从而不可能是中位数）；&lt;br&gt;如果&lt;img alt="a &amp;lt; n^2/2" src="http://zhiqiang.org/bbs/bb-cache/tex_e53d79122fe5618ec48fe9436c3a031f.gif" align=absMiddle&gt;，将所有&lt;img alt="\{i|a_i \leq a}" src="http://zhiqiang.org/bbs/bb-cache/tex_2093d019978200479cfa807435b7f2aa.gif" align=absMiddle&gt;所对应的序列长度减半（因为这些序列中有一半的数&lt;img alt="\leq a_i \leq a" src="http://zhiqiang.org/bbs/bb-cache/tex_5edc884e52fc5b89c92b6ceeba487a78.gif" align=absMiddle&gt;从而不可能是中位数）；&lt;br&gt;4.对于剩下的所有长度&lt;img alt="&amp;gt;1" src="http://zhiqiang.org/bbs/bb-cache/tex_fabf0374c6e2cd8fd58caf70897b4e9a.gif" align=absMiddle&gt;的序列，重复1-3。
&lt;p&gt;由于每次循环长度大于1的序列有一半被减半，并且对于长为&lt;img alt=n src="http://zhiqiang.org/bbs/bb-cache/tex_7b8b965ad4bca0e41ab51de7b31363a1.gif" align=absMiddle&gt;的序列最多经过&lt;img alt="O(\log n)" src="http://zhiqiang.org/bbs/bb-cache/tex_0ca47d9a481af371d1210a620c1945db.gif" align=absMiddle&gt;次减半后长度变为1，因此以上循环最多&lt;img alt="O(\log^2n)" src="http://zhiqiang.org/bbs/bb-cache/tex_1245d412ae9fab2fd7892fb683c6fa61.gif" align=absMiddle&gt;次以后所有序列长度为1从而问题可以简单解决。而每次循环的时间（包括找中位数的中位数，确定该中位数在所有数中的位置，对所有长度非1的序列判断是否要减半）为&lt;img alt="O(n\log n)" src="http://zhiqiang.org/bbs/bb-cache/tex_f49341ab621f12e8cb93d0146ea51d34.gif" align=absMiddle&gt;。所以总时间复杂性为&lt;img alt="O(n\log^3 n)" src="http://zhiqiang.org/bbs/bb-cache/tex_33d4dad9277c79213642f22364c8f933.gif" align=absMiddle&gt;。
&lt;p&gt;一些细节，比如更新当前循环中所有数的中位数在当前剩余数中的位置、最多需要&lt;img alt="O(\log^2 n)" src="http://zhiqiang.org/bbs/bb-cache/tex_c1fbc697a6c34cba2a5c858f5949d346.gif" align=absMiddle&gt;次循环等等，还需要仔细的考虑甚至归纳证明，这里就不讨论了。
&lt;p&gt;最后，提一下能想到的这个问题的时间复杂性下界&lt;img alt="O(n\log n)" src="http://zhiqiang.org/bbs/bb-cache/tex_f49341ab621f12e8cb93d0146ea51d34.gif" align=absMiddle&gt;：&lt;br&gt;在decision tree模型下，在n个序列中找中位数，需要区分的状态数，相当于对n个序列确定&lt;img alt="index_i,i\in n" src="http://zhiqiang.org/bbs/bb-cache/tex_4fa91768a1aaa2e2ba4d4fd09849c345.gif" align=absMiddle&gt;，使得中位数&lt;img alt=q src="http://zhiqiang.org/bbs/bb-cache/tex_7694f4a66316e53c8cdd9d9954bd611d.gif" align=absMiddle&gt;恰好满足&lt;img alt="a^i_{index_i}\leq q\leq a^i_{index_i+1}" src="http://zhiqiang.org/bbs/bb-cache/tex_143b69705807b7132ce8487d6f96ccaa.gif" align=absMiddle&gt;时所有可能的index方法的个数，也就是&lt;img alt="\sum_i ^n l_i = n^2/2" src="http://zhiqiang.org/bbs/bb-cache/tex_535e871be89410a6a51b0c1e393719a8.gif" align=absMiddle&gt;并且&lt;img alt="0\leq l_i\leq n_i" src="http://zhiqiang.org/bbs/bb-cache/tex_bdb1d49e38e6d33f2c65a16f3c1613b8.gif" align=absMiddle&gt;的解的个数。而这个数目不小于对前一半的序列随便在0到n之间取值的数目（因为这样的取值一定可以通过调整后一半的index使得方程有解），所以总状态数不小于&lt;img alt="O(n^{n/2})" src="http://zhiqiang.org/bbs/bb-cache/tex_16a1fb3fc77e167fe58eb7f123e66988.gif" align=absMiddle&gt;，从而需要的比较次数至少为其对数&lt;img alt="O(\log(n^{n/2}))=O(n\log n)" src="http://zhiqiang.org/bbs/bb-cache/tex_0aa00fafb1a92c0211f30f3ed02eef02.gif" align=absMiddle&gt;。
&lt;p&gt;==========================================================================
&lt;p&gt;&lt;a href="http://zhiqiang.org/bbs/topic/62?replies=7" target="_blank" rel=nofollow&gt;此文转自一理论计算机大牛的blog。&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+%e6%b1%82%e4%b8%ad%e4%bd%8d%e6%95%b0&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!438.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!438.entry</guid><pubDate>Sat, 17 Nov 2007 06:59:19 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!438/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!438.entry#comment</wfw:comment><dcterms:modified>2007-11-17T06:59:19Z</dcterms:modified></item><item><title>求0，1矩阵的最大连续全1块（con）</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!435.entry</link><description>&lt;div&gt;
&lt;p&gt;给一个&lt;img alt="n\times m" src="http://zhiqiang.org/bbs/bb-cache/tex_252d3754c0db62a55b9e25c870a524a5.gif" align=absMiddle&gt;的0，1矩阵，求它的面积最大的全1子块。 
&lt;p&gt;要求&lt;img alt="O(n^2)" src="http://zhiqiang.org/bbs/bb-cache/tex_9f84a66d88d24c3b1bc91df5b5346a13.gif" align=absMiddle&gt;时间。 
&lt;p&gt;------------------------------------------------------------------------
&lt;p&gt;对矩阵做三次扫描, 扫描的次序都是从左到右,从上到下.第一遍将所有为1的元素,依次标一个值,这个值从1开始 例如:&lt;br&gt;0 0 0 0 0 0 0 0 0 0&lt;br&gt;0 1 1 0 0 0 1 2 0 0&lt;br&gt;0 1 0 0 0 标注成这样 0 3 0 0 0&lt;br&gt;0 0 0 1 0 0 0 0 4 0&lt;br&gt;0 0 0 1 0 0 0 0 5 0&lt;br&gt;第二遍 如果 遇到不为0 的元素, 考虑这个元素以及它周围四个元素中,不为0的元素标，求他们的最小值，并将这些元素通标注为这个最小值　像上面，遇到了１　那么将 2和3 标注成 1 。就变成下面这样&lt;br&gt;0 0 0 0 0&lt;br&gt;0 1 1 0 0&lt;br&gt;0 1 0 0 0&lt;br&gt;0 0 0 4 0&lt;br&gt;0 0 0 4 0&lt;br&gt;左后一遍 数一下各种数字的个数，最多的，就是最大全1子块的面积 
&lt;p&gt;(注，这样做，结果是连续的全1区域，很可能是不规则的) 
&lt;p&gt;------------------------------------------------------------------------------------
&lt;p&gt;对值为1的位置(x,y)，找到最小的y'，y'&amp;lt;=y，满足(x,y)到(x,y')一段全为1，并记y-y'+1为h(x,y)，然后以h(x,y)为高向两边扩展，找到最左和最右能扩展的位置xleft(x,y),xright(x,y)，宽度w(x,y)=xright(x,y)-xleft(x,y)+1，面积S(x,y)=h(x,y)*w(x,y) 
&lt;p&gt;从所有S(x,y)中取最优，所有需要计算的变量通过递推求得，可以做到O(n^2) 
&lt;p&gt;（这方法似乎可行，不过没有细想） 
&lt;p&gt;-------------------------------------------------------------------------------------
&lt;p&gt;其实我在想，对于某个格子，假定其坐标是x0,y0，那么其上方的坐标是x0,y0+1，左边的坐标是x0-1,y0。那么我们如果知道一个左上角和右下角的坐标，就可以知道连续1块的位置和大小。所以假设x1,y1是相对x0,y0+1为左上角的坐标，而x2,y2是相对x0-1,y0为左上角的坐标，从x1,y1到x0,y0+1表示一个连续全1块，而从x2,y2到x0-1,y0表示另一个连续全1块。这样，我们对比x1,x2,y1,y2的关系就可以判断对于x0,y0来说，左上角坐标应该是多少。&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+%e6%b1%820%ef%bc%8c1%e7%9f%a9%e9%98%b5%e7%9a%84%e6%9c%80%e5%a4%a7%e8%bf%9e%e7%bb%ad%e5%85%a81%e5%9d%97%ef%bc%88con%ef%bc%89&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!435.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!435.entry</guid><pubDate>Sun, 11 Nov 2007 02:57:30 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!435/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!435.entry#comment</wfw:comment><dcterms:modified>2007-11-11T04:58:16Z</dcterms:modified></item><item><title>直方图的最大内接矩形</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!421.entry</link><description>&lt;div&gt;求直方图的最大内接矩形,假设每个细条的宽度为1&lt;/div&gt;
&lt;div&gt;======================================== &lt;/div&gt;
&lt;div&gt;这题目以前贴过，不过那时还没理解。现在仔细想想，直方图的潮涨潮落就像一个一个峰，我们能把每一峰之内的最大内接矩形算出来就可以了。我们可以用一个栈来对付一个峰。这里的本质是一个序列之最大增长子序列。&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+%e7%9b%b4%e6%96%b9%e5%9b%be%e7%9a%84%e6%9c%80%e5%a4%a7%e5%86%85%e6%8e%a5%e7%9f%a9%e5%bd%a2&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!421.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!421.entry</guid><pubDate>Sun, 23 Sep 2007 04:45:43 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!421/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!421.entry#comment</wfw:comment><dcterms:modified>2007-09-23T04:45:43Z</dcterms:modified></item><item><title>shuffle paper</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!403.entry</link><description>&lt;div&gt;&lt;table cellspacing="0" border="0"&gt;&lt;tr height="8"&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1pO825iEfWc-eDVll4MMBqSd3DukEjhMglPViegG8yGSGtspww_-gZnA"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;404&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td width="15"&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1p9-0NJL0zOAvUOxDtsEuMIah2903ZH7hgR1hu6-gZzk1WcHgO3Plodg"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;405&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td width="15"&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1p_0DJg6DDRzLvKthunRNhchSVG2N8HsNXnJgmsCJCTtkTNcHojiQ3qw"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;406&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td width="15"&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1pHzrK4xYShCDgxKddHCToW_ld58mM1pO5M5Pr6GIbPL4mFuPqbzi-Zg"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;407&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1pU4Mh3qW9R-Jt2ARvVsyklzOz3YSAvYOJBBJKjhfgf4qAGQF1mMttBg"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;408&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td width="15"&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1piTCV617J8M2D5V_clUVonb06QLQDNW-4yWU8nw8c6zcIZXrI5EPT2Q"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;409&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td width="15"&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1ppxcBS1CQIs8t2W-NrrWmbIW_Jgn32xy_CA-jj2SsVCslxtJgS8hVnQ"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;410&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td width="15"&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1p7qkRfFGZBc2hsGcC2lSg1iSijIs_IXi_F7ExeApINW2QiscyU7uxfw"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;411&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1pQgGrcw3jyKrWs3AQkdF9JSkwc9AR6LM-yieFfCPr1n06On9gLEmjDg"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;412&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td width="15"&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1p4o7hjQN1GyaNSkIQT-zYyK0aTRsdG12p9zg56BtrlmrEs3AEPg3h0w"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;413&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td width="15"&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1pMLtxdzSagPkUT1admhByMtSToDV5UC50pO39X8qoKRNrx0YRVipE7A"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;414&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td width="15"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+shuffle+paper&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!403.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!403.entry</guid><pubDate>Sat, 25 Aug 2007 09:04:59 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!403/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!403.entry#comment</wfw:comment><dcterms:modified>2007-08-25T09:04:59Z</dcterms:modified></item><item><title>古老的面试题</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!377.entry</link><description>&lt;div&gt;一位老兄在天涯上面提了一个算法题，给人取笑了，呵呵&lt;/div&gt;
&lt;div&gt;说是有一大串数字，你又不知道有多少，内存肯定装不下，叫你随机输出其中一个数。&lt;/div&gt;
&lt;div&gt;答案：你有一个integer stream,每个元素都是unique的&lt;br&gt;定义一x&lt;br&gt;然后开始接受这个stream...&lt;br&gt;&lt;br&gt;第一个 1/1 几率保存到 x&lt;br&gt;第二个 1/2 几率保存到 x&lt;br&gt;第三个 1/3 几率保存到 x&lt;br&gt;第k格 1/k 几率保存到 x&lt;br&gt;........&lt;br&gt;&lt;br&gt;最后输出x...&lt;br&gt;&lt;br&gt;可以在不知道n的情况下，保证每个元素几率均等的机会输出。。&lt;/div&gt;
&lt;div&gt; &lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+%e5%8f%a4%e8%80%81%e7%9a%84%e9%9d%a2%e8%af%95%e9%a2%98&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!377.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!377.entry</guid><pubDate>Sun, 19 Aug 2007 11:18:21 GMT</pubDate><slash:comments>2</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!377/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!377.entry#comment</wfw:comment><dcterms:modified>2007-08-19T11:18:21Z</dcterms:modified></item><item><title>旋转数组</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!373.entry</link><description>&lt;div&gt;Q:给一个n元数组 &lt;img alt="x_1, x_2, \cdots, x_n" src="http://zhiqiang.org/bbs/bb-cache/tex_53fd294956c125200888c950a8c4e9fd.gif" align=absMiddle&gt;，和k，使用常熟空间和线性时间，使得数组变成 &lt;img alt="x_k,x_{k+1}, \cdots, x_n, x_1, \cdots, x_{k-1}" src="http://zhiqiang.org/bbs/bb-cache/tex_195308498404d4a079aa1a7089a9ea47.gif" align=absMiddle&gt; &lt;/div&gt;
&lt;div&gt;A:&lt;/div&gt;
&lt;div&gt;O(n):&lt;br&gt;a(1),a(2),,,a(k-1),a(k),,,a(n)-&amp;gt;&lt;br&gt;a(k-1),,,a(2),a(1),a(n),,,a(k)-&amp;gt;&lt;br&gt;a(k),,,a(n),a(1),a(2),,,a(k-1)&lt;/div&gt;
&lt;div&gt;---------------------------------------------------------------------------------------&lt;/div&gt;
&lt;div&gt;Q:在一个旋转过的有序数组（比如3 4 5 1 2）上实现二分查找。&lt;/div&gt;
&lt;div&gt;A:&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;p&gt;example:&lt;br&gt;11 12 13 14 15 16 17 18 19 20 4 5 6 7 8 9 10&lt;br&gt;suppose ascending from left to right&lt;br&gt;a_0 &amp;gt; a_n&lt;br&gt;a_m: middle position, m = (0 + n)/2&lt;br&gt;a_m &amp;gt; a_0 --&amp;gt; the left partial is ascending&lt;br&gt;a_m &amp;lt; a_n --&amp;gt; the right partial is ascending
&lt;p&gt;b = 5&lt;br&gt;a_m = 19&lt;br&gt;a_m &amp;gt; a_0 &amp;gt; a_n &amp;gt; b --&amp;gt; b sits in the right partial
&lt;p&gt;19 20 4 5 6 7 8 9 10
&lt;p&gt;a_0 &amp;gt; a_n &amp;gt; a_m &amp;gt; b --&amp;gt; b sits in the left partial
&lt;p&gt;19 20 4 5 6
&lt;p&gt;a_0 &amp;gt; a_n &amp;gt; b &amp;gt; a_m --&amp;gt; b sits in the right partial
&lt;p&gt;4 5 6
&lt;p&gt;a_n == b &lt;/div&gt;&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+%e6%97%8b%e8%bd%ac%e6%95%b0%e7%bb%84&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!373.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!373.entry</guid><pubDate>Sat, 18 Aug 2007 07:56:39 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!373/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!373.entry#comment</wfw:comment><dcterms:modified>2007-08-18T07:56:39Z</dcterms:modified></item><item><title>Max square martix</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!372.entry</link><description>&lt;div&gt; Imagine there is a square matrix with n x n cells. Each cell is either filled with a black pixel or a white pixel. Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels;&lt;/div&gt;
&lt;div&gt;Can this be done in O(n^2) ?&lt;/div&gt;
&lt;div&gt; &lt;/div&gt;
&lt;div&gt;Here's an O(n^2) solution.  It can probably be cleaned up and made more elegant, but as a proof of concept it will do.  Although the algorithm isn't very complicated, the explanation may appear so.  It's the best I could do.  I apologize.&lt;/div&gt;
&lt;div&gt;1. Reduction to a 1-dimensional problem.&lt;br&gt;The upper-left and lower-right corners of any square will both be situated on one of the matrix's diagonals.  We therefore consider each of the 2n-1 diagonals, performing the same linear time algorithm  each.  (Note that the length of each diagonal is at most n.)  We then select the maximum result obtained in these 2n-1 invocations.&lt;/div&gt;
&lt;div&gt;2. Preprocessing.&lt;br&gt;In order to be able to run the diagonal algorithm efficiently, we first compute the following.  For each matrix entry (i,j), we compute R[i,j] as the (maximum) number of contiguous black entries on the ith row, starting at column j and going right.  We similarly compute D[i,j], which counts black squares vertically downwards.  We then compute F[i,j]=min{R[i,j],D[i,j]}.  For example, (using + for black and = for white --- I'm hoping this will give a monospace effect) if the matrix is:&lt;br&gt;=+++=+=&lt;br&gt;++===+=&lt;br&gt;=+=++==&lt;br&gt;+++++==&lt;br&gt;++++=++&lt;br&gt;=======&lt;br&gt;+++++++&lt;/div&gt;
&lt;div&gt;Then R is:&lt;br&gt;0321010&lt;br&gt;2100010&lt;br&gt;0102100&lt;br&gt;5432100&lt;br&gt;4321021&lt;br&gt;0000000&lt;br&gt;7654321,&lt;/div&gt;
&lt;div&gt;D is:&lt;br&gt;0511020&lt;br&gt;1400010&lt;br&gt;0303200&lt;br&gt;2222100&lt;br&gt;1111011&lt;br&gt;0000000&lt;br&gt;1111111,&lt;/div&gt;
&lt;div&gt;and F is:&lt;br&gt;0311010&lt;br&gt;1100010&lt;br&gt;0102100&lt;br&gt;2222100&lt;br&gt;1111011&lt;br&gt;0000000&lt;br&gt;1111111&lt;/div&gt;
&lt;div&gt;We are only really interested in F.  The meaning of F is: the maximum side-length of a square whose upper-left corner is (i,j) and whose upper and left borders are black is F[i,j].&lt;/div&gt;
&lt;div&gt;The computation of R, D, and F is a straightforward dynamic programming exercise, and takes O(n^2) time.&lt;/div&gt;
&lt;div&gt;In a symmetrical manner we compute in O(n^2) time a matrix B whose meaning is: the maximum side-length of a square whose lower-right corner is (i,j) and whose lower and right borders are black is B[i,j].&lt;/div&gt;
&lt;div&gt;The key point is that a square whose upper-left corner is (i,j) and whose lower right corner is (i+s-1,j+s-1)  (where s&amp;gt;=1) is black-bordered iff the F[i,j]&amp;gt;=s and B[i+s-1,j+s-1]&amp;gt;=s.&lt;/div&gt;
&lt;div&gt;3. Preprocessing for a given diagonal.&lt;br&gt;Given a diagonal, we copy the corresponding diagonal out of F and B and obtain two one-dimensional arrays, which I will also denote by F and B.  A square whose diagonally opposing corners are at positions i and j (where i &amp;lt;= j) down the diagonal is black-bordered iff F[i] &amp;gt;= j-i+1 and B[j] &amp;gt;= j-i+1.  Let us transform F and B by:&lt;br&gt;F[i] &amp;lt;- i+F[i]-1;&lt;br&gt;B[i] &amp;lt;- i-B[i]+1;&lt;br&gt;for all i.&lt;br&gt;With these new F and B, a square whose diagonally opposing corners are at positions i and j down the diagonal (where i&amp;lt;=j) is black bordered iff F[i]&amp;gt;=j and B[j]&amp;lt;=i.&lt;/div&gt;
&lt;div&gt;(Of course it is not necessary to actually copy out the diagonals from the 2-dimensional arrays.  We can access them in place instead.  Also, instead of computing F and B as described and then transforming them as above, we can have the dynamic programming compute the transformed values directly.)&lt;/div&gt;
&lt;div&gt;4. The diagonal algorithm.&lt;br&gt;So the one-dimensional problem we wish to tackle is the following.&lt;br&gt;Given two arrays F and B, both of length t (where t &amp;lt;= n) and both containing non-negative integers, find a *good* pair (i,j) that maximizes j-i.  A pair (i,j) is said to be good if  1 &amp;lt;= i &amp;lt;= j &amp;lt;= t,  F[i] &amp;gt;= j,  and  B[j] &amp;lt;= i.&lt;/div&gt;
&lt;div&gt;Here's an O(t) algorithm for the problem.&lt;/div&gt;
&lt;div&gt;The algorithm makes use of a Range-Minimum Query (RMQ) data structure with the following properties:&lt;br&gt;Given an array A[1..m] of numbers, there is a construction stage in which some internal data structures are created.  This stage takes O(m) time.  Once it is completed, range-min queries RM(i,j) can be answered in O(1) time per query.  An RM(i,j) query (for i &amp;lt;= j) asks for the index k that minimizes A[k] in the range i &amp;lt;= k &amp;lt;= j.&lt;br&gt;A simple and elegant implementation of such a data structure is described in a paper by Bender and Farach-Colton (BF-C).&lt;/div&gt;
&lt;div&gt;We construct the BF-C data structure on the array B.&lt;br&gt;Consider an index i.  Suppose we are given some k&amp;gt;=i and are asked to find the largest j&amp;gt;=k such that F[i]&amp;gt;=j and B[j]&amp;lt;=i, we can do the following:&lt;/div&gt;
&lt;div&gt;  maxJ = k-1;&lt;br&gt;  while(maxJ &amp;lt; F(i)){&lt;br&gt;      j = RM(maxJ+1, F[i]);&lt;br&gt;      if(B[j] &amp;gt; i)&lt;br&gt;        break;  /* no more candidates */&lt;br&gt;      maxJ = j;&lt;br&gt;  }&lt;/div&gt;
&lt;div&gt;If the desired j exists, we get it in maxJ.  Otherwise, we exit the loop in the first iteration with maxJ=k-1.  Each iteration takes O(1) time.  Each complete iteration increases maxJ.  The number of iterations is therefore bounded by 1+[the number of times maxJ increases], which is at most 1 + (maxJ+1 - k)  (where maxJ in this expression refers to the value of maxJ upon exit from the loop).&lt;/div&gt;
&lt;div&gt;The algorithm proceeds as follows.  Let's enclose the above piece of code in a function find_maxJ(i,k).  We then start by:&lt;/div&gt;
&lt;div&gt;bestI = bestJ = 0;&lt;br&gt;maxJ = 0;&lt;br&gt;i = 1;&lt;/div&gt;
&lt;div&gt;k = max{i, maxJ+1};&lt;br&gt;maxJ = find_maxJ(i, k);&lt;br&gt;if(maxJ-i &amp;gt; bestJ-bestI){&lt;br&gt;  bestI = i;&lt;br&gt;  bestJ = maxJ;&lt;br&gt;}&lt;/div&gt;
&lt;div&gt;(Note that because of the initialization, max{i, maxJ+1} = i = 1.)  This will find the best good pair of the form (1,j)  and set bestI and bestJ to reflect this, or leave maxJ=0 if no such good pair exists.&lt;/div&gt;
&lt;div&gt;Now suppose if a good pair exists of the form (i',j) with i'&amp;lt;i then (bestI, bestJ) is the best such pair, and that otherwise bestI=bestJ=0.  Also suppose that maxJ contains the maximum j such that a good pair (i',j) exists for some i'&amp;lt;i (and that, if no such good pair exists, maxJ=0).  We want to find the best good pair of the form (i',j) with i'&amp;lt;=i (and update bestI, bestJ, maxJ accordingly).&lt;/div&gt;
&lt;div&gt;Regardless of the current value of maxJ, the answer can be computed by exactly the same code as above:&lt;/div&gt;
&lt;div&gt;k = max{i, maxJ+1};&lt;br&gt;maxJ = find_maxJ(i, k);&lt;br&gt;if(maxJ &amp;gt;= k &amp;amp;&amp;amp; maxJ-i &amp;gt; bestJ-bestI){&lt;br&gt;  bestI = i;&lt;br&gt;  bestJ = maxJ;&lt;br&gt;}&lt;/div&gt;
&lt;div&gt;The correctness of this in the case maxJ&amp;lt;i (before the code is executed) is straightforward.  The correctness in the case maxJ&amp;gt;=i follows by the following argument.  By definition the situation in this case is that there exists a good pair (i',maxJ) such that i'&amp;lt;i&amp;lt;=maxJ.  Therefore it is impossible for any index j, i&amp;lt;=j&amp;lt;=maxJ to satisfy j-i&amp;gt;maxJ-i'.  Since maxJ-i' is surely not more than bestJ-bestI, it is also impossible for any such j to satisfy j-i&amp;gt;bestJ-bestI.  We can therefore start the search from maxJ+1 (which is the value of k in this case).&lt;/div&gt;
&lt;div&gt;The algorithm is therefore the following.&lt;/div&gt;
&lt;div&gt;bestI = bestJ = 0;&lt;br&gt;maxJ = 0;&lt;br&gt;for(i = 1; i &amp;lt;= t; i++){&lt;br&gt;  k = max{i, maxJ+1};&lt;br&gt;  maxJ = find_maxJ(i, k);&lt;br&gt;  if(maxJ-i &amp;gt; bestJ-bestI){&lt;br&gt;      bestI = i;&lt;br&gt;      bestJ = maxJ;&lt;br&gt;  }&lt;br&gt;}&lt;/div&gt;
&lt;div&gt;All that remains is to analyze the time complexity.  Let us denote by maxJ(i) the value of maxJ at the end of the ith iteration (and maxJ(0)=0).  As we have already seen, the time complexity of the call to find_maxJ in the ith iteration is bounded by:&lt;br&gt;  1 + (maxJ(i)+1 - max{i, maxJ(i-1)+1})&lt;br&gt;which is at most&lt;br&gt;  1 + (maxJ(i)+1 - (maxJ(i-1)+1))&lt;br&gt;which is equal to&lt;br&gt;  maxJ(i)-maxJ(i-1) + 1.&lt;br&gt;Summing over the iterations i=1,2,...,t, yields&lt;br&gt;  maxJ(t)-maxJ(0) + t  &amp;lt;=  t - 0 + t = 2t.&lt;/div&gt;
&lt;div&gt;So the function calls take O(t) time in total.  Everything else takes O(t) time too, so we are done. &lt;br&gt;A.F.&lt;br&gt;Tuesday, July 03, 2007 &lt;br&gt; &lt;br&gt; &lt;br&gt;I forgot to put  &amp;quot;maxJ &amp;gt;=k &amp;amp;&amp;amp; ...&amp;quot; in some of the if() statements above. &lt;br&gt;A.F.&lt;br&gt;Tuesday, July 03, 2007 &lt;br&gt; &lt;br&gt; &lt;br&gt;Having thought about it some more, I realized that the BF-C data structure is not really needed.  I'll write it up when I get some time. &lt;br&gt;A.F.&lt;br&gt;Tuesday, July 03, 2007 &lt;br&gt; &lt;br&gt; &lt;br&gt;Sorry.  The idea I had didn't work out.  It seems that the BF-C data structure is needed after all. &lt;/div&gt;
&lt;div&gt;Sunday, July 08, 2007&lt;/div&gt;
&lt;div&gt;--------------------------------------------------------------------------------------------------------&lt;/div&gt;
&lt;div&gt;all the articles above come from &lt;a href="http://discuss.techinterview.org/"&gt;http://discuss.techinterview.org/&lt;/a&gt;, all rights belong to original owner.&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+Max+square+martix&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!372.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!372.entry</guid><pubDate>Sun, 12 Aug 2007 07:39:09 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!372/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!372.entry#comment</wfw:comment><dcterms:modified>2007-08-12T07:39:09Z</dcterms:modified></item><item><title>string(pattern) algorithm source</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!366.entry</link><description>&lt;div&gt;偶然发现一个有趣的网址&lt;span style="word-spacing:0px;font:12px 'Lucida Grande';text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:pre;letter-spacing:normal;border-collapse:separate"&gt;&lt;a href="http://www-igm.univ-mlv.fr/~lecroq/string/node14.html"&gt;http://www-igm.univ-mlv.fr/~lecroq/string/node14.html&lt;/a&gt; &lt;/span&gt;，包括很多字符串匹配的算法，如kmp, bm, &lt;span style="word-spacing:0px;font:12px/18px Arial;text-transform:none;color:rgb(51,51,51);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;ASS&lt;/span&gt;等， 但不晓得叫什么名字，估计是叫 The Gaspard of Monge institute of electronic and computer science, &lt;span style="word-spacing:0px;font:12px 'Lucida Grande';text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:pre;letter-spacing:normal;border-collapse:separate"&gt;法国的？也许吧。&lt;/span&gt;&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+string(pattern)+algorithm+source&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!366.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!366.entry</guid><pubDate>Sat, 11 Aug 2007 06:12:27 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!366/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!366.entry#comment</wfw:comment><dcterms:modified>2007-08-11T06:23:31Z</dcterms:modified></item><item><title>algorithm rediscuss about shuffle</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!365.entry</link><description>&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;Suppose we have an array&lt;br&gt;&lt;br&gt;a1,a2....an,b1,b2....bn&lt;br&gt;&lt;br&gt;How to change this array to&lt;br&gt;&lt;br&gt;a1,b1,a2,b2....an,bn in O(n)/O(nlgn) time using O(1) space&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;&lt;/span&gt; &lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;the following algorithm is O(nlgn), simple and effective&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;&lt;/span&gt; &lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;nlgn=4*2=8&lt;br&gt;1  2  3  4  5  6  7  8&lt;br&gt;1  2  5  6  3  4  7  8&lt;br&gt;1  5  2  6  3  7  4  8&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;&lt;/span&gt; &lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;nlgn=8*lg8=24&lt;br&gt;1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16   &lt;br&gt;1  2  3  4  9  10  11  12  5  6  7  8  13  14  15  16&lt;br&gt;1  2  9  10  3  4  11  12  5  6  13  14  7  8  15  16&lt;br&gt;1  9  2  10  3  11  4  12  5  13  6  14  7  15  8  16&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;&lt;/span&gt; &lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;and the O(n) solution is much more complex, I will post it when understand&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;&lt;font color="#ff0000"&gt;(update: the so-called O(n) solution is proofed wrong)&lt;/font&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－&lt;/span&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;&lt;br&gt;Given Array A[1...2n]. &lt;br&gt; concepts of number theory!&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;(Warning: long post)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;For this we consider a slightly different problem:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;That of converting the array &lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;a1 a2 ... an b1 b2 ... bn&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;to &lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;b1 a1 b2 a2 .... bn an&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;(instead of the one given in the problem statement)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;Note if we can get an O(n) time O(1) space algorithm for one, then we have an O(n) time O(1) space time algorithm for the other.&lt;/span&gt;&lt;/div&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;
&lt;div&gt;&lt;br&gt;Now assume the elements are c1 c2 ... cn c(n+1) .. c2n&lt;/div&gt;
&lt;div&gt;In the resulting array (which is a permutation of the original) &lt;br&gt;The ith element from the original goes to 2i modulo 2n+1.&lt;/div&gt;
&lt;div&gt;Thus index 1 goes to 2, 2 goes to 4, 4 goes to 8 ... ultimately comes back to 1 (all working modulo 2n+1)&lt;/div&gt;
&lt;div&gt;Note that, it is a permutation which is composed of &amp;quot;cycles&amp;quot; like the ones described above (I think Zekaric made the mistake of assuming there is just one cycle)&lt;/div&gt;
&lt;div&gt;For instance consider the case when n=4&lt;/div&gt;
&lt;div&gt;1 2 3 4 5 6 7 8&lt;/div&gt;
&lt;div&gt;It should become&lt;/div&gt;
&lt;div&gt;5 1 6 2 7 3 8 4&lt;/div&gt;
&lt;div&gt;so 1 -&amp;gt; 5 -&amp;gt; 7 -&amp;gt; 8 -&amp;gt; 4 -&amp;gt; 2 -&amp;gt; 1 (-&amp;gt; means goes to)&lt;br&gt;and 3 -&amp;gt; 6 -&amp;gt; 3&lt;/div&gt;
&lt;div&gt;So the cycles are (1 5 7 8 4 2) and (3 6)&lt;/div&gt;
&lt;div&gt;Assume you had an oracle which told you one element from each cycle. Can you come up with an O(n) time and O(1) space algorithm?&lt;/div&gt;
&lt;div&gt;If we do not have such an oracle, trying to come up with elements from distinct cycles becomes hard.&lt;/div&gt;
&lt;div&gt;This is where number theory comes in:&lt;/div&gt;
&lt;div&gt;In case 2n+1 is a power of 3 = 3^m say, using number theory, we can show that the cycles are exactly&lt;/div&gt;
&lt;div&gt;(1, 2, 2^2, ... ) (powers of 2) (In fact these are all the numbers relatively prime to 3^m, note, working modulo 2n+1, i.e modulo 3^m)&lt;br&gt;(3, 3*2, 3*2^2, ...)  (numbers of the form 3 * 2^j&lt;br&gt;(3^2, 3^2 *2 ,....) numbers of the form 3^2 * 2^j&lt;br&gt;...&lt;br&gt;(3^(m-1),....) numbers of the form 3^(m-1) * 2^j &lt;/div&gt;
&lt;div&gt;(will post a proof of this later if someone is interested)&lt;/div&gt;
&lt;div&gt;Thus the &amp;quot;number theory oracle&amp;quot; has told us that the powers of 3 are guaranteed to be in different cycles! That is all we need!&lt;/div&gt;
&lt;div&gt;So in the special case of 2n+1 being a power of 3, we can give an O(n) time and O(1) space algorithm.&lt;/div&gt;
&lt;div&gt;Given the special case (power of 3) algorithm, we can now make this work for any n: &lt;/div&gt;
&lt;div&gt;In the O(nlogn) solution I gave above, instead of k = n/2, pick k to be a power of 3 close to n and use the special knowledge of cycles to shuffle that left partial array in O(n) time. Now for the right partial array, we have a new n' = (n-k) and repeat finding a k' which is a power of 3 etc. Overall, time is O(n) and space is O(1). &lt;/div&gt;
&lt;div&gt; &lt;/div&gt;
&lt;div&gt;copyrights of all the articles above are belonged to the original owner.&lt;br&gt;------------------------------------------------------------------------------------------------------------&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;&lt;/span&gt;&lt;span style="word-spacing:0px;font:16px Cambria;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate"&gt;more details about the maths are ignored. I prefer the first solution, :) &lt;/span&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+algorithm+rediscuss+about+shuffle&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!365.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!365.entry</guid><pubDate>Sat, 11 Aug 2007 05:45:02 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!365/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!365.entry#comment</wfw:comment><dcterms:modified>2008-05-10T04:18:15Z</dcterms:modified></item><item><title>Algorithm about gas station</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!364.entry</link><description>&lt;div&gt;Q:&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;There is a circular track. There are n gas stations of capacity ci. To go from one station to the next station it takes certain amount of gas gi. For any station the gas available may not be enough to get to the next station. Write a linear time algorithm to find the correct station in which we must start in order to complete one round around the track.&lt;/span&gt; &lt;/div&gt;
&lt;div&gt; &lt;/div&gt;
&lt;div&gt;A:&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;令a_i=c_i-g_i，那么原问题等价于一个圆周上从哪个数a_i开始，所有的连续和都非负。&lt;br&gt;反复进行如下2步操作：&lt;br&gt;i)把连续的正数求和变成一个数，连续的负数求和变成一个数。&lt;br&gt;操作后正负数间隔&lt;br&gt;ii)按车行进的方向，把相邻的正数和负数相加，形成一个新的数。这个操作后圆周上的数会减少一半&lt;br&gt;反复进行上面的操作，直到圆周上的数非负。那么从上面任何一个位置开始都可以了。&lt;br&gt;时间复杂度为n+n/2+n/4+...&amp;lt;2n&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;-----------------------------------------------------------------------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;All the articles above come from yueweitang blog&lt;/span&gt;&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+Algorithm+about+gas+station&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!364.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!364.entry</guid><pubDate>Sat, 11 Aug 2007 02:04:49 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!364/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!364.entry#comment</wfw:comment><dcterms:modified>2007-08-11T02:04:49Z</dcterms:modified></item><item><title>找最短的cover - Amazon面试题</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!363.entry</link><description>&lt;div&gt;Q:&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;Given two arrays A [1..n] and B[1..m], find the smallest window in A that contains all elements of B. That is, find a pair &amp;lt;l,k&amp;gt; such that A[l..k] contains B[1..m] For example, given A = 3,1,5,7,3,5,2 and B = 5,3 then the smallest window is [3,5].&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;&lt;/span&gt; &lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;A:&lt;/span&gt; &lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;O(nlogm+mlogm)&lt;br&gt;整个算法只对A数组进行一次扫描&lt;br&gt;维护m个变量h_1,h_2,...,h_m，h_i表示最靠近当前扫描点的一个序列B_1,B_2,...,B_i的起始位置B_1。&lt;br&gt;同时保存最小的窗口s信息。&lt;br&gt;在整个扫描的过程中不断的更新这些变量。&lt;br&gt;假设在扫描到A_j位置时碰到B_i，这时比较h_{i-1}和h_i，如果h_{i-1}&amp;lt;h_i，那么更新h_i=h_{i-1}。&lt;br&gt;当碰到B_m时计算出j-h_m，和已保存的窗口信息s比较，如果更小，那么更新s的信息。&lt;br&gt;这样就能找出最小的窗口了。&lt;br&gt;然后计算时间复杂度，整个扫描过程O(n),但是每次取出一个数需要判断它是否是B中的数，所以需要logm&lt;br&gt;的时间来二分查找，因为B开始没有排序过，所以需要格外的mlogm时间排序。&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;----------------------------------------------------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;以上问题及回答都来自yueweitang blog&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;-----------------------------------------------------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;现在细想了一下，原作者说的不是很详细。&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;举个例子，A=abcabdcdabddcd, B=abcd,容易看出，有三个比较短的cover在A里面，长度为6，5，6。&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;一开始j=1,h_1=1,这样一直扫描到j=3，h_1=h_2=h_3=1;然后又遇到一个a, 这时h_1=4, 类似的，&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;接着h_2=4;然后碰到d,这时的&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;h数组为4411，这时B数组已经遍历，所以得到第一个长度是6&lt;/span&gt;;然后，遇到c和d，&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;所以更新&lt;/span&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;h数组为4444，得到第二个长度是5；A数组还剩下abddcd，游标j已经为8。&lt;/span&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;继续扫描，遇到a和b，&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;h数组更新为9944，遇到dd，不必理会，因为我们需要的是c。最后遇到cd，&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;h数组更新为9999，得到第三个长度是6。&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;此时A数组已经遍历，算法完成。&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;如果辅助空间只能是O(1)的话，那么少于O(n^2)的时间就很难了。&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="word-spacing:0px;font:15px/18px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;text-align:left"&gt;&lt;/span&gt; &lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+%e6%89%be%e6%9c%80%e7%9f%ad%e7%9a%84cover+-+Amazon%e9%9d%a2%e8%af%95%e9%a2%98&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!363.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!363.entry</guid><pubDate>Sat, 11 Aug 2007 02:00:20 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!363/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!363.entry#comment</wfw:comment><dcterms:modified>2007-08-19T10:22:59Z</dcterms:modified></item><item><title>手套和避孕套</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!354.entry</link><description>&lt;div&gt;
&lt;p&gt;这两个东西在某一类问题里面是同质的。这就是以前说过的只有两个安全套又要4p的问题。（以下文字来自yueweitang）
&lt;p&gt;岛国上流行一种极易接触传染的病&lt;br&gt;一旦染上该病1月后病发身亡&lt;br&gt;但该病可通外科手术治愈 
&lt;p&gt;岛上每个人都有已被传染的可能&lt;br&gt;国王怀疑自己得了该病&lt;br&gt;在该岛上找到了医术最高名的3个医生&lt;br&gt;并要求这3个医生在当天轮流给自己动手术&lt;br&gt;然而已消毒过的手术手套只有2双&lt;br&gt;怎样最安全 
&lt;p&gt;现在把问题一般化，假设岛上有n个医生，还有m个（非医生）居民。现在岛上流行一种极易接触传染的病。每个居民要想治愈传染病，必须得到这n个医生的治疗，治疗的顺序无所谓（先别管这种假设的由来）。怎么安排它们手术的次序和带手套的方案，使得所用的手术手套最少。 
&lt;p&gt;注一：医生也有可能已被传染，故他们之间也要防止相互感染。&lt;br&gt;注二：手套可以套着用…还可以反着用… 
&lt;p&gt;下面的解答作者是JtR，来自水母IQDoor版。 
&lt;p&gt;用{m/2}+{n/2}+{n/4}(m&amp;lt;=n)双手套即可。{}表示向上取整。 
&lt;p&gt;为了叙述方便，假设m是2的倍数，n是4的倍数，不是4的倍数时类似，只是手套的“利用率”没有那么高。可能可以通过优化减少一副到两副。 
&lt;p&gt;1.将医生平均分为两组，分别为A，B。各m/2人；&lt;br&gt;将病人分为三组，人数分别为a＝n/2，b=n/4，c=n/4 
&lt;p&gt;2.将m/2副手套分配给A，n/2副手套分配给a，A中的每一个医生检查a中的每一个病人；&lt;br&gt;检查前均在自己的手套外套上指定给该病人的手套；&lt;br&gt;注意所有的手套均只有一面污染。（假设医生之间也要防止相互感染） 
&lt;p&gt;3.将分配给a的n/2副手套中的一半翻转，两两一组套在一起，污染面互相接触，此时每一组手套内外均无污染；将这n/4组手套分配给b(n/4人）；&lt;br&gt;将剩下的未使用过的n/4只手套分配给c(n/4人）； 
&lt;p&gt;4.A中的每一位医生分别检查b，c中的病人，检查前均在自己的手套外套上指定给该病人的手套（或两只套在一起的手套）；&lt;br&gt;这样A中的医生完成对所有病人的检查，而且医生的手套外侧和指定给病人的手套内侧(注意有n/4是翻转过的)均无污染。 
&lt;p&gt;5.A中所有的医生将各自的手套翻转后给B中的医生戴上；&lt;br&gt;b中的病人把套在一起的手套里面的那只抽出来翻转后套入c组病人的手套中。此时c组的手套每一组相互接触的面都是干净的； 
&lt;p&gt;6.B中所有的医生按前述方法给b，c两组病人检查； 
&lt;p&gt;7.将c组的手套两两分开，把套在外面的手套翻转，这样得到n/2只外侧干净的手套重新分配给a组的病人。 
&lt;p&gt;8.B中的所有医生检查a组的病人。 
&lt;p&gt;这样就完成了所有医生对病人的检查。 
&lt;p&gt;如果医生的数量比病人多只要把医生和病人交换即可。在该问题中二者是对称的。&lt;/div&gt;
&lt;div&gt; &lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+%e6%89%8b%e5%a5%97%e5%92%8c%e9%81%bf%e5%ad%95%e5%a5%97&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!354.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!354.entry</guid><pubDate>Mon, 16 Jul 2007 12:22:36 GMT</pubDate><slash:comments>1</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!354/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!354.entry#comment</wfw:comment><dcterms:modified>2007-07-16T12:22:36Z</dcterms:modified></item><item><title>链表重合点</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!351.entry</link><description>&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;p&gt;两条链表，从他们相交的地方开始重合，找到这个重合点。 
&lt;p&gt;如果输入的链表是可写的，整个算法线形时间，常数空间就行了。关键是把两个链表反向（线形时间，常数空间就可以做到），这时候，两个链表的前一部分是重合的，顺序查找就可找到分叉点。算法完成之后在把链表反向，即可将输入复原。
&lt;p&gt;当两个链表长度相等的时候，一步一步比较就可以了。假设链表A和B，B比A长，指针a从A的第一个节点开始，指针b从B的第(B.length - A.length - 1 )个节点开始，这样一步一步比较也可以找到相交点。&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+%e9%93%be%e8%a1%a8%e9%87%8d%e5%90%88%e7%82%b9&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!351.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!351.entry</guid><pubDate>Sun, 15 Jul 2007 03:03:32 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!351/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!351.entry#comment</wfw:comment><dcterms:modified>2007-07-16T12:04:14Z</dcterms:modified></item><item><title>microsoft_interview_algorithm(2)</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!349.entry</link><description>&lt;div&gt;a:设矩阵为A[m][n]现从A[0][0]进，从A[m-1][0]出，要求经历所有矩阵中的点，且不重复访问任何点，问总共有多少种路径？&lt;/div&gt;
&lt;div&gt; &lt;/div&gt;
&lt;div&gt;b:有一个矩阵，每个元素是一个数，要求，从左上角出发到达右下角，每次只能向右或向下，要求全路线经过的元素和最小，求出路径及这个和的值。要求分别用递归和非递归方法实现之。&lt;/div&gt;
&lt;div&gt; &lt;/div&gt;
&lt;div&gt;以上题目来自yueweitang&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+microsoft_interview_algorithm(2)&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!349.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!349.entry</guid><pubDate>Sun, 15 Jul 2007 02:18:09 GMT</pubDate><slash:comments>1</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!349/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!349.entry#comment</wfw:comment><dcterms:modified>2007-07-15T02:19:08Z</dcterms:modified></item><item><title>google_interview_algorithm(5)</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!348.entry</link><description>&lt;div&gt;
&lt;p&gt;算法题一：Given 1 GB memory, input a file which contians 4 billion integers,&lt;br&gt;output one integer that is not in the file. What if you have only 10 MB&lt;br&gt;memory?
&lt;p&gt;算法题二：There are 100 hundred sorted arrays, and each of them contains 100&lt;br&gt;numbers. Give an algorithm to merge them into a single sorted array, using&lt;br&gt;only one temporary array in the middle steps.
&lt;p&gt;编程题：Input an integer array of size n and an integer k (k&amp;lt;=n), output all&lt;br&gt;subsets of size k. 
&lt;p&gt;以上题目来自著名博客yueweitang。
&lt;p&gt; &lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+google_interview_algorithm(5)&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!348.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!348.entry</guid><pubDate>Sun, 15 Jul 2007 02:09:22 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!348/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!348.entry#comment</wfw:comment><dcterms:modified>2007-07-15T02:09:22Z</dcterms:modified></item><item><title>叠箱子</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!345.entry</link><description>给定n个大小各异的立方体，这些立方体每一个面的面积在1-n之间，将这些立方体堆积成两个塔，问这两个塔的高的最小差是多少? 
假定这个问题为a，有个变体问题为b，b是这样，给定1-n之间的n个数，如何把他分为两列排序数，要求这两列树各自的和之差最少。现在能想到的方法的结果是1。如果n为偶数，如1234，就分为14，23；123456就分为1 45，23 6。如果n为奇数，如12345，就分为12 5，34；1234567就分为12 56，34 7。这样方法是能够达到1的。不过问题a有所不同，比如，12 56和34 7，12是能够同时放在5上面的，同理，34是能同时在7之上的。这样，两堆箱子的高度之差就不是1，而是2。如此类推，12 5，34两堆箱子高度之差就是0。1 45，23 6两堆箱子高度之差就是0。&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+%e5%8f%a0%e7%ae%b1%e5%ad%90&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!345.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!345.entry</guid><pubDate>Sun, 17 Jun 2007 12:51:26 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!345/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!345.entry#comment</wfw:comment><dcterms:modified>2007-06-17T12:51:26Z</dcterms:modified></item><item><title>魔方的计算</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!342.entry</link><description>  用什么数据结构恰当的描述一个n阶魔方？用什么算法来把一个魔方还原？ 
&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+%e9%ad%94%e6%96%b9%e7%9a%84%e8%ae%a1%e7%ae%97&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!342.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!342.entry</guid><pubDate>Fri, 15 Jun 2007 12:15:10 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!342/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!342.entry#comment</wfw:comment><dcterms:modified>2007-06-17T12:35:41Z</dcterms:modified></item><item><title>Codes for Reservoir Sampling</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!337.entry</link><description>不多说，贴图。&lt;div&gt;&lt;table cellspacing="0" border="0"&gt;&lt;tr height="8"&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1pozwUtA94wtai0jVuVoWSzJAH8BbBhLSFOfBj_VSpKRc53LCejhiSEw"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;338&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td width="15"&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1pozwUtA94wtZls4rZ7XNGsJ-FZfC49SJ_Ye7txJeoLh52QELxIMMj8Q"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;339&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td width="15"&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1pDc0lGm7HC2sEWyPVTkzeLKUZYShFGskyeylRCzJcRX3WgRPIRDPzEw"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;340&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td width="15"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+Codes+for+Reservoir+Sampling&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!337.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!337.entry</guid><pubDate>Sun, 10 Jun 2007 08:12:32 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!337/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!337.entry#comment</wfw:comment><dcterms:modified>2007-06-10T08:12:32Z</dcterms:modified></item><item><title>Reservoir Sampling</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!333.entry</link><description>There is a link list of numbers of length N. N is very large and you don’t know N. You have to write a function that will return k random numbers from the list. Numbers should be completely random. http://www.ddj.com/dept/architect/184404464 //几种抽样方法. 1.2.1 水库抽样(reservoir sampling) 水库抽样方法 [21] 单遍扫描数据集,生成均匀抽样集合.令样本集合的容量为 S,在任一时刻 n,数据流中的元素都以 S/n 的概率被选取到样本集合中去.如果样本集合大小超出 S,则从中随机去除一个样本.可以证明,各元素的入选几率相同.文献[21]同时推荐了一个技巧来提高算法效率:在原算法中,对于流中的每一个元素都需要 “扔骰子”,判断该元素是否以 S/n 概率被选中;改进的算法转而判断一次需要略过多少个后续元素,从而大大减少了扔骰子的次数. 1.2.2 精确抽样(concise sampling) 在水库抽样方法中,样本集合中各个元素单独占据一个位置,即使它们具有相同的数值,因而表达的效率并不高.举例来说,假设元素 1 出现了 8 次,元素 2 出现了 1 次,则样本集合被表示为(1,1,1,1,1,1,1,1,2,…).精确抽样方法 [22] 改进了样本集合的表示方法.对于仅出现一次的元素,类似于水库抽样,仍然用元素代码表示;而对于多次出现的元素,则利用结构〈value,count〉表示,value 代表元素代码,count 表示样本集合中该元素的数目.这样,在精确抽样中,上面样本集合就表示为(〈1,8〉,2,…).很明显,精确抽样方法比水库抽样方法更节约空间.精确抽样算法维护一个初始值为 1 的概率参数 T,各元素以概率 1/T 加入到样本集合中去.如果该元素已经存在于样本集合中,则相应的计数器加 1(对于仅出现一次的元素,需要改由结构〈value,count〉进行表示,且 count 值为 2);否则,将该元素添加到样本集合中去.一旦样本集合溢出,改变参数 T 到 T′,T′&amp;gt;T.样本集合中的各个元素均以概率 T/T′ 被删除,从而腾出空间以便存放新数据.精确抽样方法通过逐步提高参数 T 的值,实现数据流上的均匀抽样. 1.2.3 计数抽样(counting sampling) 计数抽样方法 [22] 是精确抽样方法的一个变种.二者的区别在于样本集合溢出时如何处理.在计数抽样算法中,当样本集合溢出时,首先将参数 T 提高到 T′.对于其中的任意一个元素,都是首先以概率 T/T′,之后以概率 1/T′ 判断是否减去 1.一旦该计数器值已经降为 0,或者某一次随机判断之后计数器的值并没有减小,则结束对该元素的操作.计数抽样方法不是均匀抽样方法,但却能有效地获得数据集中的热门元素列&lt;div&gt;&lt;table cellspacing="0" border="0"&gt;&lt;tr height="8"&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1ptiJwkC8OEVbvJHullgIz04-2IalrPcX3iC22Ul2WmJf57QgcOAcZ5Q"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;341&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td width="15"&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1pdXUp2q4xZE0A9WYOB5BVqJokGQbOrmv_nrLTVcykvAmlCClk4YGtVg"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;334&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td width="15"&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;p&gt;&lt;a href="http://tkfiles.storage.live.com&amp;#47;y1p64Fk7eBZ0bdlO-toTcHXpPoLoKAtIrRaJLcjdIatTkjDCSwElWBKgQ"&gt;&lt;img src="http://storage.live.com&amp;#47;items&amp;#47;6CAA7F40F084070A&amp;#33;335&amp;#58;thumbnail" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td width="15"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+Reservoir+Sampling&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!333.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!333.entry</guid><pubDate>Sun, 10 Jun 2007 08:07:32 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!333/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!333.entry#comment</wfw:comment><dcterms:modified>2007-06-10T15:44:25Z</dcterms:modified></item><item><title>find kth largest sum</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!329.entry</link><description>Two reverse sorted arrays A and B have been given. such that size of A is m and size of B is n. You need to find the k th largest sum (a+b) where a is taken from A and b is taken from B. such that k &amp;lt; m*n It can be done in O(m*n) but it can also be done in really optimal way.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+find+kth+largest+sum&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!329.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!329.entry</guid><pubDate>Sun, 03 Jun 2007 00:29:03 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!329/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!329.entry#comment</wfw:comment><dcterms:modified>2007-06-03T01:20:11Z</dcterms:modified></item><item><title>一个全排列的判断</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!328.entry</link><description>给一个n长的数组，判断它是否为一个的全排列，要求在线性时间，常数空间内实现。
可以对每个a[i]进行操作：
如果a[i]不在1,n范围内，则不对；
如果a[i] == i，则i++；
如果a[i] &amp;gt; i，判断a[i]和a[a[i]]是否相等，如果不相等则交换，相等则不对；
如果a[i] &amp;lt; i,则不对；
分摊复杂性是O(1),时间复杂度是O(n），还是in place algorithm&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+%e4%b8%80%e4%b8%aa%e5%85%a8%e6%8e%92%e5%88%97%e7%9a%84%e5%88%a4%e6%96%ad&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!328.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!328.entry</guid><pubDate>Sat, 02 Jun 2007 14:43:02 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!328/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!328.entry#comment</wfw:comment><dcterms:modified>2007-06-02T14:43:02Z</dcterms:modified></item><item><title>remove the duplicate entries</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!327.entry</link><description>Write an algorithm to remove the duplicate entries from a sorted, null-terminated array of integers.
                        
1|2|2|2|3|3|5|7|7|7|8|8|8|9|9|9|10|10|11|null


void remove_dupes(int ints[])
{
    int i = 0;
    int j = 0;
    while(ints[i]!=0)
    {
        ++i; ++j;
        while(ints[j]==ints[j-1])
        {
            ++j;
        }
        ints[i] = ints[j];
    }
}&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+remove+the+duplicate+entries&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!327.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!327.entry</guid><pubDate>Sat, 02 Jun 2007 09:30:07 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!327/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!327.entry#comment</wfw:comment><dcterms:modified>2007-06-02T09:30:07Z</dcterms:modified></item><item><title>Largest sum sub-matrix  </title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!326.entry</link><description>Largest sum sub-matrix

This is very similar to the largest sum sub list but here goes: 
 
Given a matrix filled with +ve and -ve Integers..  
now find the largest possible continous sub-matrix having maximum sum... 
 
matrix or the sub-matrix need not be a square matrix..

I have a dynamic programming solution. The complexity is O(m*n*n), which is not a significant improvement over the brute force method whose complexity is O(m*m*n*n).

Let a[m][n] be the 2D array. Let M[i, p, q] be the maximum sum of all sub-matrices with bottom row a[i][p:q], then the following optimal substructure exists:

M[i, p, q] = max{ M[i-1, p, q] + sum(a[i][p:q]), sum(a[i][p:q]) };
0 &amp;lt;= i &amp;lt; m;
0 &amp;lt;= p &amp;lt; n;
p &amp;lt;= q &amp;lt; n;

OK, this is exactly what we did to find the maximum subarray for 1D case. The next thing to do is to define and calculate the following:
L[i, p, q] = sum(a[i][p:q]).

Now the program is:

int maxSumSubMatrix( int *a, int m, int n )
{
    int *b = new int[ m * n ];
    int i, p, q;

    for( i = 0; i &amp;lt; m; ++i ){
        b[ i * n ] = a[ i * n ];
        for( p = 1; p &amp;lt; n; ++p ){
            b[ i * n + p ] = a[ i * n + p ];
            b[ i * n + p ] += b[ i * n + p - 1 ];
        }
    }

    int *L = new int[ m * n * n ];

    for ( i = 0; i &amp;lt; m; ++i ) {
        for( q = 0; q &amp;lt; n; ++q )
            L[ i * n * n + q ] = b[ i * n + q ];

        for( p = 1; p &amp;lt; n; ++p ){
            for( q = p; q &amp;lt; n; ++q ){
                L[ i * n * n + p * n + q ] = b[ i * n + q ] - b[ i * n + p - 1 ];
            }
        }
    }

    int *M = new int[ m * n * n ];
    int maxSum = -65535;

    for ( p = 0; p &amp;lt; n; ++p ){
        for ( q = 0; q &amp;lt; n; ++q ){
            M[ p * n + q ] = L[ p * n + q ];
            if( maxSum &amp;lt; M[ p * n + q ] )
                maxSum = M[ p * n + q ];
        }
    }

    for ( i = 1; i &amp;lt; m; ++i ){
        for ( p = 0; p &amp;lt; n; ++p ){
            for ( q = p; q &amp;lt; n; ++q ){
                if( M[ (i-1) * n * n + p * n + q ] &amp;gt; 0 ){
                    M[ i * n * n + p * n + q ] = M[ (i-1) * n * n + p * n + q ] + L[ i * n * n + p * n + q ];
                }
                else {
                    M[ i * n * n + p * n + q ] = L[ i * n * n + p * n + q ];
                }

                if ( maxSum &amp;lt; M[ i * n * n + p * n + q ] )
                    maxSum = M[ i * n * n + p * n + q ];
            }
        }
    }

    return maxSum;
}

The driver is:
int main()
{
    int a[] = {  8,  5, -1, -2,
                -3, -1,  -5, 8,
                 1,  3, -2,  2 };

    cout &amp;lt;&amp;lt; maxSumSubMatrix( a, 3, 4 );
    cout &amp;lt;&amp;lt; endl;
    return 0;
}

Running result = 13.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+Largest+sum+sub-matrix++&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!326.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!326.entry</guid><pubDate>Sat, 02 Jun 2007 09:29:13 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!326/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!326.entry#comment</wfw:comment><dcterms:modified>2007-06-02T09:29:13Z</dcterms:modified></item><item><title>google_interview_algorithm(5)</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!317.entry</link><description>
A queue (FILO) and a stack (FIFO) can be made from each other. 

Qs:
---Phone Numner -
You need to check that your friend, Bob, has your correct phone number, but you cannot ask him directly. You 

must write a the question on a card which and give it to Eve who will take the card to Bob and return the 

answer to you. What must you write on the card, besides the question, to ensure Bob can encode the message 

so that Eve cannot read your phone number? 

---Special Debugging -
You are given a the source to a application which is crashing when run. After running it 10 times in a debugger, 

you find it never crashes in the same place. The application is single threaded, and uses only the C standard 

library. What programming errors could be causing this crash? How would you test each one? 

---Next Prime -
Given a number, describe an algorithm to find the next number which is prime. 

---O(n)- 
Order the functions in order of their asymptotic performance :
 2^n, 
 n^Googol ( 10^100), 
 n! , 
 n^n , 

---BST- 
what is the best and worst performance time for a hash tree and binary search tree? 

---String -
What is the best and worst time for the operation 'equals'

How do you speed up the 'equals' operation

Write a function (and dictate it to your interviewer) that reverse the order of words in a sentence. The final 

algorithm should work in-place!! E.g.: &amp;quot;This is a sample&amp;quot; --&amp;gt; &amp;quot;sample a is This&amp;quot; 

---Multi Threading- 
Trade offs between a multi threaded and single threaded implementation ?
N threads .. n resources .. what protocol do you use to ensure no deadlocks occur? 

There are a set of 'n' integers. Describe an algorithm to find for each of all its subsets of n-1 integers the 

product of its integers. 

---Sorting- 
Describe Sorting algorithms and their complexity - Quick sort, Insertion Sort, Merge Sort,Shell,Radix,Heap

If you had a million integers how would you sort them and how much memeory would that consume? 

---Check for Square- 
Describe an alogrithm to find out if an integer is a square? 

---Adwords -
How would you determine if adwords was up and running and serving contextual content ? 

---N*N Matrix -
Suppose you have an NxN matrix of positive and negative integers. Write some code that finds the sub-matrix 

with the maximum sum of its elements. 

---Division -
Implement division (without using the divide operator, obviously). 

---Infinite Queries -
You have a stream of infinite queries (ie: real time Google search queries that people are entering). Describe 

how you would go about finding a good estimate of 1000 samples from this never ending set of data and then 

write code for it. 

---Trees- 
Tree search algorithms. Write BFS and DFS code, explain run time and space requirements. Modify the code to 

handle trees with weighted edges and loops with BFS and DFS, make the code print out path to goal state. 

---Minimum of List- 
You are given a list of numbers. When you reach the end of the list you will come back to the beginning of the 

list (a circular list). Write the most efficient algorithm to find the minimum # in this list. Find any given # in the 

list. The numbers in the list are always increasing but you don't know where the circular list begins, ie: 38, 40, 

55, 89, 6, 13, 20, 23, 36. 

---Google Home Server- 
Design a web server that serves the Google homepage. a) What are the requirements. b) Design a multi 

threaded web server that uses shared memory instead of disk access. c) Design a work load balancer for their 

data centers. d) Design a DNS server that returns the IP address of a data center that has the best connectivity 

relative to your IP. 

---Nth last- 
How would you find the nth to last element in a linked list? 
 
---Card Shuffle- 
Explain algorithm to shuffle cards 
---Jigsaw puzzle -
Coding: Jig saw puzzle. What are the data structures? Assume you have some method which can tell you if two 

pieces fit together. How would you solve the puzzle, minimizing the number of times you have to call that 

function? 
 
---Parsing phone numbers- 
You have 50,000 html files, some of which contain phone numbers. How would you create a list of all the files 

which contain phone numHow would you detect a repeated element in an integer array. 
 
---Smoothening the curve -
Write a function to smooth out stock fluctuations. 

---Union -
Given two sets, Write a function to provide the union of them. 

---Most viewed -
Design an algorithm to calculate the most user viewed pages 

---BST Serialization- 
Design an algorithm and write code to serialize a binary tree. 

---Numbers with Sum -
Design an algorithm and write code to find two numbers in an array whose sum equals a given value 
Bits in Integer 

---count bits in an integer.- 

---Similar Trees- 
Given two binary trees, find whether or not they are similar. 

---Random character from stream- 
assume your computer is reading characters one by one from a stream (you don't know the length of the 

stream before ending). Note that you have only one character of storage space (so you cann't save the 

characters you've read to a something like a strong). When you've finished reading you should return a 

character out of the stream with equal probability. 

---Print Tree- 
You have a tree (not Binary) and you want to print the values of all the nodes in this tree level by level.bers? 

&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+google_interview_algorithm(5)&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!317.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!317.entry</guid><pubDate>Thu, 31 May 2007 03:41:33 GMT</pubDate><slash:comments>1</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!317/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!317.entry#comment</wfw:comment><dcterms:modified>2007-06-01T09:20:11Z</dcterms:modified></item><item><title>Microsoft_interview_stack</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!316.entry</link><description>How to implement 3 stacks in a single array as efficiently as possible ?

w1: 3个变量分别当作三个stack的sp,初始化为a a+1 a+2(a[]就是这个数组），push就加3，pop就减3.这个方法最坏情况会浪费2/3的空间。
w2: 两个stack分别在两端向中间生长，第三个在array中间位置交替向两边生长。这个方法最坏情况会浪费1/2的空间。
w3: 首先把array分为若干块，一块大小为k，块的两端用做链接。第一个stack在0位置出分配一块空间，stack在快空间内可以简单快速操作；第二个stack在k+1处分配一个，第三个在2k+1处分配一个。最后在3k+1处做个记号，表示记号右边的空间都没有分配。如果哪个stack用完空间了就申请一个，把新空间和旧空间连接起来。这样子最多浪费2k大小的空间，如果k大小得当，那么操作速度和空间浪费都能有效控制。&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+Microsoft_interview_stack&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!316.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!316.entry</guid><pubDate>Wed, 30 May 2007 03:22:01 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!316/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!316.entry#comment</wfw:comment><dcterms:modified>2007-05-30T03:22:01Z</dcterms:modified></item><item><title>Why Mathematicians Now Care About Their Hat Color</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!315.entry</link><description>BERKELEY, Calif., April 9 — It takes a particularly clever puzzle to stump a mind accustomed to performing mental gymnastics.

So it's no ordinary puzzle that's spreading through networks of mathematicians like a juicy bit of gossip. Known as &amp;quot;the hat problem&amp;quot; in its most popular incarnation, this seemingly simple puzzle is consuming brain cycles at universities and research labs across the country and has become a vibrant topic of discussion on the Internet.

The reason this problem is so captivating, mathematicians say, is that it is not just a recreational puzzle to be solved and put away.

Rather, it has deep and unexpected connections to coding theory, an active area of mathematical research with broad applications in telecommunications and computer science.

In their attempts to devise a complete solution to the problem, researchers are proving new theorems in coding theory that may have applications well beyond mathematical puzzles.

&amp;quot;This puzzle is unique since it connects to unsolved mathematical questions,&amp;quot; said Dr. Joe Buhler, deputy director of the Mathematical Sciences Research Institute here and a hat problem enthusiast.

The hat problem goes like this:

Three players enter a room and a red or blue hat is placed on each person's head. The color of each hat is determined by a coin toss, with the outcome of one coin toss having no effect on the others. Each person can see the other players' hats but not his own.

No communication of any sort is allowed, except for an initial strategy session before the game begins. Once they have had a chance to look at the other hats, the players must simultaneously guess the color of their own hats or pass. The group shares a hypothetical $3 million prize if at least one player guesses correctly and no players guess incorrectly.

The same game can be played with any number of players. The general problem is to find a strategy for the group that maximizes its chances of winning the prize.

One obvious strategy for the players, for instance, would be for one player to always guess &amp;quot;red&amp;quot; while the other players pass. This would give the group a 50 percent chance of winning the prize. Can the group do better?

Most mathematicians initially think not. Since each person's hat color is independent of the other players' colors and no communication is allowed, it seems impossible for the players to learn anything just by looking at one another. All the players can do, it seems, is guess.

&amp;quot;I tell someone the problem and they think they don't have the conditions right,&amp;quot; said Dr. Peter Winkler, director of fundamental mathematics research at Bell Labs of Lucent Technologies in Murray Hill, N.J. &amp;quot;But if you try to prove it's impossible, it doesn't quite work.&amp;quot;

Mathematicians credit the problem to Dr. Todd Ebert, a computer science instructor at the University of California at Irvine, who introduced it in his Ph.D. thesis at the University of California at Santa Barbara in 1998.

Dr. Ebert said he discovered the problem's appeal only recently, when he offered extra credit to his students for solving a seven-player version he called the &amp;quot;seven prisoners puzzle.&amp;quot;

Next thing he knew, the problem was posted on Internet news groups and in chat rooms. &amp;quot;I started getting e-mail from all over the country,&amp;quot; Dr. Ebert said.

Meanwhile, Dr. Winkler, a well- known collector and distributor of clever puzzles, heard the problem from a colleague and spread it widely. It has cropped up at Microsoft Research in Redmond, Wash., at Hewlett-Packard Laboratories in Palo Alto, Calif., and at mathematics, statistics and computer science departments at universities across the country.

The problem has even spread to the Caribbean. At a workshop at a research institute in Barbados, one hardy group of theoretical computer scientists stayed up late one rum- soaked night, playing a drinking game based on the puzzle.

It spread to Berkeley after Dr. Winkler bumped into Dr. Elwyn Berlekamp, a professor in the Berkeley math department, at a conference in New Orleans in January.

&amp;quot;I told him about the problem and next thing I knew he was leaving messages on my hotel phone saying, `Great problem, haven't gotten it yet,' then finally, `I got it,' &amp;quot; Dr. Winkler said. &amp;quot;I thought, with his knowledge of coding theory, he'd find that approach, and he didn't disappoint me.&amp;quot;

Dr. Berlekamp, a coding theory expert, said he figured out the solution to the simplest case in about half an hour, but he saw the coding theory connection only while he was falling asleep that night.

&amp;quot;If you look at old things that you know from a different angle, sometimes you can't see them,&amp;quot; he said.

The first thing Dr. Berlekamp saw was that in the three-player case, it is possible for the group to win three- fourths of the time.

Three-fourths of the time, two of the players will have hats of the same color and the third player's hat will be the opposite color. The group can win every time this happens by using the following strategy: Once the game starts, each player looks at the other two players' hats. If the two hats are different colors, he passes. If they are the same color, the player guesses his own hat is the opposite color.

This way, every time the hat colors are distributed two and one, one player will guess correctly and the others will pass, and the group will win the game. When all the hats are the same color, however, all three players will guess incorrectly and the group will lose.

&amp;quot;If you look at the total number of guesses made, it's still the case that half are right and half wrong,&amp;quot; Dr. Winkler said. &amp;quot;You only make progress if, when players are guessing wrong, a great many are guessing wrong.&amp;quot;

The strategy gets far more complicated for larger numbers of players.

Still, it all comes down to making sure that most of the time no one is wrong and occasionally everyone is wrong at once.

As it turns out, this requirement can be perfectly met only when the number of players is one less than a power of two (three, seven, 15 and so on.)

For example, in the game with 15 players, there is a strategy for which the group is victorious 15 out of every 16 times they play.

This strategy can be described using elegant mathematical structures known as Hamming codes. Hamming codes, named after Richard Hamming, the mathematician who discovered them, are basic tools studied by engineering students all over the world.

Hamming codes straddle the boundary between two types of mathematical objects: error correcting codes and covering codes.

Error correcting codes, techniques for correcting errors in data sent across noisy channels, are used in everything from cell phones to compact discs. Covering codes can be used to compress data so they take up less space in a computer's memory.

&amp;quot;Hamming codes are perfect structures, a lot like crystals, where you can't move an atom in them or they are completely destroyed,&amp;quot; said Dr. Amin Shokrollahi, chief scientist at Digital Fountain, which uses coding theory to speed up Internet data transmissions. &amp;quot;When you take the hat problem apart and look at its core, you see what you need are exactly Hamming codes.&amp;quot;

When the game is played with fewer than nine players, the optimal solution can be determined using various types of codes. For larger numbers that aren't one less than a power of two, a strategy designed around the Hamming code solution works closer and closer to 100 percent of the time as the number of players grows.

Dr. Hendrik Lenstra, a professor of mathematics at Berkeley, and Dr. Gadiel Seroussi, director of information theory research at HP Labs, have developed a new type of covering code to define an even better strategy for large numbers of players.

While their strategy is the best so far, they don't know that it is always optimal. The optimal solution to the hat problem, for all numbers of players, is still unknown.

&amp;quot;We're still working on it,&amp;quot; Dr. Seroussi said. &amp;quot;And as a consequence of working on this problem, we've got some results in coding theory that are interesting in and of themselves.&amp;quot;

For now, researchers say, it seems unlikely that a solution will have immediate practical applications. Still, one never knows what the future might hold. &amp;quot;My experience is that any mathematics I've done is useful eventually,&amp;quot; Dr. Seroussi said.

Practically useful or not, for some researchers the hat problem has interesting social implications. &amp;quot;I like problems that have philosophical punch lines,&amp;quot; Dr. Berlekamp said, citing two life lessons that can be gleaned from the puzzle:

&amp;quot;The first is that it's O.K. to be wrong as long as you contrive not to be wrong alone,&amp;quot; he said. &amp;quot;The other, more important lesson is a need for teamwork that goes against the grain of most mathematicians. If the evidence suggests someone on your team knows more than you, you should keep your mouth shut.

&amp;quot;Most of us assume that each player's strategy is oriented toward him getting it right, and it's not. It's the whole team.&amp;quot;


http://www.msri.org/people/members/sara/articles/hat.html
http://www.oursci.org/bbs/oursci/showthread.php?threadid=2395&amp;amp;perpage=15&amp;amp;pagenumber=2&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+Why+Mathematicians+Now+Care+About+Their+Hat+Color&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!315.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!315.entry</guid><pubDate>Wed, 30 May 2007 02:55:59 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!315/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!315.entry#comment</wfw:comment><dcterms:modified>2007-05-30T02:55:59Z</dcterms:modified></item><item><title>约瑟夫猜想</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!313.entry</link><description>&lt;div&gt;&lt;br&gt;六个小孩围成一个圆圈，分别给他们标志为1，2，……，6。从第一个小孩开始数数1，数到3的小孩要出列；然后刚出列的小孩的下一个又开始数数1，数到3的小孩又要出列，……这样循环下去，问：1.最后剩下的小孩的标志号是多少？2.依次排出出列的小孩的标志号 &lt;/div&gt;
&lt;div&gt;无论是用链表实现还是用数组实现都有一个共同点：要模拟整个游戏过程，不仅程序写起来比较烦，而且时间复杂度高达O(nm)，当n，m非常大(例如上百万，上千万)的时候，几乎是没有办法在短时间内出结果的。我们注意到原问题仅仅是要求出最后的胜利者的序号，而不是要读者模拟整个过程。因此如果要追求效率，就要打破常规，实施一点数学策略。&lt;/div&gt;
&lt;div&gt;为了讨论方便，先把问题稍微改变一下，并不影响原意：&lt;/div&gt;
&lt;div&gt;问题描述：n个人（编号0~(n-1))，从0开始报数，报到(m-1)的退出，剩下的人继续从0开始报数。求胜利者的编号。&lt;/div&gt;
&lt;div&gt;我们知道第一个人(编号一定是m%n-1) 出列之后，剩下的n-1个人组成了一个新的约瑟夫环（以编号为k=m%n的人开始）:&lt;br&gt;  k  k+1  k+2  ... n-2, n-1, 0, 1, 2, ... k-2&lt;br&gt;并且从k开始报0。&lt;/div&gt;
&lt;div&gt;现在我们把他们的编号做一下转换：&lt;br&gt;k     --&amp;gt; 0&lt;br&gt;k+1   --&amp;gt; 1&lt;br&gt;k+2   --&amp;gt; 2&lt;br&gt;...&lt;br&gt;...&lt;br&gt;k-2   --&amp;gt; n-2&lt;br&gt;k-1   --&amp;gt; n-1&lt;/div&gt;
&lt;div&gt;变换后就完完全全成为了(n-1)个人报数的子问题，假如我们知道这个子问题的解：例如x是最终的胜利者，那么根据上面这个表把这个x变回去不刚好就是n个人情况的解吗？！！变回去的公式很简单，相信大家都可以推出来：x'=(x+k)%n&lt;/div&gt;
&lt;div&gt;如何知道(n-1)个人报数的问题的解？对，只要知道(n-2)个人的解就行了。(n-2)个人的解呢？当然是先求(n-3)的情况 ---- 这显然就是一个倒推问题！好了，思路出来了，下面写递推公式：&lt;/div&gt;
&lt;div&gt;令f[i]表示i个人玩游戏报m退出最后胜利者的编号，最后的结果自然是f[n]&lt;/div&gt;
&lt;div&gt;递推公式&lt;br&gt;f[1]=0;&lt;br&gt;f[i]=(f[i-1]+m)%i;  (i&amp;gt;1)&lt;/div&gt;
&lt;div&gt;有了这个公式，我们要做的就是从1-n顺序算出f[i]的数值，最后结果是f[n]。因为实际生活中编号总是从1开始，我们输出f[n]+1&lt;/div&gt;
&lt;div&gt;由于是逐级递推，不需要保存每个f[i]，程序也是异常简单：&lt;/div&gt;
&lt;div&gt;＃i nclude &amp;lt;stdio.h&amp;gt;&lt;/div&gt;
&lt;div&gt;main()&lt;br&gt;{&lt;br&gt;  int n, m, i, s=0;&lt;br&gt;  printf (&amp;quot;N M = &amp;quot;); scanf(&amp;quot;%d%d&amp;quot;, &amp;amp;n, &amp;amp;m);&lt;br&gt;  for (i=2; i&amp;lt;=n; i++) s=(s+m)%i;&lt;br&gt;  printf (&amp;quot;The winner is %d\n&amp;quot;, s+1);&lt;br&gt;}&lt;/div&gt;
&lt;div&gt;这个算法的时间复杂度为O(n)，相对于模拟算法已经有了很大的提高。算n，m等于一百万，一千万的情况不是问题了。可见，适当地运用数学策略，不仅可以让编程变得简单，而且往往会成倍地提高算法执行效率&lt;/div&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=7830210819026847498&amp;page=RSS%3a+%e7%ba%a6%e7%91%9f%e5%a4%ab%e7%8c%9c%e6%83%b3&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=shitfromthemouth.spaces.live.com&amp;amp;GT1=shitfromthemouth"&gt;</description><comments>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!313.entry#comment</comments><guid isPermaLink="true">http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!313.entry</guid><pubDate>Tue, 29 May 2007 08:55:34 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://shitfromthemouth.spaces.live.com/blog/cns!6CAA7F40F084070A!313/comments/feed.rss</wfw:commentRss><wfw:comment>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!313.entry#comment</wfw:comment><dcterms:modified>2007-05-29T08:55:34Z</dcterms:modified></item><item><title>google_interview_algorithm(4)</title><link>http://shitfromthemouth.spaces.live.com/Blog/cns!6CAA7F40F084070A!312.entry</link><description>传说中一个google的笔试题
                                       

题目:
长度为n的整数数组，找出其中任意(n-1)个乘积最大的那一组的乘积，只能用乘法，不可以用除法。要求对算法的时间复杂度和空间复杂度作出分析，不要求写程序.

很早