﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-夜幻梦回-文章分类-字典序</title><link>http://www.cppblog.com/Ylemzy/category/16414.html</link><description>足迹的足迹</description><language>zh-cn</language><lastBuildDate>Wed, 30 Mar 2011 17:39:29 GMT</lastBuildDate><pubDate>Wed, 30 Mar 2011 17:39:29 GMT</pubDate><ttl>60</ttl><item><title>pku 2001 Shortest Prefixes ——字典树应用</title><link>http://www.cppblog.com/Ylemzy/articles/103089.html</link><dc:creator>火碳黑</dc:creator><author>火碳黑</author><pubDate>Sun, 13 Dec 2009 03:15:00 GMT</pubDate><guid>http://www.cppblog.com/Ylemzy/articles/103089.html</guid><description><![CDATA[
		<div style="text-align: center;">
				<span style="color: rgb(0, 112, 255);">Shortest Prefixes</span>
				<br style="color: rgb(0, 112, 255);" />
		</div>
		<p style="color: rgb(0, 112, 255);" class="pst">Description</p>
		<div style="color: rgb(0, 112, 255);" class="ptx" lang="en-US">A prefix of
a string is a substring starting at the beginning of the given string.
The prefixes of "carbon" are: "c", "ca", "car", "carb", "carbo", and
"carbon". Note that the empty string is not considered a prefix in this
problem, but every non-empty string is considered to be a prefix of
itself. In everyday language, we tend to abbreviate words by prefixes.
For example, "carbohydrate" is commonly abbreviated by "carb". In this
problem, given a set of words, you will find for each word the shortest
prefix that uniquely identifies the word it represents. <br /><br />In the sample input below, "carbohydrate" can be abbreviated to
"carboh", but it cannot be abbreviated to "carbo" (or anything shorter)
because there are other words in the list that begin with "carbo". <br /><br />An exact match will override a prefix match. For example, the
prefix "car" matches the given word "car" exactly. Therefore, it is
understood without ambiguity that "car" is an abbreviation for "car" ,
not for "carriage" or any of the other words in the list that begins
with "car". <br /></div>
		<p style="color: rgb(0, 112, 255);" class="pst">Input</p>
		<div style="color: rgb(0, 112, 255);" class="ptx" lang="en-US">The
input contains at least two, but no more than 1000 lines. Each line
contains one word consisting of 1 to 20 lower case letters. </div>
		<p style="color: rgb(0, 112, 255);" class="pst">Output</p>
		<div style="color: rgb(0, 112, 255);" class="ptx" lang="en-US">The
output contains the same number of lines as the input. Each line of the
output contains the word from the corresponding line of the input,
followed by one blank space, and the shortest prefix that uniquely
(without ambiguity) identifies this word. </div>
		<p style="color: rgb(0, 112, 255);" class="pst">Sample Input</p>
		<pre style="color: rgb(0, 112, 255);" class="sio">carbohydrate<br />cart<br />carburetor<br />caramel<br />caribou<br />carbonic<br />cartilage<br />carbon<br />carriage<br />carton<br />car<br />carbonate<br /></pre>
		<p style="color: rgb(0, 112, 255);" class="pst">Sample Output</p>
		<pre style="color: rgb(0, 112, 255);" class="sio">carbohydrate carboh<br />cart cart<br />carburetor carbu<br />caramel cara<br />caribou cari<br />carbonic carboni<br />cartilage carti<br />carbon carbon<br />carriage carr<br />carton carto<br />car car<br />carbonate carbona<br />题意：<br />查找单词的最短前缀<br /><br />代码如下：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> maxn 10000</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> dic[maxn][</span><span style="color: rgb(0, 0, 0);">21</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> M 10000000</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">struct</span><span style="color: rgb(0, 0, 0);"> Dict<br />{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> times, isWord;<br />    Dict </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> next[</span><span style="color: rgb(0, 0, 0);">26</span><span style="color: rgb(0, 0, 0);">];<br />};<br />Dict F[M];</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">供newNode用的，比malloc快。</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> th </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">th表示F被调用过的空间</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">Dict </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> newNode()</span><span style="color: rgb(0, 128, 0);">//<br /></span><span style="color: rgb(0, 0, 0);">{<br />    Dict </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> p </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">F[th</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">];<br />    p</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">isWord </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">, p</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">times </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">初始化P</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">; i </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">26</span><span style="color: rgb(0, 0, 0);">; i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)<br />    {<br />        p</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">next[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> NULL;<br />    }<br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> p;<br />}<br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> insert(Dict </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> r, </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> s)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (s[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)<br />    {<br />        r</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">isWord</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">表示单词数量</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">r</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">next[s[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">])</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">如果s[0]不是结束符，且该单词前缀不存在，生成当前字母</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    {<br />        r</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">next[s[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> newNode();<br />    }<br />    r</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">next[s[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">times</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">该单词当前前缀出现次数</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    insert(r</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">next[s[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">], s </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />}<br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">符合的前缀条件：<br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">前缀是唯一的，或者以该单词本身做自己的前缀。</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> search(Dict </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> r, </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> s, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (s[len] </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (r</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">isWord)</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">表示该前缀是个单词。</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        {<br />            </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> len;<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (r</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">times </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">表示该前缀唯一</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    {<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> len;<br />    }<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">r</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">next[s[len]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">])<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> search(r</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">next[s[len]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">], s, len </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />}<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i, j, k, t, len;<br />    </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> a[</span><span style="color: rgb(0, 0, 0);">21</span><span style="color: rgb(0, 0, 0);">];<br />    Dict </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> root </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> newNode();<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> (i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">; scanf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%s</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, dic[i]) </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> EOF; i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)<br />    {<br />        insert(root, dic[i]);<br />    }<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> (j </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">; j </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);"> i; j</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)<br />    {<br />        printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%s </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, dic[j]);<br />        len </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> search(root, dic[j], </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />        dic[j][len] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />        printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%s\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, dic[j]);<br />    }<br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}</span></div><br /></pre>
		<br style="color: rgb(0, 112, 255);" />
<img src ="http://www.cppblog.com/Ylemzy/aggbug/103089.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Ylemzy/" target="_blank">火碳黑</a> 2009-12-13 11:15 <a href="http://www.cppblog.com/Ylemzy/articles/103089.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>