悬赏园豆:50
[已关闭问题]
<dd>在有道搜索框中,当输入一个或者多个字符时,搜索框会出现一定数量的提示,如下图所示:<br /><img src="http://poj.youdao.com/groups/youdao/suggest.jpg" alt="" /><br /><br />现在给你N个单词和一些查询,请输出提示结果,为了简化这个问题,只需要输出以查询词为前缀的并且按字典序排列的最前面的8个单词,如果符合要求的单词一个也没有请只输出当前查询词。 </dd><dt>输入 </dt><dd>第一行是一个正整数N,表示词表中有N个单词。<br />接下来有N行,每行都有一个单词,注意词表中的单词可能有重复,请忽略掉重复单词。所有的单词都由小写字母组成。<br />接下来的一行有一个正整数Q,表示接下来有Q个查询。<br />接下来Q行,每行有一个单词,表示一个查询词,所有的查询词也都是由小写字母组成,并且所有的单词以及查询的长度都不超过20,且都不为空<br />其中:N<=10000,Q<=10000 </dd><dt>输出 </dt><dd>对于每个查询,输出一行,按顺序输出该查询词的提示结果,用空格隔开。 </dd><dt>样例输入 </dt><dt>10<br />a<br />ab<br />hello<br />that<br />those<br />dict<br />youdao<br />world<br />your<br />dictionary<br />6<br />bob<br />d<br />dict<br />dicti<br />yo<br />z</dt><dt>样例输出</dt><dt>bob<br />dict dictionary<br />dict dictionary<br />dictionary<br />youdao your<br />z<br /><br />从上面的测试样例来看,我的答案是对的。可以,提交到POJ 上面,就是错误。Wrong Answer</dt>
<div class="cnblogs_code" onclick="cnblogs_code_show('718d8cb3-f8df-43b7-898a-a70fbfaaf519')"><img id="code_img_closed_718d8cb3-f8df-43b7-898a-a70fbfaaf519" class="code_img_closed" style="display: none;" src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" /><img id="code_img_opened_718d8cb3-f8df-43b7-898a-a70fbfaaf519" class="code_img_opened" onclick="cnblogs_code_hide('718d8cb3-f8df-43b7-898a-a70fbfaaf519',event)" src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" /><span class="cnblogs_code_collapse">代码</span></div>
<div class="cnblogs_code" onclick="cnblogs_code_show('718d8cb3-f8df-43b7-898a-a70fbfaaf519')">
<div id="cnblogs_code_open_718d8cb3-f8df-43b7-898a-a70fbfaaf519">
<pre><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">stdio.h</span><span style="color: #000000;">></span><span style="color: #000000;"><br />#include </span><span style="color: #000000;"><</span><span style="color: #0000ff;">string</span><span style="color: #000000;">.h</span><span style="color: #000000;">></span><span style="color: #000000;"><br />#include </span><span style="color: #000000;"><</span><span style="color: #000000;">stdlib.h</span><span style="color: #000000;">></span><span style="color: #000000;"><br />#include </span><span style="color: #000000;"><</span><span style="color: #000000;">assert.h</span><span style="color: #000000;">></span><span style="color: #000000;"><br /><br /></span><span style="color: #0000ff;">char</span><span style="color: #000000;"> _dict[</span><span style="color: #800080;">10000</span><span style="color: #000000;">][</span><span style="color: #800080;">21</span><span style="color: #000000;">];<br /></span><span style="color: #0000ff;">char</span><span style="color: #000000;"> dict[</span><span style="color: #800080;">10000</span><span style="color: #000000;">][</span><span style="color: #800080;">21</span><span style="color: #000000;">];<br /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> N;<br /><br /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> likecmp(</span><span style="color: #0000ff;">char</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;">a, </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;">b)<br />{<br /> </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;">c, </span><span style="color: #000000;">*</span><span style="color: #000000;">d;<br /> c </span><span style="color: #000000;">=</span><span style="color: #000000;"> a;<br /> d </span><span style="color: #000000;">=</span><span style="color: #000000;"> b;<br /> assert(a </span><span style="color: #000000;">!=</span><span style="color: #000000;"> NULL </span><span style="color: #000000;">&&</span><span style="color: #000000;"> b </span><span style="color: #000000;">!=</span><span style="color: #000000;"> NULL </span><span style="color: #000000;">&&</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;">a </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;"> </span><span style="color: #000000;">&&</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;">b </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">);<br /> </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (</span><span style="color: #000000;">*</span><span style="color: #000000;">a </span><span style="color: #000000;">&&</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;">b)<br /> {<br /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">*</span><span style="color: #000000;">a </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;">b) {<br /> a</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br /> b</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br /> } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {<br /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;">a </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;">b;<br /> }<br /> }<br /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">*</span><span style="color: #000000;">a </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">) </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> strcmp(c, d);<br />}<br /><br /></span><span style="color: #0000ff;">char</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;"> dict_query(</span><span style="color: #0000ff;">char</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;">query)<br />{<br /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> result[</span><span style="color: #800080;">250</span><span style="color: #000000;">];<br /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> low </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> high </span><span style="color: #000000;">=</span><span style="color: #000000;"> N </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">;<br /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> mid;<br /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> number </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> len </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br /><br /> assert(query </span><span style="color: #000000;">!=</span><span style="color: #000000;"> NULL </span><span style="color: #000000;">&&</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;">query </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">);<br /> </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (low </span><span style="color: #000000;"><=</span><span style="color: #000000;"> high)<br /> {<br /> mid </span><span style="color: #000000;">=</span><span style="color: #000000;"> (low </span><span style="color: #000000;">+</span><span style="color: #000000;"> high) </span><span style="color: #000000;">/</span><span style="color: #000000;"> </span><span style="color: #800080;">2</span><span style="color: #000000;">;<br /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (likecmp(query, dict[mid]) </span><span style="color: #000000;">></span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">) {<br /> low </span><span style="color: #000000;">=</span><span style="color: #000000;"> mid </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">;<br /> } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (likecmp(query, dict[mid]) </span><span style="color: #000000;"><</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">) {<br /> high </span><span style="color: #000000;">=</span><span style="color: #000000;"> mid </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">;<br /> } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {<br /> </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (mid </span><span style="color: #000000;">></span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">) <br /> {<br /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (likecmp(query, dict[mid </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">]) </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">) {<br /> mid</span><span style="color: #000000;">--</span><span style="color: #000000;">;<br /> } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {<br /> </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br /> }<br /> }<br /> </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (likecmp(query, dict[mid]) </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;"> </span><span style="color: #000000;">&&</span><span style="color: #000000;"> mid </span><span style="color: #000000;"><=</span><span style="color: #000000;"> N </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">)<br /> {<br /> strcpy(result </span><span style="color: #000000;">+</span><span style="color: #000000;"> len, dict[mid]);<br /> len </span><span style="color: #000000;">+=</span><span style="color: #000000;"> strlen(dict[mid]);<br /> strcpy(result </span><span style="color: #000000;">+</span><span style="color: #000000;"> len, </span><span style="color: #800000;">"</span><span style="color: #800000;"> </span><span style="color: #800000;">"</span><span style="color: #000000;">);<br /> len</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br /> mid</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br /> number</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (number </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800080;">8</span><span style="color: #000000;">) </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br /> }<br /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (result[len </span><span style="color: #000000;">-</span><span style="color: #800080;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800000;">'</span><span style="color: #800000;"> </span><span style="color: #800000;">'</span><span style="color: #000000;">)<br /> {<br /> result[len </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br /> len</span><span style="color: #000000;">--</span><span style="color: #000000;">;<br /> }<br /> assert(result[len </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800000;">'</span><span style="color: #800000;"> </span><span style="color: #800000;">'</span><span style="color: #000000;">);<br /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result;<br /> }<br /> }<br /> strcpy(result, query);<br /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result;<br />}<br /><br /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main()<br />{<br /> <br /> </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> query[</span><span style="color: #800080;">21</span><span style="color: #000000;">];<br /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i;<br /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> j </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> query_N;<br /><br /> scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #000000;">&</span><span style="color: #000000;">N);<br /> getchar();<br /> <br /> </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">; i </span><span style="color: #000000;"><</span><span style="color: #000000;"> N; i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br /> {<br /> gets(_dict[i]);<br /> }<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">排序</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> qsort(_dict, N, </span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(_dict[</span><span style="color: #800080;">0</span><span style="color: #000000;">]), strcmp);<br /><br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">去掉重复的单词</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> strcpy(dict[</span><span style="color: #800080;">0</span><span style="color: #000000;">], _dict[</span><span style="color: #800080;">0</span><span style="color: #000000;">]);<br /> </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">; i </span><span style="color: #000000;"><</span><span style="color: #000000;"> N; i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br /> {<br /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (strcmp(dict[j] , _dict[i]) </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">) {<br /> </span><span style="color: #0000ff;">continue</span><span style="color: #000000;">;<br /> }<br /> j</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br /> strcpy(dict[j], _dict[i]);<br /> }<br /> N </span><span style="color: #000000;">=</span><span style="color: #000000;"> j </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">;<br /><br /> scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #000000;">&</span><span style="color: #000000;">query_N);<br /> getchar();<br /> </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">; i </span><span style="color: #000000;"><</span><span style="color: #000000;"> query_N; i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br /> {<br /> gets(query);<br /><br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">搜索单词</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%s\n</span><span style="color: #800000;">"</span><span style="color: #000000;">, dict_query(query));<br /> }<br /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br />}<br /></span></div></pre>
</div>
</div>
<p> </p>
暮夏
|
菜鸟二级
|
园豆:
391
提问于:2010-05-30 12:50