<?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/tag/%e6%a0%91/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>整理下字符串的一些数据结构和算法</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>一个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>
