<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>-Flyぁ梦- &#187; 数据结构和算法</title>
	<atom:link href="http://blog.11034.org/category/coding/ds-algorithm/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.11034.org</link>
	<description></description>
	<lastBuildDate>Sun, 22 Jun 2025 08:59:05 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>java.util中几个Map的性能测试</title>
		<link>http://blog.11034.org/2013-05/java_map.html</link>
		<comments>http://blog.11034.org/2013-05/java_map.html#comments</comments>
		<pubDate>Mon, 27 May 2013 08:47:35 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[数据结构和算法]]></category>
		<category><![CDATA[hash表]]></category>

		<guid isPermaLink="false">http://blog.stariy.org/?p=1786</guid>
		<description><![CDATA[突然有这么个想法，就写了段代码测试了下。在单线程下对Java里所有Map的性能测试，包括HashMap、Has [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>突然有这么个想法，就写了段代码测试了下。在单线程下对Java里所有Map的性能测试，包括HashMap、Hashtable、LinkedHashMap、IdentityMap、TreeMap，没有测试WeakHashMap。</p>
<p>数据为&lt;Integer, Integer&gt;，范围在0~1千万，共1百万对数据，由Python的random.randint()自动生成。</p>
<p>测试的性能包括写入（put）、随机读（get）和遍历（foreach语法），也是Map这个数据结构最常用的三个操作。其中写入就是将这1百万对测试数据依次put一遍，随机读就是在上一步写入之后再按顺序get一遍。数据显示最后Map大小都为995012（当然IdentityMap的size是1百万），就是有近5000条重复的key。<br />
<span id="more-1786"></span></p>
<p>下面是笔记本上跑出的数据：</p>
<p align="center">Java中各个Map的性能测试（T400笔记本，酷睿P8700/2.46G）数据表</p>
<div align="center">
<table border="0" cellspacing="1" cellpadding="4" bgcolor="silver">
<tbody>
<tr>
<th bgcolor="#FFFFFF" width="150">
<div align="center">Map</div>
</th>
<th bgcolor="#FFFFFF" width="250">
<div align="center">Run Time(ms)</div>
</th>
<th bgcolor="#FFFFFF" width="150">
<div align="center">Type[order]</div>
</th>
</tr>
<tr>
<td rowspan="3" valign="top" bgcolor="#FFFFFF">
<div align="center">HashMap</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">744, 739, 736, 741, 741</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Put[3]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">220, 225, 228, 225, 221</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Get[2]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">239, 218, 213, 209, 210</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Foreach[1]</div>
</td>
</tr>
<tr>
<td rowspan="3" align="center" valign="top" bgcolor="#FFFFFF">
<div align="center">Hashtable</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">694, 726, 699, 685, 686</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Put[2]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">264, 272, 272, 266, 264</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Get[4]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">331, 291, 284, 295, 286</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Foreach[2]</div>
</td>
</tr>
<tr>
<td rowspan="3" valign="top" bgcolor="#FFFFFF">
<div align="center">LinkedHashMap</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">814, 802, 817, 825, 818</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Put[4]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">227, 229, 218, 223, 227</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Get[2]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">481, 466, 472, 475, 473</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Foreach[4]</div>
</td>
</tr>
<tr>
<td rowspan="3" valign="top" bgcolor="#FFFFFF">
<div align="center">IdentityHashMap</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">649, 660, 658, 662, 659</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Put[1]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">181, 186, 188, 191, 182</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Get[1]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">754, 774, 773, 774, 773</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Foreach[5]</div>
</td>
</tr>
<tr>
<td rowspan="3" valign="top" bgcolor="#FFFFFF">
<div align="center">TreeMap</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">1880, 1879, 1876</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Put[5]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">1229, 1234, 1270</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Get[5]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">420, 403, 399, 401, 401</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Foreach[3]</div>
</td>
</tr>
</tbody>
</table>
</div>
<p>然后又在实验室的台式电脑上跑了一下测试，基本结果类似。</p>
<p align="center">Java中各个Map的性能测试（台式机，i3/4G）数据表</p>
<div align="center">
<table border="0" cellspacing="1" cellpadding="4" bgcolor="silver">
<tbody>
<tr>
<th bgcolor="#FFFFFF" width="150">
<div align="center">Map</div>
</th>
<th bgcolor="#FFFFFF" width="250">
<div align="center">Run Time(ms)</div>
</th>
<th bgcolor="#FFFFFF" width="150">
<div align="center">Type[order]</div>
</th>
</tr>
<tr>
<td rowspan="3" valign="top" bgcolor="#FFFFFF">
<div align="center">HashMap</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">422, 423, 438</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Put[2]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">234, 218, 218</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Get[2]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">250, 234, 234</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Foreach[1]</div>
</td>
</tr>
<tr>
<td rowspan="3" align="center" valign="top" bgcolor="#FFFFFF">
<div align="center">Hashtable</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">407, 422, 406</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Put[1]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">249, 250, 250</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Get[4]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">250, 250, 249</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Foreach[1]</div>
</td>
</tr>
<tr>
<td rowspan="3" valign="top" bgcolor="#FFFFFF">
<div align="center">LinkedHashMap</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">610, 594, 594</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Put[4]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">234, 234, 234</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Get[3]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">265, 280, 280</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Foreach[3]</div>
</td>
</tr>
<tr>
<td rowspan="3" valign="top" bgcolor="#FFFFFF">
<div align="center">IdentityHashMap</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">532, 516, 531</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Put[3]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">172, 172, 172</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Get[1]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">327, 328, 312</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Foreach[4]</div>
</td>
</tr>
<tr>
<td rowspan="3" valign="top" bgcolor="#FFFFFF">
<div align="center">TreeMap</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">1437, 1421, 1431</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Put[5]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">1185, 1185, 1186</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Get[5]</div>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<div align="center">327, 328, 343</div>
</td>
<td bgcolor="#FFFFFF">
<div align="center">Foreach[4]</div>
</td>
</tr>
</tbody>
</table>
</div>
<p>与上一份数据相比，区别如下：</p>
<ul>
<li>HashMap和Hashtable的操作结果类似，但数据基本很接近，差异不大</li>
<li>IdentityHashMap的写入速度比HashMap慢了一些，读的速度依旧最快，foreach虽然仍然很慢但基本和TreeMap一致，没有太夸张</li>
</ul>
<p>从上面数据上可以看出一些有趣的地方，大部分都很好理解，倒是有2处不太理解呢。</p>
<h3>HashMap</h3>
<p>HashMap不愧为精心设计的最常用的Map，读写性能基本上都是最优秀的，即使某项不是最最优秀的也相差无多，综合性能是毫无争议的最高。</p>
<p>这边也谈谈HashMap源码中对Object.hashCode()再次hash的内部函数：static int hash(int h) {}。之前看源码的时候也只是知道它二次hash的目的以及全部二进制抑或和移位的实现方法，代码原理完全没看懂，而且也没有深究二次hash的原因直到在面试天猫的时候被问到了，倒是傻了一下含糊了说了一些猜测的原因，提高hash分布均匀这样的。</p>
<p>这里附上源码中对此函数的注释，不是太好理解。个人理解：这个函数用来避免质量差的hashCode，因为HashMap的hash表是2的整数倍大小的，普通的hashCode在低bit位的冲突会发生的特别多。</p>
<pre>/**
 * Applies a supplemental hash function to a given hashCode, which
 * defends against poor quality hash functions. This is critical
 * because HashMap uses power-of-two length hash tables, that
 * otherwise encounter collisions for hashCodes that do not differ
 * in lower bits.
 */</pre>
<h3>Hashtable</h3>
<p>相信应该没有多少代码会用到Hashtable了，但不得不惊讶于其单论写入（put）性能竟然优于HashMap，虽然快了没多少基本可以忽略，但比较与读和遍历上要慢上一些，这不能不说很神奇，而且从源码层面一直没怎么看出来原因在哪里。因为synchronized修饰包含占用锁的消耗，速度慢一些是好理解的，当然代码上比HashMap要简单一些调用的子函数也少一些但这不足以使得性能会优于HashMap啊，不得其解，求指导~</p>
<h3>LinkedHashMap</h3>
<p>LinkedHashMap从HashMap继承而来，自然速度会慢一些些。主要重写了内部addEntry()方法（主要写操作put时候被调用）来维护插入顺序链表，所以写入速度比HashMap慢是好理解的；默认内部变量accessOrder == false表示不会在get()方法调用时使用LRU策略修改链表，所以默认随机读的性能和HashMap完全一致；至于foreach遍历，HashMap走的是hash table数组，LinkedHashMap走的是链表，指针操作比数组操作慢了一些。</p>
<h3>IdentityHashMap</h3>
<p>IdentityHashMap和以上几个Map使用的冲突策略不同为线性探测法，所有的冲突操作由邻接链表的指针操作转化为数组操作，所以随机读速度比HashMap快了不少，至于写的话大概差不多（两份测试数据不一致），但是foreach遍历却这么慢有点想不通了源代码也没看出所以然来。</p>
<h3>TreeMap</h3>
<p>这个就不用多说了，时间复杂度都不一样的，实现巨复杂的红黑树，当然速度非常慢了，不过它的foreach遍历效率相对而言是比较高的，因为Tree中邻接节点是有连接关系的，和其他Map相差并不多。</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;分割线&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>再把上面提到的2个问题列举下：</p>
<ol>
<li>为什么Hashtable单论写入速度比HashMap快？（<span style="color: #0000ff;">不同机器、JVM、JDK版本会导致运行结果差异，所以问题意义不大</span>）</li>
<li>为什么IdentityHashMap的foreach遍历速度却很慢？</li>
</ol>
<div>发博文后，辛苦串叔拿着测试代码和数据在不同的JVM和不同JDK的版本下进行测试，发现结果并不是太一致，所以纠结这快一点慢一点没有太大的意义，可能就是本身Java代码和JVM实现的问题了，所以不再深究啦，大致了解这么一个情况就可以啦。</div>
<h4  class="related_post_title">随便看一看</h4><ul class="related_post"><li>2013-12-05 -- <a target="_blank" href="http://blog.11034.org/2013-12/wp-sns-share_2-8.html" title="wp_sns_share更新至2.8，添加小功能">wp_sns_share更新至2.8，添加小功能</a></li><li>2013-08-10 -- <a target="_blank" href="http://blog.11034.org/2013-08/some_skills_in_java.html" title="学到的一些东西">学到的一些东西</a></li><li>2016-06-11 -- <a target="_blank" href="http://blog.11034.org/2016-06/saint_seiya_lc.html" title="圣斗士星矢-冥王神话">圣斗士星矢-冥王神话</a></li><li>2016-03-01 -- <a target="_blank" href="http://blog.11034.org/2016-03/simpledateformat_thread_not_safe.html" title="jdk中SimpleDateFormat的实例线程不安全">jdk中SimpleDateFormat的实例线程不安全</a></li><li>2013-04-03 -- <a target="_blank" href="http://blog.11034.org/2013-04/array_pointer.html" title="C中的数组指针">C中的数组指针</a></li></ul><h4 class="related_post_title">看看 Java , 数据结构和算法 </h4><ul class="related_post"><li>2016-09-09 -- <a target="_blank" href="http://blog.11034.org/2016-09/64bits_linux_arena_memory.html" title="64位Linux下Java进程堆外内存迷之64M问题">64位Linux下Java进程堆外内存迷之64M问题</a></li><li>2016-08-18 -- <a target="_blank" href="http://blog.11034.org/2016-08/java_concurrency_in_practice.html" title="读java concurrency in practice">读java concurrency in practice</a></li><li>2016-08-05 -- <a target="_blank" href="http://blog.11034.org/2016-08/thread_stop.html" title="线程清理">线程清理</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/futuretask.html" title="FutureTask简单分析和用法">FutureTask简单分析和用法</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/semaphore.html" title="Semaphore简单分析">Semaphore简单分析</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2013-05/java_map.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>判断矩形是否重叠</title>
		<link>http://blog.11034.org/2013-05/rectangle_overlap.html</link>
		<comments>http://blog.11034.org/2013-05/rectangle_overlap.html#comments</comments>
		<pubDate>Tue, 07 May 2013 07:53:13 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[数据结构和算法]]></category>
		<category><![CDATA[矩形]]></category>

		<guid isPermaLink="false">http://blog.stariy.org/?p=1767</guid>
		<description><![CDATA[正常思维下，就会去判断一个矩形的每个点是否在另一个矩形中，然后衍生出4个判断，每个判断都要带上4对大于小于比较 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>正常思维下，就会去判断一个矩形的每个点是否在另一个矩形中，然后衍生出4个判断，每个判断都要带上4对大于小于比较，甚是麻烦。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">boolean</span> judgeOverlap<span style="color: #009900;">&#40;</span>Rect A, Rect B<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Point</span> p <span style="color: #339933;">:</span> all points in A<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>p in B<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>上面这段代码其实有一点问题</strong>，设A的左上右下点为A1和A2，B的为B1和B2，就是：A与B重叠不等价于judgeOverlap(A1, A2, B1, B2) == true （比如当B真包含于A时就返回false了），而是等价于judgeOverlap(A1, A2, B1, B2) == true || judgeOverlap(B1, B2, A1, A2) == true，这里很容易犯bug。</p>
<p>有一个更容易的方法来解决这个问题，反向思维也很容易，就是排除那些A和B不可能重叠的情况，就4种。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">boolean</span> judgeOverlap<span style="color: #009900;">&#40;</span>Rect A, Rect B<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>A.<span style="color: #006633;">right</span> <span style="color: #339933;">&lt;</span> B.<span style="color: #006633;">left</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>A.<span style="color: #006633;">left</span> <span style="color: #339933;">&gt;</span> B.<span style="color: #006633;">right</span> <span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>A.<span style="color: #006633;">top</span> <span style="color: #339933;">&lt;</span> B.<span style="color: #006633;">bottom</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>A.<span style="color: #006633;">bottom</span> <span style="color: #339933;">&gt;</span> B.<span style="color: #006633;">top</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>现在项目中还在使用上面版本的代码跑着，觉得第二种方法应该没有问题但没有测试过也没有应用到项目中去实践。</p>
<h4  class="related_post_title">随便看一看</h4><ul class="related_post"><li>2011-10-20 -- <a target="_blank" href="http://blog.11034.org/2011-10/zoj_100_ac.html" title="ZOJ起步 100AC">ZOJ起步 100AC</a></li><li>2011-03-24 -- <a target="_blank" href="http://blog.11034.org/2011-03/apache_fileupload.html" title="使用Apache FileUpload">使用Apache FileUpload</a></li><li>2011-03-18 -- <a target="_blank" href="http://blog.11034.org/2011-03/blog_first_anniversary.html" title="庆祝博客上线一周年">庆祝博客上线一周年</a></li><li>2010-03-18 -- <a target="_blank" href="http://blog.11034.org/2010-03/ie_is_tragedy.html" title="IE就是个悲剧…">IE就是个悲剧…</a></li><li>2013-05-19 -- <a target="_blank" href="http://blog.11034.org/2013-05/tomcat6.html" title="Tomcat6源码学习">Tomcat6源码学习</a></li></ul><h4 class="related_post_title">看看 数据结构和算法 </h4><ul class="related_post"><li>2013-05-27 -- <a target="_blank" href="http://blog.11034.org/2013-05/java_map.html" title="java.util中几个Map的性能测试">java.util中几个Map的性能测试</a></li><li>2013-01-15 -- <a target="_blank" href="http://blog.11034.org/2013-01/pack_in_zoj.html" title="背包练习小集合">背包练习小集合</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/string.html" title="整理下字符串的一些数据结构和算法">整理下字符串的一些数据结构和算法</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/suffix_array.html" title="一套可用的后缀数组代码">一套可用的后缀数组代码</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/ac_automachine.html" title="一个OOP的AC自动机代码">一个OOP的AC自动机代码</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2013-05/rectangle_overlap.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>背包练习小集合</title>
		<link>http://blog.11034.org/2013-01/pack_in_zoj.html</link>
		<comments>http://blog.11034.org/2013-01/pack_in_zoj.html#comments</comments>
		<pubDate>Mon, 14 Jan 2013 16:14:23 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[数据结构和算法]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[ZOJ]]></category>

		<guid isPermaLink="false">http://blog.stariy.org/?p=1529</guid>
		<description><![CDATA[练习DP就从背包开始吧，比较简单一些，因为套路基本都差不多，适合入门。做提前，推荐上网搜一下《背包九讲》读个一 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>练习DP就从背包开始吧，比较简单一些，因为套路基本都差不多，适合入门。做提前，推荐上网搜一下《背包九讲》读个一遍就大致了解背包题了，顺便再次ym下dd大牛&#8230;做了不少zoj里的背包题目，大概估计下来，但背包大小 &lt;= 10w的情况下，一般都不会超时。</p>
<p><span id="more-1529"></span></p>
<h2>0-1背包</h2>
<p>poj 3624 最基础的0-1背包</p>
<p>zoj 3258 要考虑剩下的背包空间，然后按贪心算法用水去装填</p>
<p>zoj 3264 已经有点分组背包的意思了，不过只要简单处理下二选一即可；要注意w = 0的情况，两次dp[m] = max( dp[m], dp[m &#8211; w] + v )会出错，用临时变量记录dp[m]的值做计算</p>
<p>zoj 3305 要排序后去重，即真包含的情况</p>
<p>zoj 3049 先贪心后背包，即用哪几个特殊物品直接卖掉来凑这个卷轴钱，使得损失最小</p>
<p>zoj 2822 求一个数由不同质数求和而成的种数。一开始没用01背包直接DP一直过不了“不同”质数这一条件。</p>
<h2>完全背包</h2>
<p>zoj 1666 计算钱的凑法</p>
<p>zoj 2014 完全背包取最小值</p>
<p>zoj 2224 多次的完全背包</p>
<p>zoj 2955 完全背包不难，但是背包值太大，看网上用的抽屉原理优化（原理没咋看懂&#8230;看过就忘&#8230;）</p>
<h2>多重背包</h2>
<p>多重背包比较复杂，一般用背包九讲上说的O( V * ∑n[i] )的方法也足够了。</p>
<p>当dp数组存的是0或1的情况下（即只考虑这个背包值是否可以达到，而不保存某最优值），可以模拟完全背包然后用个数组保存背包值-&gt;物品用的次数就可以了，这样简单很多。</p>
<p>zoj 1149 就是看一堆数是否能凑到所有数和的一半</p>
<p>zoj 2189 计算钱的凑法，硬币数量有限，要求硬币数量最少，保存dp路径</p>
<p>zoj 2156 也是计算钱的凑法，不过硬币数量有限而且要取硬币数量最多的表示方法（并不是只用小的货币就一定是最多的硬币数），还要保存dp的路径</p>
<h2>分组背包</h2>
<p>zoj 2972 刘翔跨栏题</p>
<p>zoj 3450 按照坐标原点的射线分组，最大公约数</p>
<h2>综合背包</h2>
<p>zoj 3164 集合0-1背包、完全背包、多重背包、分组背包&#8230;不过0-1背包算是多重背包的一个特殊情况吧</p>
<h4  class="related_post_title">看看 DP , ZOJ</h4><ul class="related_post"><li>2013-01-25 -- <a target="_blank" href="http://blog.11034.org/2013-01/winter_holiday_plan.html" title="zoj 200AC，寒假计划">zoj 200AC，寒假计划</a></li><li>2011-10-20 -- <a target="_blank" href="http://blog.11034.org/2011-10/zoj_100_ac.html" title="ZOJ起步 100AC">ZOJ起步 100AC</a></li></ul><h4 class="related_post_title">看看 数据结构和算法 </h4><ul class="related_post"><li>2013-05-27 -- <a target="_blank" href="http://blog.11034.org/2013-05/java_map.html" title="java.util中几个Map的性能测试">java.util中几个Map的性能测试</a></li><li>2013-05-07 -- <a target="_blank" href="http://blog.11034.org/2013-05/rectangle_overlap.html" title="判断矩形是否重叠">判断矩形是否重叠</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/string.html" title="整理下字符串的一些数据结构和算法">整理下字符串的一些数据结构和算法</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/suffix_array.html" title="一套可用的后缀数组代码">一套可用的后缀数组代码</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/ac_automachine.html" title="一个OOP的AC自动机代码">一个OOP的AC自动机代码</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2013-01/pack_in_zoj.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>整理下字符串的一些数据结构和算法</title>
		<link>http://blog.11034.org/2012-12/string.html</link>
		<comments>http://blog.11034.org/2012-12/string.html#comments</comments>
		<pubDate>Thu, 06 Dec 2012 13:27:52 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[ACM]]></category>
		<category><![CDATA[数据结构和算法]]></category>
		<category><![CDATA[字符串]]></category>
		<category><![CDATA[树]]></category>

		<guid isPermaLink="false">http://blog.stariy.org/?p=1412</guid>
		<description><![CDATA[别看字符串挺简单，还真牵扯到好多数据结构和算法啊，给跪了，要将所有的都好好掌握真心是一项艰难的任务。这里就列举 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>别看字符串挺简单，还真牵扯到好多数据结构和算法啊，给跪了，要将所有的都好好掌握真心是一项艰难的任务。这里就列举下自己做zoj时候碰到过的一些，然后配合自己做过的例题讲一下。不过每种算法、数据结构具体的描述和代码，还请自己去搜索吧。<span id="more-1412"></span></p>
<h2>算法</h2>
<h3>KMP算法</h3>
<p>字符串在普通项目中用得最多的必然是匹配和查询问题了，KMP算法通过O(M)的时间预处理短串，然后在O(N)的时间内搜索长串得到结果，算是最经典的字符串匹配算法了，其中预处理字符串next数组的方法非常具有扩展性。KMP并不太容易懂，推荐百度搜索“Matrix67 KMP”这篇文章，比较好懂。自己写一遍KMP再配合着OJ做一些题，就有感觉了。</p>
<p><strong>zoj 2177 Period</strong> : 由重复子串不重叠构成的所有前缀。这里用到了next函数的思想。</p>
<p><strong>zoj 2177 Crack</strong> : int值的KMP。搜索到串后，再分别往前往后继续匹配，比较最大值。</p>
<p><strong>zoj 3587 Marlon&#8217;s String</strong> : 串A和串B，求出A[i1, j1] + A[i2, j2] = B有多少种。KMP中每一次i与j的匹配都意味着一个前缀的匹配，后缀通过字符串反序同理前缀处理。</p>
<p><strong>zoj 3643 Keep Deleting</strong> : KMP和栈的结合。</p>
<h3>最小表示法</h3>
<p>一个字符串按照字典序的最小表示序列。可以用来判断某些字符串是否循环同构。</p>
<p>一开始看懂了，最近又忘了，不过知道有这个算法，应用起来套进去好像比较容易。</p>
<p><strong>zoj 1729 Hidden Password</strong> : 求字符串的最小表示位于原字符串的位置。</p>
<h3>Manacher算法</h3>
<p>用来求一个字符串中最长回文子串的算法，比用后缀数组的方案既快又节约内存。没怎么看懂，好像也就这么个用途，勉强记得有这个算法吧 =.=。</p>
<p><strong>poj 3974 Palindrome</strong> : 求最长回文子串的长度。</p>
<h3>栈处理</h3>
<p>主要是字符串里的括号匹配的问题，很容易想到用栈。</p>
<p><strong>zoj 1423 (Your)((Term)((Project)))</strong> : 删除多余的括号。</p>
<p><strong>zoj 2483 Boolean Expressions</strong> : 计算布尔表达式的值。</p>
<p><strong>zoj 2704 Brackets</strong> : 判断带圆括号和方括号的表达式是否正确。</p>
<h2>数据结构</h2>
<h3>Trie树</h3>
<p>这算是最简单的了，很容易看懂和应用的数据结构。用来查找某些字符串是否存在于一个字符串集合当中，查找花费O(N)的时间（这里用Java的HashMap和String自带的hashCode方法，也比较不错）。Trie树适合被搜索对象是一个word，搜索其是否整体匹配单词源集合某一个。</p>
<p><strong>zoj 1109 Language of FatMouse</strong> : Trie树最基础应用</p>
<p><strong>zoj 1888 Zipf&#8217;s Law</strong> : 查找一段文章中出现次数为N的单词。也是Trie树普通运用，最后选择遍历Trie树也可以，当然也可以优化下。</p>
<p><strong>zoj 2346 Shortest Prefixes</strong> : N个串，将每个串缩短至最短的前缀串来唯一表示这个串，但在N个串中不能有冲突。</p>
<h3>AC自动机 &#8211; Trie树升级版</h3>
<p>AC自动机，就是在Trie树的基础上给每个节点加一个fail指针，类似KMP的next数组的感觉，每次匹配失败后可以继续向前匹配。为什么叫自动机呢，学过计算理论应该还是比较好理解的。AC自动机的建立，在Trie树已经建立后，加一步建立fail指针的过程，需要通过bfs遍历一遍Trie树。然后搜索的时候没匹配一个节点，都要依次循着fail指针向上查询直到root节点查找是表示为串结尾的节点，这些都是符合的情况。</p>
<p>AC自动机适合处理那些一长段文字中去寻找某些个单词的情况，即适合被搜索对象是一个长串，搜索其部分匹配单词源集合。</p>
<p>附上一份自己写的AC自动机的代码，<a title="一个OOP的AC自动机代码" href="/2012-12/ac_automachine.html" target="_blank">Click here</a>。</p>
<p><strong>zoj 3228 Searching the String</strong> : 可重叠和不可重叠地去查询某个串存在的次数。</p>
<p><strong>zoj 3430 Detect the Virus</strong> : Base64解码后再匹配。题目比较恶心&#8230;然后这里字符集有256个，包括&#8217;\0&#8217;，所以用insert(char *s, int len)比较好，然后很恶心的一点习惯写Java后才发现C++的char默认是signed的&#8230;这里要转换成unsigned char，不然OJ上就不断地SF了。</p>
<h3>后缀树和后缀自动机</h3>
<p>将一个字符串的所有后缀作为不同的串，插入到Trie树形成的树，同理再加上fail指针就是后缀自动机。后缀树和后缀自动机都是对一个长串进行某些处理，比如寻找某些子串、寻找最长回文子串、寻找两个串的某些共同属性（比如最长公共部分等）。但是后缀树算法和程序比较复杂，做题的时候好像很少用到，而是用较为简易的后缀数组的来代替。</p>
<h3>后缀数组</h3>
<p>后缀树的简化版本，用几个数组来记录后缀串的不同属性。网上流行的模板代码里，sa数组按字典顺序记录后缀的序号（ra[rk] = index），rank数组与sa数组互逆，按照后缀序号记录后缀的字典序大小（rank[index] = rk）。然后为了模拟后缀树，还有height数组，记录height[i] = LCP(ra[i &#8211; 1], ra[i])，LCP为Longest Common Prefix（最长公共前缀），即有了后缀树里的Least Common Ancestors（最近公共祖先）的意思。后缀数组开了这么多数组比较耗费内存，所以不适合字符串长度很长（超过一百万）的题目。</p>
<p>建立后缀数组，有两种比较复杂难懂的算法，倍增算法和DC3算法，前者O(NlogN)后者O(N)的复杂度，反正LZ看不懂也不会写用的也是网上找的模板。计算height值是O(N)的算法，也不懂。任意两个后缀的LCP值是它们sa值之间的height值的最小值，这里包含了一个RMQ（Range Minimum Query）问题。</p>
<p>后缀数组的详细可以看一下《后缀数组—处理字符串的有力工具》这篇高中生写的论文，膜拜&#8230;虽然觉得里面某些代码和题解有点问题，不过算法描述很详细很值得去读而理解。</p>
<p>附上一份网上找的蛮好用的后缀数组代码，<a title="一套可用的后缀数组代码" href="/2012-12/suffix_array.html" target="_blank">Click here</a>。</p>
<p><strong>ural 1297 Palindrome</strong> : 求最长回文子串。poj也有类似一题但是数据量太大，可以用前面提到的Manacher算法求解。</p>
<p><strong>zoj 2737 Occurrence</strong> : 求串B所有的循环同构串在串A中出现的次数和。解决循环的办法就是设B&#8217; = B + B，S = B&#8217; + &#8216;$&#8217; + A，然后对S进行后缀数组处理。</p>
<p><strong>zoj 3199 Longest Repeated Substring</strong> : 含有不可重叠且连续子串的子串。</p>
<p><strong>zoj 3296 Connecting the Segments</strong> : 这道题真是牛逼了，结合了后缀数组、RMQ、贪心之最小区间覆盖，很经典很值得一练的题。后缀数组的作用并不是求出所有的回文子串（无法求出被真包含的回文子串，比如aabaa，aabaa本身是回文子串，其中真包含的aba也是回文子串，aba就无法得到），但是不影响区间覆盖。</p>
<p><strong>zoj 3395 Stammering Aliens</strong> : 可重叠子串多于k个。这里需要将height数组分组，很多后缀数组的题目都需要如此处理，原因就是上面提到的LCP取最小值。</p>
<p>学习后缀数组真心花了好多天，一开始只会按照套路和题解去套用后缀数组的方法解题，做了几个题目就慢慢理解后缀数组的意义所在，就能自己想办法去利用后缀数组了。不做题，不coding，理解不了，但是痛苦的是一段时间不做的话又会忘了，这个怎么破 T_T。</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-to be continued&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>&nbsp;</p>
<h4  class="related_post_title">看看 字符串 , 树</h4><ul class="related_post"><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/ac_automachine.html" title="一个OOP的AC自动机代码">一个OOP的AC自动机代码</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/suffix_array.html" title="一套可用的后缀数组代码">一套可用的后缀数组代码</a></li><li>2012-07-03 -- <a target="_blank" href="http://blog.11034.org/2012-07/trie_in_php.html" title="敏感词过滤，PHP实现的Trie树">敏感词过滤，PHP实现的Trie树</a></li></ul><h4 class="related_post_title">看看 ACM , 数据结构和算法 </h4><ul class="related_post"><li>2013-05-27 -- <a target="_blank" href="http://blog.11034.org/2013-05/java_map.html" title="java.util中几个Map的性能测试">java.util中几个Map的性能测试</a></li><li>2013-05-07 -- <a target="_blank" href="http://blog.11034.org/2013-05/rectangle_overlap.html" title="判断矩形是否重叠">判断矩形是否重叠</a></li><li>2013-01-15 -- <a target="_blank" href="http://blog.11034.org/2013-01/pack_in_zoj.html" title="背包练习小集合">背包练习小集合</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/suffix_array.html" title="一套可用的后缀数组代码">一套可用的后缀数组代码</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/ac_automachine.html" title="一个OOP的AC自动机代码">一个OOP的AC自动机代码</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2012-12/string.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>一套可用的后缀数组代码</title>
		<link>http://blog.11034.org/2012-12/suffix_array.html</link>
		<comments>http://blog.11034.org/2012-12/suffix_array.html#comments</comments>
		<pubDate>Thu, 06 Dec 2012 13:14:53 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[数据结构和算法]]></category>
		<category><![CDATA[字符串]]></category>

		<guid isPermaLink="false">http://blog.stariy.org/?p=1420</guid>
		<description><![CDATA[后缀数组的代码算法难度比较高，反正没怎么看也不打算看懂，从网上找了好久找到一份比较好用的代码，效率也比较高，据 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>后缀数组的代码算法难度比较高，反正没怎么看也不打算看懂，从网上找了好久找到一份比较好用的代码，效率也比较高，据说是用的倍增算法？看不懂 =.= <span id="more-1420"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// r : 为字符串转化成的int数组, 0 - len-1 为源字符串, r[len] = 0</span>
<span style="color: #666666;">// sa : 1 - len有效, 值从0到len-1表示后缀序号</span>
<span style="color: #666666;">// rank : 0 - len-1有效, 值从1到len表示后缀字典序大小</span>
<span style="color: #666666;">// height : 2 - len有效, height[i] 为 sa[i]和sa[i-1]的LCP值</span>
&nbsp;
<span style="color: #666666;">// da : n为字符串长度+1, m为字符集大小（用于基数排序）</span>
<span style="color: #666666;">// calh : n为字符串长度</span>
&nbsp;
<span style="color: #339900;">#define MAX 40010</span>
<span style="color: #0000ff;">int</span> height<span style="color: #008000;">&#91;</span>MAX<span style="color: #008000;">&#93;</span>,rank<span style="color: #008000;">&#91;</span>MAX<span style="color: #008000;">&#93;</span>,r<span style="color: #008000;">&#91;</span>MAX<span style="color: #008000;">&#93;</span>,sa<span style="color: #008000;">&#91;</span>MAX<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> ts<span style="color: #008000;">&#91;</span>MAX<span style="color: #008000;">&#93;</span>,ta<span style="color: #008000;">&#91;</span>MAX<span style="color: #008000;">&#93;</span>,tb<span style="color: #008000;">&#91;</span>MAX<span style="color: #008000;">&#93;</span>,tv<span style="color: #008000;">&#91;</span>MAX<span style="color: #008000;">&#93;</span>,pos<span style="color: #008080;">;</span>
<span style="color: #0000ff;">bool</span> cmp<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span>y,<span style="color: #0000ff;">int</span> a,<span style="color: #0000ff;">int</span> b,<span style="color: #0000ff;">int</span> l<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> y<span style="color: #008000;">&#91;</span>a<span style="color: #008000;">&#93;</span><span style="color: #000080;">==</span>y<span style="color: #008000;">&#91;</span>b<span style="color: #008000;">&#93;</span><span style="color: #000040;">&amp;&amp;</span>y<span style="color: #008000;">&#91;</span>a<span style="color: #000040;">+</span>l<span style="color: #008000;">&#93;</span><span style="color: #000080;">==</span>y<span style="color: #008000;">&#91;</span>b<span style="color: #000040;">+</span>l<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #666666;">//计算sa和rank数组</span>
<span style="color: #0000ff;">void</span> da<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n,<span style="color: #0000ff;">int</span> m<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> i,j,<span style="color: #000040;">*</span>x<span style="color: #000080;">=</span>ta,<span style="color: #000040;">*</span>y<span style="color: #000080;">=</span>tb,p<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span>m<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> ts<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span>n<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> ts<span style="color: #008000;">&#91;</span>x<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>r<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #000040;">++</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span>m<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> ts<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">+</span><span style="color: #000080;">=</span>ts<span style="color: #008000;">&#91;</span>i<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span>n<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&gt;=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000040;">--</span><span style="color: #008000;">&#41;</span> sa<span style="color: #008000;">&#91;</span><span style="color: #000040;">--</span>ts<span style="color: #008000;">&#91;</span>x<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>i<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>j<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span>,p<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>p<span style="color: #000080;">&lt;</span>n<span style="color: #008080;">;</span>j<span style="color: #000040;">*</span><span style="color: #000080;">=</span><span style="color: #0000dd;">2</span>,m<span style="color: #000080;">=</span>p<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
        p<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span>n<span style="color: #000040;">-</span>j<span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span>n<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> y<span style="color: #008000;">&#91;</span>p<span style="color: #000040;">++</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>i<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span>n<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>sa<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000080;">&gt;=</span>j<span style="color: #008000;">&#41;</span> y<span style="color: #008000;">&#91;</span>p<span style="color: #000040;">++</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>sa<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span>j<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span>m<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> ts<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span>n<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> tv<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>x<span style="color: #008000;">&#91;</span>y<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span>n<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> ts<span style="color: #008000;">&#91;</span>tv<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #000040;">++</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span>m<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> ts<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">+</span><span style="color: #000080;">=</span>ts<span style="color: #008000;">&#91;</span>i<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span>n<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&gt;=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000040;">--</span><span style="color: #008000;">&#41;</span> sa<span style="color: #008000;">&#91;</span><span style="color: #000040;">--</span>ts<span style="color: #008000;">&#91;</span>tv<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>y<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
        swap<span style="color: #008000;">&#40;</span>x,y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        x<span style="color: #008000;">&#91;</span>sa<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        p<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span>n<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>cmp<span style="color: #008000;">&#40;</span>y,sa<span style="color: #008000;">&#91;</span>i<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span>,sa<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>,j<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> x<span style="color: #008000;">&#91;</span>sa<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>p<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">else</span> x<span style="color: #008000;">&#91;</span>sa<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>p<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #666666;">//计算height数组</span>
<span style="color: #0000ff;">void</span> calh<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> i,k,tmp<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;=</span>n<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> rank<span style="color: #008000;">&#91;</span>sa<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>i<span style="color: #008080;">;</span>
    k<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span>n<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
        tmp<span style="color: #000080;">=</span>sa<span style="color: #008000;">&#91;</span>rank<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">;</span>r<span style="color: #008000;">&#91;</span>i<span style="color: #000040;">+</span>k<span style="color: #008000;">&#93;</span><span style="color: #000080;">==</span>r<span style="color: #008000;">&#91;</span>tmp<span style="color: #000040;">+</span>k<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>k<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span>
        height<span style="color: #008000;">&#91;</span>rank<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>k<span style="color: #008080;">;</span>
        k<span style="color: #008080;">?</span><span style="color: #000040;">--</span>k<span style="color: #008080;">:</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> N, len<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">char</span> line<span style="color: #008000;">&#91;</span>MAX<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">scanf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%d&quot;</span>, <span style="color: #000040;">&amp;</span>N<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">EOF</span> <span style="color: #000040;">&amp;&amp;</span> N <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">getchar</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">gets</span><span style="color: #008000;">&#40;</span>line<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		len <span style="color: #000080;">=</span> <span style="color: #0000dd;">strlen</span><span style="color: #008000;">&#40;</span>line<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i <span style="color: #000080;">&lt;</span> len<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> r<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> line<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">-</span> <span style="color: #FF0000;">'a'</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>	<span style="color: #666666;">// 须保证r数组的值都 &gt; 0</span>
		r<span style="color: #008000;">&#91;</span>len<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
		da<span style="color: #008000;">&#40;</span>len <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">27</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 27表示所有r数组内的值必须小于这个值, r从1到26 (基数排序)</span>
		calh<span style="color: #008000;">&#40;</span>len<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h4  class="related_post_title">看看 字符串</h4><ul class="related_post"><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/string.html" title="整理下字符串的一些数据结构和算法">整理下字符串的一些数据结构和算法</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/ac_automachine.html" title="一个OOP的AC自动机代码">一个OOP的AC自动机代码</a></li><li>2012-07-03 -- <a target="_blank" href="http://blog.11034.org/2012-07/trie_in_php.html" title="敏感词过滤，PHP实现的Trie树">敏感词过滤，PHP实现的Trie树</a></li></ul><h4 class="related_post_title">看看 数据结构和算法 </h4><ul class="related_post"><li>2013-05-27 -- <a target="_blank" href="http://blog.11034.org/2013-05/java_map.html" title="java.util中几个Map的性能测试">java.util中几个Map的性能测试</a></li><li>2013-05-07 -- <a target="_blank" href="http://blog.11034.org/2013-05/rectangle_overlap.html" title="判断矩形是否重叠">判断矩形是否重叠</a></li><li>2013-01-15 -- <a target="_blank" href="http://blog.11034.org/2013-01/pack_in_zoj.html" title="背包练习小集合">背包练习小集合</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/string.html" title="整理下字符串的一些数据结构和算法">整理下字符串的一些数据结构和算法</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/ac_automachine.html" title="一个OOP的AC自动机代码">一个OOP的AC自动机代码</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2012-12/suffix_array.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>一个OOP的AC自动机代码</title>
		<link>http://blog.11034.org/2012-12/ac_automachine.html</link>
		<comments>http://blog.11034.org/2012-12/ac_automachine.html#comments</comments>
		<pubDate>Thu, 06 Dec 2012 12:56:18 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[数据结构和算法]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[字符串]]></category>
		<category><![CDATA[树]]></category>

		<guid isPermaLink="false">http://blog.stariy.org/?p=1416</guid>
		<description><![CDATA[网上代码很多，但是大多ACMer的风格，呃不是我说，代码可读性和封装性是比较欠缺的&#8230;也许Java出 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>网上代码很多，但是大多ACMer的风格，呃不是我说，代码可读性和封装性是比较欠缺的&#8230;也许Java出身的码农也就这点还有些优势了吧&#8230;纯自己手动敲的，build_ac函数（建立fail指针的过程）学习了网上的教程后模仿着写的，而且带clear()释放内存。<span id="more-1416"></span></p>
<p>AC自动机基于Trie树，Trie树基于字符表，宏定义SIZE表明字符集大小，宏定义MINCHAR表明字符集中最小的字符，这里的定义只适合26个英文小写字母。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define SIZE 26</span>
<span style="color: #339900;">#define MINCHAR ('a')</span>
<span style="color: #0000ff;">struct</span> TrieNode<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> tail<span style="color: #008080;">;</span>		<span style="color: #666666;">//表明此节点为某一字符串结尾</span>
	TrieNode<span style="color: #000040;">*</span> fail<span style="color: #008080;">;</span>		<span style="color: #666666;">//失败指针</span>
	TrieNode<span style="color: #000040;">*</span> nodes<span style="color: #008000;">&#91;</span>SIZE<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>	<span style="color: #666666;">//节点指针的数组</span>
	TrieNode<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i <span style="color: #000080;">&lt;</span> SIZE<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> nodes<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
		tail <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span>, fail <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">void</span> clear<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i <span style="color: #000080;">&lt;</span> SIZE<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>nodes<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
				nodes<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>clear<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
				<span style="color: #0000dd;">delete</span> nodes<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
				nodes<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">struct</span> Trie<span style="color: #008000;">&#123;</span>
	TrieNode<span style="color: #000040;">*</span> root<span style="color: #008080;">;</span>
&nbsp;
	Trie<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span> root <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> TrieNode<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #666666;">//清空Trie树，释放内存</span>
	<span style="color: #0000ff;">void</span> clear<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span> root<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>clear<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #666666;">//插入一个字符串</span>
	<span style="color: #0000ff;">void</span> insert<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>s, <span style="color: #0000ff;">int</span> len<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
		TrieNode<span style="color: #000040;">*</span> N <span style="color: #000080;">=</span> root<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">int</span> idx<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i <span style="color: #000080;">&lt;</span> len<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
			idx <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span><span style="color: #008000;">&#41;</span>s<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">-</span> MINCHAR<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>N<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>nodes<span style="color: #008000;">&#91;</span>idx<span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
				N<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>nodes<span style="color: #008000;">&#91;</span>idx<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> TrieNode<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
			N <span style="color: #000080;">=</span> N<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>nodes<span style="color: #008000;">&#91;</span>idx<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		N<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>tail <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #666666;">//插入字符串完毕后，建立fail指针</span>
	<span style="color: #0000ff;">void</span> build_ac<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
		queue<span style="color: #000080;">&lt;</span>TrieNode<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> q<span style="color: #008080;">;</span>
		TrieNode <span style="color: #000040;">*</span>nd, <span style="color: #000040;">*</span>child, <span style="color: #000040;">*</span>pt<span style="color: #008080;">;</span>
		root<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>fail <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
		q.<span style="color: #007788;">push</span><span style="color: #008000;">&#40;</span>root<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>q.<span style="color: #007788;">empty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
			nd <span style="color: #000080;">=</span> q.<span style="color: #007788;">front</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			q.<span style="color: #007788;">pop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			pt <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i <span style="color: #000080;">&lt;</span> SIZE<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
				child <span style="color: #000080;">=</span> nd<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>nodes<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
				<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>child <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
					<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>nd <span style="color: #000080;">==</span> root<span style="color: #008000;">&#41;</span> child<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>fail <span style="color: #000080;">=</span> root<span style="color: #008080;">;</span>
					<span style="color: #0000ff;">else</span><span style="color: #008000;">&#123;</span>
						pt <span style="color: #000080;">=</span> nd<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>fail<span style="color: #008080;">;</span>
						<span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span>pt <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
							<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>pt<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>nodes<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
								child<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>fail <span style="color: #000080;">=</span> pt<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>nodes<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
								<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
							<span style="color: #008000;">&#125;</span>
							pt <span style="color: #000080;">=</span> pt<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>fail<span style="color: #008080;">;</span>
						<span style="color: #008000;">&#125;</span>
						<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>pt <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> child<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>fail <span style="color: #000080;">=</span> root<span style="color: #008080;">;</span>
					<span style="color: #008000;">&#125;</span>
					q.<span style="color: #007788;">push</span><span style="color: #008000;">&#40;</span>child<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
				<span style="color: #008000;">&#125;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #666666;">//建立fail指针完毕后，在指定的字符串文本数据中来搜索匹配串</span>
	<span style="color: #0000ff;">void</span> process<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>s, <span style="color: #0000ff;">int</span> len<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
		TrieNode <span style="color: #000040;">*</span>nd <span style="color: #000080;">=</span> root, <span style="color: #000040;">*</span>nd2<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">int</span> idx<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i <span style="color: #000080;">&lt;</span> len<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
			idx <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span><span style="color: #008000;">&#41;</span>s<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">-</span> MINCHAR<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span>nd <span style="color: #000040;">!</span><span style="color: #000080;">=</span> root <span style="color: #000040;">&amp;&amp;</span> nd<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>nodes<span style="color: #008000;">&#91;</span>idx<span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> nd <span style="color: #000080;">=</span> nd<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>fail<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>nd<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>nodes<span style="color: #008000;">&#91;</span>idx<span style="color: #008000;">&#93;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				nd <span style="color: #000080;">=</span> nd<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>nodes<span style="color: #008000;">&#91;</span>idx<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
				nd2 <span style="color: #000080;">=</span> nd<span style="color: #008080;">;</span>
				<span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span>nd2 <span style="color: #000040;">!</span><span style="color: #000080;">=</span> root<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
					<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>nd2<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>tail <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
						<span style="color: #666666;">//找到一个匹配</span>
					<span style="color: #008000;">&#125;</span>
					nd2 <span style="color: #000080;">=</span> nd2<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>fail<span style="color: #008080;">;</span>
				<span style="color: #008000;">&#125;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<h4  class="related_post_title">看看 oop , 字符串 , 树</h4><ul class="related_post"><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/string.html" title="整理下字符串的一些数据结构和算法">整理下字符串的一些数据结构和算法</a></li><li>2014-07-07 -- <a target="_blank" href="http://blog.11034.org/2014-07/ruby_on_rails.html" title="ruby on rails">ruby on rails</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/suffix_array.html" title="一套可用的后缀数组代码">一套可用的后缀数组代码</a></li><li>2012-07-03 -- <a target="_blank" href="http://blog.11034.org/2012-07/trie_in_php.html" title="敏感词过滤，PHP实现的Trie树">敏感词过滤，PHP实现的Trie树</a></li><li>2012-03-22 -- <a target="_blank" href="http://blog.11034.org/2012-03/java_util_collections.html" title="java.util中的集合类解析">java.util中的集合类解析</a></li></ul><h4 class="related_post_title">看看 数据结构和算法 </h4><ul class="related_post"><li>2013-05-27 -- <a target="_blank" href="http://blog.11034.org/2013-05/java_map.html" title="java.util中几个Map的性能测试">java.util中几个Map的性能测试</a></li><li>2013-05-07 -- <a target="_blank" href="http://blog.11034.org/2013-05/rectangle_overlap.html" title="判断矩形是否重叠">判断矩形是否重叠</a></li><li>2013-01-15 -- <a target="_blank" href="http://blog.11034.org/2013-01/pack_in_zoj.html" title="背包练习小集合">背包练习小集合</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/string.html" title="整理下字符串的一些数据结构和算法">整理下字符串的一些数据结构和算法</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/suffix_array.html" title="一套可用的后缀数组代码">一套可用的后缀数组代码</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2012-12/ac_automachine.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
