﻿<?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++博客-Programming Every Day-随笔分类-算法笔记</title><link>http://www.cppblog.com/tianlearn-language/category/13766.html</link><description>桥梁是由钢铁工人建造的，而不是由工程师建造的。
同样地，软件是由程序人员编写的，而不是由工程师编写的！</description><language>zh-cn</language><lastBuildDate>Sun, 29 Aug 2010 16:55:21 GMT</lastBuildDate><pubDate>Sun, 29 Aug 2010 16:55:21 GMT</pubDate><ttl>60</ttl><item><title>poj 1611 The Suspects</title><link>http://www.cppblog.com/tianlearn-language/archive/2010/08/26/124867.html</link><dc:creator>田兵</dc:creator><author>田兵</author><pubDate>Thu, 26 Aug 2010 12:02:00 GMT</pubDate><guid>http://www.cppblog.com/tianlearn-language/archive/2010/08/26/124867.html</guid><wfw:comment>http://www.cppblog.com/tianlearn-language/comments/124867.html</wfw:comment><comments>http://www.cppblog.com/tianlearn-language/archive/2010/08/26/124867.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tianlearn-language/comments/commentRss/124867.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tianlearn-language/services/trackbacks/124867.html</trackback:ping><description><![CDATA[<table style="FONT-SIZE: 10pt" align=center>
    <tbody>
        <tr>
            <td><strong>Problem:</strong> <a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1611"><u><font color=#800080>1611</font></u></a></td>
            <td width=10><u><font color=#800080></font></u></td>
            <td><strong>User:</strong> <a href="http://acm.pku.edu.cn/JudgeOnline/userstatus?user_id=hehexiaobai"><u><font color=#800080>hehexiaobai</font></u></a></td>
        </tr>
        <tr>
            <td><strong>Memory:</strong> 364K</td>
            <td width=10></td>
            <td><strong>Time:</strong> 454MS</td>
        </tr>
        <tr>
            <td><strong>Language:</strong> C++</td>
            <td width=10></td>
            <td><strong>Result:</strong> <font color=blue>Accepted</font></td>
        </tr>
    </tbody>
</table>
<br>&nbsp;&nbsp; 又是一个并查集。<br>&nbsp;&nbsp;&nbsp;一个人是SARS疑似病例，他所在的整个group都是疑似病例，第0(1)个是疑似病例，求有多少个疑似病例。<br><br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; FONT-FAMILY: courier new; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;MAX</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">30001</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;fa[MAX];<br><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;find(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;fa[x]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">x</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">x:find(fa[x]);<br>}<br><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;Union(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x,&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;y)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fa[find(x)]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">find(y);<br>}<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,m;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(;&nbsp;;)<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">m;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;n</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">m</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fa[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;k,stu;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">m;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">k;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;temp,ttemp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">temp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">temp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fa[temp]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">find(temp);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">k;&nbsp;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">ttemp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">ttemp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fa[find(temp)]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">find(ttemp);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Union(temp,ttemp);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">如果没有找到根节点合并的话可能会断掉，要整个都和另一个合并&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;father</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">find(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">),ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(find(i)</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">father)ans</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">ans</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br></span></div>
<img src ="http://www.cppblog.com/tianlearn-language/aggbug/124867.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tianlearn-language/" target="_blank">田兵</a> 2010-08-26 20:02 <a href="http://www.cppblog.com/tianlearn-language/archive/2010/08/26/124867.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>poj 2524 Ubiquitous Religions 【并查集】</title><link>http://www.cppblog.com/tianlearn-language/archive/2010/08/26/124865.html</link><dc:creator>田兵</dc:creator><author>田兵</author><pubDate>Thu, 26 Aug 2010 11:20:00 GMT</pubDate><guid>http://www.cppblog.com/tianlearn-language/archive/2010/08/26/124865.html</guid><wfw:comment>http://www.cppblog.com/tianlearn-language/comments/124865.html</wfw:comment><comments>http://www.cppblog.com/tianlearn-language/archive/2010/08/26/124865.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tianlearn-language/comments/commentRss/124865.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tianlearn-language/services/trackbacks/124865.html</trackback:ping><description><![CDATA[<div lang=en-US>Ubiquitous Religions</div>
<div>
<table align=center>
    <tbody>
        <tr>
            <td>Time Limit: 5000MS</td>
            <td width=10></td>
            <td>Memory Limit: 65536K</td>
        </tr>
        <tr>
            <td>Total Submissions: 12445</td>
            <td width=10></td>
            <td>Accepted: 5900</td>
        </tr>
    </tbody>
</table>
</div>
<p>Description</p>
<div lang=en-US>There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in. <br><br>You know that there are n students in your university (0 &lt; n &lt;= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 &lt;= m &lt;= n(n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion. </div>
<p>Input</p>
<div lang=en-US>The input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0. </div>
<p>Output</p>
<div lang=en-US>For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.</div>
<p>Sample Input</p>
<pre>10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0
</pre>
<p>Sample Output</p>
<pre>Case 1: 1
Case 2: 7</pre>
<pre style="FONT-SIZE: 12pt"><span style="FONT-SIZE: 10pt">第一个并查集程序，最小生成树不算。</span></pre>
<span style="FONT-SIZE: 10pt">&nbsp;n个点，给你m条边，求最大能有多少个连通分量。<br></span>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; FONT-FAMILY: courier new; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;MAX</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">50001</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;fa[MAX];<br><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;find(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;fa[x]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">x</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">x:find(fa[x]);<br>}<br><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;Union(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x,&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;y)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fa[find(x)]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">find(y);<br>}<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,m;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;tt</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;;&nbsp;tt</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">m;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;n</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">m</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fa[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;max</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">n;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,s,t;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">m;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">s</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">t;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(find(s)</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">find(t))max</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">max</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Union(s,t);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Case&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">tt</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">:</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">max</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br></span></div>
<br><br>
<img src ="http://www.cppblog.com/tianlearn-language/aggbug/124865.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tianlearn-language/" target="_blank">田兵</a> 2010-08-26 19:20 <a href="http://www.cppblog.com/tianlearn-language/archive/2010/08/26/124865.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>poj 3273 Monthly Expense 【二分】</title><link>http://www.cppblog.com/tianlearn-language/archive/2010/08/25/124621.html</link><dc:creator>田兵</dc:creator><author>田兵</author><pubDate>Wed, 25 Aug 2010 01:19:00 GMT</pubDate><guid>http://www.cppblog.com/tianlearn-language/archive/2010/08/25/124621.html</guid><wfw:comment>http://www.cppblog.com/tianlearn-language/comments/124621.html</wfw:comment><comments>http://www.cppblog.com/tianlearn-language/archive/2010/08/25/124621.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tianlearn-language/comments/commentRss/124621.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tianlearn-language/services/trackbacks/124621.html</trackback:ping><description><![CDATA[<br>把n个数分成m分，数字的顺序不能变，使每份的和的最大值最小。<br><br>第一次用二分，觉得很强大，参考牛人的。<br><br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-FAMILY: courier new; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">numeric</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;digit[</span><span style="COLOR: #000000">100005</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,&nbsp;m,&nbsp;i,&nbsp;maxv</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,&nbsp;minv</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">m;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">digit[i]);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">cin&gt;&gt;digit[i];</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;maxv</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">accumulate(digit</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,digit</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">n,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;minv</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">max_element(digit</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,digit</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">n);<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;midv;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(&nbsp;minv&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;maxv&nbsp;)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;midv&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(&nbsp;minv&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;maxv&nbsp;)</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;k&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&nbsp;,&nbsp;sum&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i&nbsp;</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">&nbsp;n;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">&nbsp;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;digit[i];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(sum&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;midv)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;digit[i];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;k&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;m&nbsp;)&nbsp;maxv&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;midv;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;minv&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;midv&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;maxv&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br></span></div>
<img src ="http://www.cppblog.com/tianlearn-language/aggbug/124621.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tianlearn-language/" target="_blank">田兵</a> 2010-08-25 09:19 <a href="http://www.cppblog.com/tianlearn-language/archive/2010/08/25/124621.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Anagrams by Stack  【集训比赛用题】 </title><link>http://www.cppblog.com/tianlearn-language/archive/2010/08/21/124205.html</link><dc:creator>田兵</dc:creator><author>田兵</author><pubDate>Sat, 21 Aug 2010 10:40:00 GMT</pubDate><guid>http://www.cppblog.com/tianlearn-language/archive/2010/08/21/124205.html</guid><wfw:comment>http://www.cppblog.com/tianlearn-language/comments/124205.html</wfw:comment><comments>http://www.cppblog.com/tianlearn-language/archive/2010/08/21/124205.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tianlearn-language/comments/commentRss/124205.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tianlearn-language/services/trackbacks/124205.html</trackback:ping><description><![CDATA[<div class=BigTitle align=center>Anagrams by Stack</div>
<div class=Limits align=center>Time Limit: 1000 ms&nbsp;&nbsp;&nbsp;Memory Limit: 64 MB<br>Total Submission: 10&nbsp;&nbsp;&nbsp;Accepted: 1 </div>
<div class=Special align=center></div>
<div class=ProblemTitle>Description</div>
<div class=ProblemContent>How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT: <br><br>[<br>i i i i o o o o<br>i o i i o o i o<br>]<br><br>where i stands for Push and o stands for Pop. Your program should, given pairs of words produce sequences of stack operations which convert the first word to the second. </div>
<br>
<div class=ProblemTitle>Input</div>
<div class=ProblemContent>The input will consist of several lines of input. The first line of each pair of input lines is to be considered as a source word (which does not include the end-of-line character). The second line (again, not including the end-of-line character) of each pair is a target word. The end of input is marked by end of file.</div>
<br>
<div class=ProblemTitle>Output</div>
<div class=ProblemContent>For each input pair, your program should produce a sorted list of valid sequences of i and o which produce the target word from the source word. Each list should be delimited by <br><br>[<br>]<br><br>and the sequences should be printed in "dictionary order". Within each sequence, each i and o is followed by a single space and each sequence is terminated by a new line. <br>Process<br>A stack is a data storage and retrieval structure permitting two operations: <br><br>Push - to insert an item and<br>Pop - to retrieve the most recently pushed item <br>We will use the symbol i (in) for push and o (out) for pop operations for an initially empty stack of characters. Given an input word, some sequences of push and pop operations are valid in that every character of the word is both pushed and popped, and furthermore, no attempt is ever made to pop the empty stack. For example, if the word FOO is input, then the sequence: <br><br>i i o i o o is valid, but <br>i i o is not (it's too short), neither is <br>i i o o o i (there's an illegal pop of an empty stack) <br><br>Valid sequences yield rearrangements of the letters in an input word. For example, the input word FOO and the sequence i i o i o o produce the anagram OOF. So also would the sequence i i i o o o. You are to write a program to input pairs of words and output all the valid sequences of i and o which will produce the second member of each pair from the first.</div>
<br>
<div class=ProblemTitle>Sample Input</div>
<div class=IO>madam<br>adamm<br>bahama<br>bahama<br>long<br>short<br>eric<br>rice<br>&nbsp;</div>
<br>
<div class=ProblemTitle>Sample Output</div>
<div class=IO>[<abbr>[EOL]</abbr><br>i&nbsp;i&nbsp;i&nbsp;i&nbsp;o&nbsp;o&nbsp;o&nbsp;i&nbsp;o&nbsp;o&nbsp;<abbr>[EOL]</abbr><br>i&nbsp;i&nbsp;i&nbsp;i&nbsp;o&nbsp;o&nbsp;o&nbsp;o&nbsp;i&nbsp;o&nbsp;<abbr>[EOL]</abbr><br>i&nbsp;i&nbsp;o&nbsp;i&nbsp;o&nbsp;i&nbsp;o&nbsp;i&nbsp;o&nbsp;o&nbsp;<abbr>[EOL]</abbr><br>i&nbsp;i&nbsp;o&nbsp;i&nbsp;o&nbsp;i&nbsp;o&nbsp;o&nbsp;i&nbsp;o&nbsp;<abbr>[EOL]</abbr><br>]<abbr>[EOL]</abbr><br>[<abbr>[EOL]</abbr><br>i&nbsp;o&nbsp;i&nbsp;i&nbsp;i&nbsp;o&nbsp;o&nbsp;i&nbsp;i&nbsp;o&nbsp;o&nbsp;o&nbsp;<abbr>[EOL]</abbr><br>i&nbsp;o&nbsp;i&nbsp;i&nbsp;i&nbsp;o&nbsp;o&nbsp;o&nbsp;i&nbsp;o&nbsp;i&nbsp;o&nbsp;<abbr>[EOL]</abbr><br>i&nbsp;o&nbsp;i&nbsp;o&nbsp;i&nbsp;o&nbsp;i&nbsp;i&nbsp;i&nbsp;o&nbsp;o&nbsp;o&nbsp;<abbr>[EOL]</abbr><br>i&nbsp;o&nbsp;i&nbsp;o&nbsp;i&nbsp;o&nbsp;i&nbsp;o&nbsp;i&nbsp;o&nbsp;i&nbsp;o&nbsp;<abbr>[EOL]</abbr><br>]<abbr>[EOL]</abbr><br>[<abbr>[EOL]</abbr><br>]<abbr>[EOL]</abbr><br>[<abbr>[EOL]</abbr><br>i&nbsp;i&nbsp;o&nbsp;i&nbsp;o&nbsp;i&nbsp;o&nbsp;o&nbsp;<abbr>[EOL]</abbr><br>]<abbr>[EOL]</abbr><abbr>[EOF]<br></abbr></div>
<br><br>这题是zoj的题目，题目意思就是给你一个源串和一个目标串，输出所有利用栈将源串转化成目标串的方案。<br><br>i 代表入栈， o代表出栈(也就是输出)。<br><br>ZOJ是0ms轻易过了，可是在自己的我们学校的OJ就一直TLE。<br><br>搜索题，输出的 i o串的长度 为源串长len1的两边，因为每个都有进去再出去。<br>有几个剪枝的地方：<br>1，cnti代码输出i的次数，肯定小于len1，即len/2，如果大于则返回。<br>2，cnto同上。<br>3，如果cnti&lt;cnto，即进栈的字符没有出栈的多这是不可能的。<br><br>我这是到了最后才用judge判断是否可行，如果在过程中就判断的话，应该会更快。<br><br>原始代码：<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; FONT-FAMILY: courier new; BACKGROUND-COLOR: #eeeeee"><img id=Code_Closed_Image_183348 onclick="this.style.display='none'; Code_Closed_Text_183348.style.display='none'; Code_Open_Image_183348.style.display='inline'; Code_Open_Text_183348.style.display='inline';" height=16 src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 align=top><img id=Code_Open_Image_183348 style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_183348.style.display='none'; Code_Closed_Image_183348.style.display='inline'; Code_Closed_Text_183348.style.display='inline';" height=16 src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 align=top><span id=Code_Closed_Text_183348 style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"></span><span id=Code_Open_Text_183348 style="DISPLAY: none"><br><!--<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">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stack</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">queue</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;ans[</span><span style="COLOR: #000000">1005</span><span style="COLOR: #000000">],len;<br></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;s1[</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">],s2[</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;source&nbsp;,target;<br></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;judge()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stack</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;s;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">out</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">len&nbsp;;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(ans[i]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">in</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">source.size())</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;s.push(source[</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">out</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">out</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">out</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">source.size())</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(s.empty())</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;s.top()</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">target[</span><span style="COLOR: #0000ff">out</span><span style="COLOR: #000000">])</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.pop();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br>}<br><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;dfs(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cnti,&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cnto)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;(cnti</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">len</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">(cnto</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">len</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)&nbsp;)</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(cnti</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cnto)</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">cout&lt;&lt;i&lt;&lt;endl;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">len</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(judge())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">len;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(ans[i]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">i&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">o&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">i</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,cnti</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,cnto);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">o</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,cnti,cnto</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br>}<br><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">source</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">target)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;s1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">source,s2</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">target;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(s1.size()</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">s2.size()){cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;&nbsp;</span><span style="COLOR: #0000ff">continue</span><span style="COLOR: #000000">;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sort(s1.begin(),s1.end());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sort(s2.begin(),s2.end());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(s1</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">s2){cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;&nbsp;</span><span style="COLOR: #0000ff">continue</span><span style="COLOR: #000000">;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">source.size()</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br></span></span></div>
<p><br>&nbsp;</p>
<p>后来几乎把所有C++的东西改为C后才100ms+过的。<br><br>尤其是把STL里面的 stack 直接用数组模拟。</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; FONT-FAMILY: courier new; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stack</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstring</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;ans[</span><span style="COLOR: #000000">1005</span><span style="COLOR: #000000">],len,len1,len2;<br></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;source[</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">],target[</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;s1[</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">],s2[</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;s[</span><span style="COLOR: #000000">1000</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;judge()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;top</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">out</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">len&nbsp;;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(ans[i]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">in</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">len1)</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;s[</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">top]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">source[</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">out</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">out</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">out</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">len1)</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(top</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;s[top]</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">target[</span><span style="COLOR: #0000ff">out</span><span style="COLOR: #000000">])</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;top</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br>}<br><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;dfs(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cnti,&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cnto)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;(cnti</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">len</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">(cnto</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">len</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)&nbsp;)</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(cnti</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cnto)</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">cout&lt;&lt;i&lt;&lt;endl;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">len</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(judge())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">len;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(ans[i]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">i&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">cout&lt;&lt;"i&nbsp;";</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">o&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">cout&lt;&lt;"o&nbsp;";</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</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: #008000">//</span><span style="COLOR: #008000">cout&lt;&lt;endl;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">i</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,cnti</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,cnto);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">o</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,cnti,cnto</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br>}<br><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s&nbsp;%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,source,target)</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">EOF)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">strlen(source),len2</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">strlen(target);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(len1</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">len2){&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;&nbsp;</span><span style="COLOR: #0000ff">continue</span><span style="COLOR: #000000">;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">len1;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s1[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">source[i];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s2[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">target[i];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sort(s1,s1</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">len1);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sort(s2,s2</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">len1);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(strcmp(s1,s2)</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">){cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;&nbsp;</span><span style="COLOR: #0000ff">continue</span><span style="COLOR: #000000">;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">len1</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br></span></div>
<img src ="http://www.cppblog.com/tianlearn-language/aggbug/124205.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tianlearn-language/" target="_blank">田兵</a> 2010-08-21 18:40 <a href="http://www.cppblog.com/tianlearn-language/archive/2010/08/21/124205.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>滑雪  寄存器数据转换（POJ上的位操作）</title><link>http://www.cppblog.com/tianlearn-language/archive/2010/08/14/123459.html</link><dc:creator>田兵</dc:creator><author>田兵</author><pubDate>Sat, 14 Aug 2010 12:41:00 GMT</pubDate><guid>http://www.cppblog.com/tianlearn-language/archive/2010/08/14/123459.html</guid><wfw:comment>http://www.cppblog.com/tianlearn-language/comments/123459.html</wfw:comment><comments>http://www.cppblog.com/tianlearn-language/archive/2010/08/14/123459.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tianlearn-language/comments/commentRss/123459.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tianlearn-language/services/trackbacks/123459.html</trackback:ping><description><![CDATA[<div class=BigTitle align=center>滑雪</div>
<div class=Limits align=center>Time Limit: 1000 ms&nbsp;&nbsp;&nbsp;Memory Limit: 64 MB<br>Total Submission: 84&nbsp;&nbsp;&nbsp;Accepted: 20 </div>
<div class=Special align=center></div>
<div class=ProblemTitle>Description</div>
<div class=ProblemContent>Michael喜欢滑雪百这并不奇怪， 因为滑雪的确很刺激。可是为了获得速度，滑的区域必须向下倾斜，而且当你滑到坡底，你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子 <br><br>1 2 3 4 5 <br>16 17 18 19 6 <br>15 24 25 20 7 <br>14 23 22 21 8 <br>13 12 11 10 9 <br><br>一个人可以从某个点滑向上下左右相邻四个点之一，当且仅当高度减小。在上面的例子中，一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上，这是最长的一条。</div>
<br>
<div class=ProblemTitle>Input</div>
<div class=ProblemContent>输入的第一行表示区域的行数R和列数C(1 &lt;= R,C &lt;= 100)。下面是R行，每行有C个整数，代表高度h，0&lt;=h&lt;=10000。输入0 0结束。</div>
<br>
<div class=ProblemTitle>Output</div>
<div class=ProblemContent>输出最长区域的长度。</div>
<br>
<div class=ProblemTitle>Sample Input</div>
<div class=IO>5&nbsp;5<br>1&nbsp;2&nbsp;3&nbsp;4&nbsp;5<br>16&nbsp;17&nbsp;18&nbsp;19&nbsp;6<br>15&nbsp;24&nbsp;25&nbsp;20&nbsp;7<br>14&nbsp;23&nbsp;22&nbsp;21&nbsp;8<br>13&nbsp;12&nbsp;11&nbsp;10&nbsp;9<br>&nbsp;</div>
<br>
<div class=ProblemTitle>Sample Output</div>
<div class=IO>25<abbr>[EOL]</abbr><abbr>[EOF]</abbr></div>
<br>
<div id=ctl00_content_PnlSource>
<div class=ProblemTitle>Source</div>
<div class=ProblemContent>SHTSC&nbsp;2002&nbsp;</div>
</div>
<br><br>第六次集训赛 <br><br>原来对每个点求一次最短路径超时了。<br><br>原来这题是记忆化搜索，DP的思想，DP真的是一点都不知道。<br><br>记忆化搜索可以避免重复的子问题，这也应该是它比较快的原因吧。<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-FAMILY: courier new; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img id=Code_Closed_Image_105331 onclick="this.style.display='none'; Code_Closed_Text_105331.style.display='none'; Code_Open_Image_105331.style.display='inline'; Code_Open_Text_105331.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 height=16><img style="DISPLAY: none" id=Code_Open_Image_105331 onclick="this.style.display='none'; Code_Open_Text_105331.style.display='none'; Code_Closed_Image_105331.style.display='inline'; Code_Closed_Text_105331.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 height=16><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Code_Closed_Text_105331></span><span style="DISPLAY: none" id=Code_Open_Text_105331><br><!--<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">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;G[</span><span style="COLOR: #000000">105</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">105</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">},d[</span><span style="COLOR: #000000">105</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">105</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">};<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;mmax</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;R,C;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;dx[</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">},<br>&nbsp;&nbsp;&nbsp;&nbsp;dy[</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">};<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;dfs(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(d[i][j]</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;d[i][j];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;tx,ty;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;max</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;k</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">;&nbsp;k</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">dx[k];&nbsp;ty</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">dy[k];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(tx</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">tx</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">R</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">ty</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">ty</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">C</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">G[i][j]</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">G[tx][ty])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;t;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(max</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;(t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dfs(tx,ty))&nbsp;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">t;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;d[i][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">max</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>}<br><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;print()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">R;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">,cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">C;&nbsp;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">d[i][j]</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br>}<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">R</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">C,R</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">C)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(G,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">&nbsp;G);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">R;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">C;&nbsp;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">G[i][j];&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(d,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">&nbsp;d);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">R;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">C;&nbsp;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(i,j);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">R;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">C;&nbsp;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(ans</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">d[i][j])ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">d[i][j];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">ans</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">print();</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br></span></span></div>
<br><br><br>
<div class=BigTitle align=center>寄存器数据转换</div>
<div class=Limits align=center>Time Limit: 1000 ms&nbsp;&nbsp;&nbsp;Memory Limit: 64 MB<br>Total Submission: 79&nbsp;&nbsp;&nbsp;Accepted: 20 </div>
<div class=Special align=center></div>
<div class=ProblemTitle>Description</div>
<div class=ProblemContent>假设你工作在一个32位的机器上，你需要将某一个外设寄存器的第X位设置成0(最低位为第0位，最高位为第31位)，将第Y位开始的连续三位设置成110（从高位到低位的顺序），而其他位保持不变。对给定的寄存器值R，及X，Y，编程计算更改后的寄存器值R。</div>
<br>
<div class=ProblemTitle>Input</div>
<div class=ProblemContent>第一行为整数T&lt;1000，表示要输入的数据数<br>每一行，包括R,X,Y，以逗号","分隔，R为16进制表示的32位整数(小写字母)，X,Y在0-31之间且Y&gt;=3，(Y-X)的绝对值&gt;=3，保证两次置位不会重合</div>
<br>
<div class=ProblemTitle>Output</div>
<div class=ProblemContent>每组数据占一行，显示更改后的寄存器值R（16进制输出），不要输出前置0</div>
<br>
<div class=ProblemTitle>Sample Input</div>
<div class=IO>1<br>12345678,0,3<br>&nbsp;</div>
<br>
<div class=ProblemTitle>Sample Output</div>
<div class=IO>1234567c<abbr>[EOL]</abbr><abbr>[EOF]</abbr></div>
<br>用位操作这么简单，我还用笨方法做。&#8230;&#8230;<br><br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-FAMILY: courier new; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img id=Code_Closed_Image_105752 onclick="this.style.display='none'; Code_Closed_Text_105752.style.display='none'; Code_Open_Image_105752.style.display='inline'; Code_Open_Text_105752.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 height=16><img style="DISPLAY: none" id=Code_Open_Image_105752 onclick="this.style.display='none'; Code_Open_Text_105752.style.display='none'; Code_Closed_Image_105752.style.display='inline'; Code_Closed_Text_105752.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 height=16><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Code_Closed_Text_105752></span><span style="DISPLAY: none" id=Code_Open_Text_105752><br><!--<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">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;t;<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">t);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(t</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;r,x,y,r1;<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%x,%d,%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">r,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">y);<br>&nbsp;&nbsp;&nbsp;&nbsp;r1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;r1</span><span style="COLOR: #000000">&lt;&lt;=</span><span style="COLOR: #000000">x;&nbsp;r1</span><span style="COLOR: #000000">=~</span><span style="COLOR: #000000">r1;&nbsp;r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">r</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">r1;<br>&nbsp;&nbsp;&nbsp;&nbsp;r1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;r1</span><span style="COLOR: #000000">&lt;&lt;=</span><span style="COLOR: #000000">y;&nbsp;r</span><span style="COLOR: #000000">|=</span><span style="COLOR: #000000">r1;<br>&nbsp;&nbsp;&nbsp;&nbsp;r1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;r1</span><span style="COLOR: #000000">&lt;&lt;=</span><span style="COLOR: #000000">(y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);&nbsp;r</span><span style="COLOR: #000000">|=</span><span style="COLOR: #000000">r1;<br>&nbsp;&nbsp;&nbsp;&nbsp;r1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;r1</span><span style="COLOR: #000000">&lt;&lt;=</span><span style="COLOR: #000000">(y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">);&nbsp;r1</span><span style="COLOR: #000000">=~</span><span style="COLOR: #000000">r1;&nbsp;r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">r</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">r1;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%x\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,r);<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br></span></span></div>
<img src ="http://www.cppblog.com/tianlearn-language/aggbug/123459.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tianlearn-language/" target="_blank">田兵</a> 2010-08-14 20:41 <a href="http://www.cppblog.com/tianlearn-language/archive/2010/08/14/123459.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO chapter 3 section 2 Sweet Butter</title><link>http://www.cppblog.com/tianlearn-language/archive/2010/08/08/122617.html</link><dc:creator>田兵</dc:creator><author>田兵</author><pubDate>Sun, 08 Aug 2010 01:20:00 GMT</pubDate><guid>http://www.cppblog.com/tianlearn-language/archive/2010/08/08/122617.html</guid><wfw:comment>http://www.cppblog.com/tianlearn-language/comments/122617.html</wfw:comment><comments>http://www.cppblog.com/tianlearn-language/archive/2010/08/08/122617.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.cppblog.com/tianlearn-language/comments/commentRss/122617.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tianlearn-language/services/trackbacks/122617.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 做了一题，开心，冒一下泡：题目大意：有一些草原（1，&#8230;&#8230;，p个），告诉你n个奶牛的在哪个草原，Famer John 会在其中一个草原放一块糖，让所有的奶牛去舔（提高奶的质量^-^），要求的就是把糖放在哪个草原使所有的奶牛到那的路程和最短。（当然奶牛都是拣最短的路走）。&nbsp;第一种超时思路 ：Floyd算法求出任意两个草原间的距离，枚举所有草原，取所...&nbsp;&nbsp;<a href='http://www.cppblog.com/tianlearn-language/archive/2010/08/08/122617.html'>阅读全文</a><img src ="http://www.cppblog.com/tianlearn-language/aggbug/122617.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tianlearn-language/" target="_blank">田兵</a> 2010-08-08 09:20 <a href="http://www.cppblog.com/tianlearn-language/archive/2010/08/08/122617.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>POJ 2709 Painter  贪心</title><link>http://www.cppblog.com/tianlearn-language/archive/2010/08/07/122535.html</link><dc:creator>田兵</dc:creator><author>田兵</author><pubDate>Sat, 07 Aug 2010 08:56:00 GMT</pubDate><guid>http://www.cppblog.com/tianlearn-language/archive/2010/08/07/122535.html</guid><wfw:comment>http://www.cppblog.com/tianlearn-language/comments/122535.html</wfw:comment><comments>http://www.cppblog.com/tianlearn-language/archive/2010/08/07/122535.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tianlearn-language/comments/commentRss/122535.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tianlearn-language/services/trackbacks/122535.html</trackback:ping><description><![CDATA[<pre>这题比较犀利，特别的地方是在配灰色的时候。</pre>
<pre>用judge[]存储减去对应颜色后剩余的体积，用这部分配灰色。当然judge中的每一项都有大于0</pre>
<pre>配法：每排一次序配1ml，保证这1ml是由体积最多的三种颜料配成。</pre>
<pre>最初的时候是排序，然后一下配最大的三个当中最小的那个的体积，这是不够贪的。<br></pre>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
<pre><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstring</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;color[</span><span style="COLOR: #000000">13</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">};<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;judge[</span><span style="COLOR: #000000">13</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">};<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;deal(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,t;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sort(judge</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;judge</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">n);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(judge[n]</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">judge[n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">judge[n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;judge[n]</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;judge[n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;judge[n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sort(judge</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;judge</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">n);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;ans;<br>}<br><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,i;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">n,n)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&nbsp;,num;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;f</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">color[i];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">num;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;t</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">1000</span><span style="COLOR: #000000">;&nbsp;t</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;&nbsp;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;judge[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">50</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">t;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;judge[j]</span><span style="COLOR: #000000">-=</span><span style="COLOR: #000000">color[j];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(judge[j]</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)f</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(f</span><span style="COLOR: #000000">==</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">deal(n);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;ans</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">num)&nbsp;{&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">t</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br></pre>
</span></div>
<img src ="http://www.cppblog.com/tianlearn-language/aggbug/122535.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tianlearn-language/" target="_blank">田兵</a> 2010-08-07 16:56 <a href="http://www.cppblog.com/tianlearn-language/archive/2010/08/07/122535.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>背包问题【转】</title><link>http://www.cppblog.com/tianlearn-language/archive/2010/08/03/122099.html</link><dc:creator>田兵</dc:creator><author>田兵</author><pubDate>Tue, 03 Aug 2010 12:40:00 GMT</pubDate><guid>http://www.cppblog.com/tianlearn-language/archive/2010/08/03/122099.html</guid><wfw:comment>http://www.cppblog.com/tianlearn-language/comments/122099.html</wfw:comment><comments>http://www.cppblog.com/tianlearn-language/archive/2010/08/03/122099.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tianlearn-language/comments/commentRss/122099.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tianlearn-language/services/trackbacks/122099.html</trackback:ping><description><![CDATA[<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-WEIGHT: bold; FONT-SIZE: 10.5pt; COLOR: rgb(255,0,0); mso-spacerun: yes">P01<font face=宋体>：01背包问题</font></span><span style="FONT-WEIGHT: bold; FONT-SIZE: 10.5pt; COLOR: rgb(255,0,0); mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>题目：有N件物品和一个容量为V的背包。第i件物品的费用是c[i]，价值是w[i]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量，且价值总和最大。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>基本思路：</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>这是最基础的背包问题，特点是：每种物品仅有一件，可以选择放或不放。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>用子问题定义状态：即f[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值。则其状态转移方程便是：f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>这个方程非常重要，基本上所有跟背包相关的问题的方程都是由它衍生出来的。所以有必要将它详细解释一下：&#8220;将前i件物品放入容量为v的背包中&#8221;这个子问题，若只考虑第i件物品的策略（放或不放），那么就可以转化为一个只牵扯前i-1件物品的问题。如果不放第i件物品，那么问题就转化为&#8220;前i-1件物品放入容量为v的背包中&#8221;；如果放第i件物品，那么问题就转化为&#8220;前i-1件物品放入剩下的容量为v-c[i]的背包中&#8221;，此时能获得的最大价值就是f [i-1][v-c[i]]再加上通过放入第i件物品获得的价值w[i]。</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>注意f[i][v]有意义当且仅当存在一个前i件物品的子集，其费用总和为v。所以按照这个方程递推完毕后，最终的答案并不一定是f[N] [V]，而是f[N][0..V]的最大值。如果将状态的定义中的&#8220;恰&#8221;字去掉，在转移方程中就要再加入一项f[i][v-1]，这样就可以保证f[N] [V]就是最后的答案。至于为什么这样就可以，由你自己来体会了。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>优化空间复杂度：</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>以上方法的时间和空间复杂度均为O(N*V)，其中时间复杂度基本已经不能再优化了，但空间复杂度却可以优化到O(V)。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>先考虑上面讲的基本思路如何实现，肯定是有一个主循环i=1..N，每次算出来二维数组f[i][0..V]的所有值。那么，如果只用一个数组f [0..V]，能不能保证第i次循环结束后f[v]中表示的就是我们定义的状态f[i][v]呢？f[i][v]是由f[i-1][v]和f[i-1][v-c[i]]两个子问题递推而来，能否保证在推f[i][v]时（也即在第i次主循环中推f[v]时）能够得到f[i-1][v]和f[i-1][v -c[i]]的值呢？事实上，这要求在每次主循环中我们以v=V..0的顺序推f[v]，这样才能保证推f[v]时f[v-c[i]]保存的是状态f[i -1][v-c[i]]的值。伪代码如下： </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes">for i=1..N </span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 31.5pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes">for v=V..0 </span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 52.5pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes">f[v]=max{f[v],f[v-c[i]]+w[i]}; </span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>其中的f[v]=max{f[v],f[v-c[i]]}一句恰就相当于我们的转移方程f[i][v]=max{f[i-1][v],f[i- 1][v-c[i]]}，因为现在的f[v-c[i]]就相当于原来的f[i-1][v-c[i]]。</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>如果将v的循环顺序从上面的逆序改成顺序的话，那么则成了f[i][v]由f[i][v-c[i]]推知，与本题意不符，但它却是另一个重要的背包问题P02最简捷的解决方案，故学习只用一维数组解01背包问题是十分必要的。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>总结：</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes">01<font face=宋体>背包问题是最基本的背包问题，它包含了背包问题中设计状态、方程的最基本思想，另外，别的类型的背包问题往往也可以转换成01背包问题求解。故一定要仔细体会上面基本思路的得出方法，状态转移方程的意义，以及最后怎样优化的空间复杂度。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; COLOR: rgb(255,0,0); mso-spacerun: yes">P02<font face=宋体>：完全背包问题 </font></span><span style="FONT-SIZE: 10.5pt; COLOR: rgb(255,0,0); mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>题目：有N种物品和一个容量为V的背包，每种物品都有无限件可用。第i种物品的费用是c[i]，价值是w[i]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量，且价值总和最大。</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>基本思路：</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>这个问题非常类似于01背包问题，所不同的是每种物品有无限件。也就是从每种物品的角度考虑，与它相关的策略已并非取或不取两种，而是有取0件、取1件、取2件&#8230;&#8230;等很多种。如果仍然按照解01背包时的思路，令f[i][v]表示前i种物品恰放入一个容量为v的背包的最大权值。仍然可以按照每种物品不同的策略写出状态转移方程，像这样：f[i][v]=max{f[i-1][v-k*c[i]]+k*w[i]|0&lt;=k*c[i]&lt;= v}。这跟01背包问题一样有O(N*V)个状态需要求解，但求解每个状态的时间则不是常数了，求解状态f[i][v]的时间是O(v/c[i])，总的复杂度是超过O(VN)的。</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>将01背包问题的基本思路加以改进，得到了这样一个清晰的方法。这说明01背包问题的方程的确是很重要，可以推及其它类型的背包问题。但我们还是试图改进这个复杂度。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>一个简单有效的优化：</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>完全背包问题有一个很简单有效的优化，是这样的：若两件物品i、j满足c[i]&lt;=c[j]且w[i]&gt;=w[j]，则将物品j去掉，不用考虑。这个优化的正确性显然：任何情况下都可将价值小费用高得j换成物美价廉的i，得到至少不会更差的方案。对于随机生成的数据，这个方法往往会大大减少物品的件数，从而加快速度。然而这个并不能改善最坏情况的复杂度，因为有可能特别设计的数据可以一件物品也去不掉。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>转化为01背包问题求解：</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>既然01背包问题是最基本的背包问题，那么我们可以考虑把完全背包问题转化为01背包问题来解。最简单的想法是，考虑到第i种物品最多选V/c[i]件，于是可以把第i种物品转化为V/c[i]件费用及价值均不变的物品，然后求解这个01背包问题。这样完全没有改进基本思路的时间复杂度，但这毕竟给了我们将完全背包问题转化为01背包问题的思路：将一种物品拆成多件物品。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>更高效的转化方法是：</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>把第i种物品拆成费用为c[i]*2^k、价值为w[i]*2^k的若干件物品，其中k满足c[i]*2^k&lt;V。这是二进制的思想，因为不管最优策略选几件第i种物品，总可以表示成若干个2^k件物品的和。这样把每种物品拆成O(log(V/c[i]))件物品，是一个很大的改进。但我们有更优的O(VN)的算法。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes">O(VN)<font face=宋体>的算法：这个算法使用一维数组，先看伪代码： &lt;以class为例&gt; for i=1..N for v=0..V f[v]=max{f[v],f[v-c[i]</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes">]+w[i]}; </span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>你会发现，这个伪代码与P01的伪代码只有v的循环次序不同而已。为什么这样一改就可行呢？首先想想为什么P01中要按照v=V..0的逆序来循环。这是因为要保证第i次循环中的状态f[i][v]是由状态f[i-1][v-c[i]]递推而来。换句话说，这正是为了保证每件物品只选一次，保证在考虑&#8220;选入第i件物品&#8221;这件策略时，依据的是一个绝无已经选入第i件物品的子结果f[i-1][v-c[i]]。而现在完全背包的特点恰是每种物品可选无限件，所以在考虑&#8220;加选一件第i种物品&#8221;这种策略时，却正需要一个可能已选入第i种物品的子结果f[i][v-c[i]]，所以就可以并且必须采用v= 0..V的顺序循环。这就是这个简单的程序为何成立的道理。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>这个算法也可以以另外的思路得出。例如，基本思路中的状态转移方程可以等价地变形成这种形式：f[i][v]=max{f[i-1][v],f[i][v-c[i]]+w[i]}，将这个方程用一维数组实现，便得到了上面的伪代码。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>总结： </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>完全背包问题也是一个相当基础的背包问题，它有两个状态转移方程，分别在&#8220;基本思路&#8221;以及&#8220;O(VN)的算法&#8220;的小节中给出。希望你能够对这两个状态转移方程都仔细地体会，不仅记住，也要弄明白它们是怎么得出来的，最好能够自己想一种得到这些方程的方法。事实上，对每一道动态规划题目都思考其方程的意义以及如何得来，是加深对动态规划的理解、提高动态规划功力的好方法。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; COLOR: rgb(255,0,0); mso-spacerun: yes">P03<font face=宋体>：多重背包问题 </font></span><span style="FONT-SIZE: 10.5pt; COLOR: rgb(255,0,0); mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>题目：有N种物品和一个容量为V的背包。第i种物品最多有n[i]件可用，每件费用是c[i]，价值是w[i]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量，且价值总和最大。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>基本算法：</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>这题目和完全背包问题很类似。基本的方程只需将完全背包问题的方程略微一改即可，因为对于第i种物品有n[i]+1种策略：取0件，取1件&#8230;&#8230;取 n[i]件。令f[i][v]表示前i种物品恰放入一个容量为v的背包的最大权值，则：f[i][v]=max{f[i-1][v-k*c[i]]+ k*w[i]|0&lt;=k&lt;=n[i]}。复杂度是O(V*∑n[i])。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>转化为01背包问题： </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>另一种好想好写的基本方法是转化为01背包求解：把第i种物品换成n[i]件01背包中的物品，则得到了物品数为∑n[i]的01背包问题，直接求解，复杂度仍然是O(V*∑n[i])。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>但是我们期望将它转化为01背包问题之后能够像完全背包一样降低复杂度。仍然考虑二进制的思想，我们考虑把第i种物品换成若干件物品，使得原问题中第i种物品可取的每种策略——取0..n[i]件——均能等价于取若干件代换以后的物品。另外，取超过n[i]件的策略必不能出现。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>方法是：将第i种物品分成若干件物品，其中每件物品有一个系数，这件物品的费用和价值均是原来的费用和价值乘以这个系数。使这些系数分别为 1,2,4,...,2^(k-1),n[i]-2^k+1，且k是满足n[i]-2^k+1&gt;0的最大整数。例如，如果n[i]为13，就将这种物品分成系数分别为1,2,4,6的四件物品。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>分成的这几件物品的系数和为n[i]，表明不可能取多于n[i]件的第i种物品。另外这种方法也能保证对于0..n[i]间的每一个整数，均可以用若干个系数的和表示，这个证明可以分0..2^k-1和2^k..n[i]两段来分别讨论得出，并不难，希望你自己思考尝试一下。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>这样就将第i种物品分成了O(log n[i])种物品，将原问题转化为了复杂度为O(V*∑log n[i])的01背包问题，是很大的改进。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes">O(VN)<font face=宋体>的算法：</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>多重背包问题同样有O(VN)的算法。这个算法基于基本算法的状态转移方程，但应用单调队列的方法使每个状态的值可以以均摊O(1)的时间求解。由于用单调队列优化的DP已超出了NOIP的范围，故本文不再展开讲解。我最初了解到这个方法是在楼天成的&#8220;男人八题&#8221;幻灯片上。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>小结：</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>这里我们看到了将一个算法的复杂度由O(V*∑n[i])改进到O(V*∑log n[i])的过程，还知道了存在应用超出NOIP范围的知识的O(VN)算法。希望你特别注意&#8220;拆分物品&#8221;的思想和方法，自己证明一下它的正确性，并用尽量简洁的程序来实现。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; COLOR: rgb(255,0,0); mso-spacerun: yes">P04<font face=宋体>：混合三种背包问题 </font></span><span style="FONT-SIZE: 10.5pt; COLOR: rgb(255,0,0); mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>问题：</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>如果将P01、P02、P03混合起来。也就是说，有的物品只可以取一次（01背包），有的物品可以取无限次（完全背包），有的物品可以取的次数有一个上限（多重背包）。应该怎么求解呢？ </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes">01<font face=宋体>背包与完全背包的混合：</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>考虑到在P01和P02中最后给出的伪代码只有一处不同，故如果只有两类物品：一类物品只能取一次，另一类物品可以取无限次，那么只需在对每个物品应用转移方程时，根据物品的类别选用顺序或逆序的循环即可，复杂度是O(VN)。伪代码如下： </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes">for i=1..N </span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 31.5pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes">if <font face=宋体>第i件物品是01背包 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 42pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes">for v=V..0 </span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 52.5pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes">f[v]=max{f[v],f[v-c[i]]+w[i]}; </span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 31.5pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes">else if <font face=宋体>第i件物品是完全背包 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 63pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes">for v=0..V </span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 73.5pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes">f[v]=max{f[v],f[v-c[i]]+w[i]}; </span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>再加上多重背包： </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>如果再加上有的物品最多可以取有限次，那么原则上也可以给出O(VN)的解法：遇到多重背包类型的物品用单调队列解即可。但如果不考虑超过NOIP范围的算法的话，用P03中将每个这类物品分成O(log n[i])个01背包的物品的方法也已经很优了。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>小结： </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>有人说，困难的题目都是由简单的题目叠加而来的。这句话是否公理暂且存之不论，但它在本讲中已经得到了充分的体现。本来01背包、完全背包、多重背包都不是什么难题，但将它们简单地组合起来以后就得到了这样一道一定能吓倒不少人的题目。但只要基础扎实，领会三种基本背包问题的思想，就可以做到把困难的题目拆分成简单的题目来解决。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; COLOR: rgb(255,0,0); mso-spacerun: yes">P05<font face=宋体>：二维费用的背包问题 </font></span><span style="FONT-SIZE: 10.5pt; COLOR: rgb(255,0,0); mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>问题：</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>二维费用的背包问题是指：对于每件物品，具有两种不同的费用；选择这件物品必须同时付出这两种代价；对于每种代价都有一个可付出的最大值（背包容量）。问怎样选择物品可以得到最大的价值。设这两种代价分别为代价1和代价2，第i件物品所需的两种代价分别为a[i]和b[i]。两种代价可付出的最大值（两种背包容量）分别为V和U。物品的价值为w[i]。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>算法：</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>费用加了一维，只需状态也加一维即可。设f[i][v][u]表示前i件物品付出两种代价分别为v和u时可获得的最大价值。状态转移方程就是：f [i][v][u]=max{f[i-1][v][u],f[i-1][v-a[i]][u-b[i]]+w[i]}。如前述方法，可以只使用二维的数组：当每件物品只可以取一次时变量v和u采用顺序的循环，当物品有如完全背包问题时采用逆序的循环。当物品有如多重背包问题时拆分物品。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>物品总个数的限制：</font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>有时，&#8220;二维费用&#8221;的条件是以这样一种隐含的方式给出的：最多只能取M件物品。这事实上相当于每件物品多了一种&#8220;件数&#8221;的费用，每个物品的件数费用均为1，可以付出的最大件数费用为M。换句话说，设f[v][m]表示付出费用v、最多选m件时可得到的最大价值，则根据物品的类型（01、完全、多重）用不同的方法循环更新，最后在f[0..V][0..M]范围内寻找答案。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>另外，如果要求&#8220;恰取M件物品&#8221;，则在f[0..V][M]范围内寻找答案。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>小结： </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<p class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt; TEXT-INDENT: 21pt" align=left><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"><font face=宋体>事实上，当发现由熟悉的动态规划题目变形得来的题目时，在原来的状态中加一纬以满足新的限制是一种比较通用的方法。希望你能从本讲中初步体会到这种方法。 </font></span><span style="FONT-SIZE: 10.5pt; mso-spacerun: yes"></span></p>
<img src ="http://www.cppblog.com/tianlearn-language/aggbug/122099.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tianlearn-language/" target="_blank">田兵</a> 2010-08-03 20:40 <a href="http://www.cppblog.com/tianlearn-language/archive/2010/08/03/122099.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>快速排序</title><link>http://www.cppblog.com/tianlearn-language/archive/2010/06/15/117954.html</link><dc:creator>田兵</dc:creator><author>田兵</author><pubDate>Tue, 15 Jun 2010 02:57:00 GMT</pubDate><guid>http://www.cppblog.com/tianlearn-language/archive/2010/06/15/117954.html</guid><wfw:comment>http://www.cppblog.com/tianlearn-language/comments/117954.html</wfw:comment><comments>http://www.cppblog.com/tianlearn-language/archive/2010/06/15/117954.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tianlearn-language/comments/commentRss/117954.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tianlearn-language/services/trackbacks/117954.html</trackback:ping><description><![CDATA[简单实现的快速排序：<br>最优解：每次选取的是中间位置的元素，O(nlogn)<br>最差解：每次选的是最大的或最小的元素，这样每次只能排一个O(n^2)<br><br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-FAMILY: Verdana; FONT-SIZE: 12pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdlib.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arraySize</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;array[arraySize]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">7</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">};</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;swap(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;array[],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;temp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">array[i];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">array[j];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">temp;<br>}<br><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;quicksort(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;array[],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,last;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(n</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;swap(array,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,rand()</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">n);&nbsp;&nbsp;//随即选取一个点，作为比较元素<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&nbsp;;i&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;n;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">&nbsp;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(array[i]&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;array[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]&nbsp;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;swap(array,</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">last,i);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;swap(array,last,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //把用于比较的元素放入合适位置<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quicksort(array,last);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quicksort(array</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">last</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">last</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br>}<br><br><br><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;quicksort(array,</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">arraySize;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">array[i]</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br></span></div>
<img src ="http://www.cppblog.com/tianlearn-language/aggbug/117954.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tianlearn-language/" target="_blank">田兵</a> 2010-06-15 10:57 <a href="http://www.cppblog.com/tianlearn-language/archive/2010/06/15/117954.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PKU 2251 用DFS TLE了 BFS AC了</title><link>http://www.cppblog.com/tianlearn-language/archive/2010/05/28/116643.html</link><dc:creator>田兵</dc:creator><author>田兵</author><pubDate>Fri, 28 May 2010 13:50:00 GMT</pubDate><guid>http://www.cppblog.com/tianlearn-language/archive/2010/05/28/116643.html</guid><wfw:comment>http://www.cppblog.com/tianlearn-language/comments/116643.html</wfw:comment><comments>http://www.cppblog.com/tianlearn-language/archive/2010/05/28/116643.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tianlearn-language/comments/commentRss/116643.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tianlearn-language/services/trackbacks/116643.html</trackback:ping><description><![CDATA[<div lang=en-US class=ptt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dungeon Master</div>
<div class=plm>
<table align=center>
    <tbody>
        <tr>
            <td><strong>Time Limit:</strong> 1000MS</td>
            <td width=10></td>
            <td><strong>Memory Limit:</strong> 65536K</td>
        </tr>
        <tr>
            <td><strong>Total Submissions:</strong> 6561</td>
            <td width=10></td>
            <td><strong>Accepted:</strong> 2610</td>
        </tr>
    </tbody>
</table>
</div>
<p class=pst>Description</p>
<div lang=en-US class=ptx>You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides. <br><br>Is an escape possible? If yes, how long will it take? <br></div>
<p class=pst>Input</p>
<div lang=en-US class=ptx>The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). <br>L is the number of levels making up the dungeon. <br>R and C are the number of rows and columns making up the plan of each level. <br>Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C. </div>
<p class=pst>Output</p>
<div lang=en-US class=ptx>Each maze generates one line of output. If it is possible to reach the exit, print a line of the form <br>
<blockquote>Escaped in x minute(s). </blockquote><br>where x is replaced by the shortest time it takes to escape. <br>If it is not possible to escape, print the line <br>
<blockquote>Trapped! </blockquote></div>
<p class=pst>Sample Input</p>
<pre class=sio>3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
</pre>
<p class=pst>Sample Output</p>
<pre class=sio>Escaped in 11 minute(s).
Trapped!
</pre>
<br>三维迷宫问题<br>据说要用BFS，范围30 ，我以为DFS能搞定的，为什么会超时呢？？？？<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 200.34%; PADDING-RIGHT: 5px; HEIGHT: 1047px; FONT-SIZE: 12pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px">&nbsp;1<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include&lt;iostream&gt;<br>&nbsp;2<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include&lt;string.h&gt;<br>&nbsp;3<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">using&nbsp;namespace&nbsp;std;<br>&nbsp;4<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">char&nbsp;G[35][35][35];<br>&nbsp;5<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">bool&nbsp;g[35][35][35];<br>&nbsp;6<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">void&nbsp;dfs(int&nbsp;l,int&nbsp;r,int&nbsp;c)<br>&nbsp;7<img id=Codehighlighter1_127_1067_Open_Image onclick="this.style.display='none'; Codehighlighter1_127_1067_Open_Text.style.display='none'; Codehighlighter1_127_1067_Closed_Image.style.display='inline'; Codehighlighter1_127_1067_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img id=Codehighlighter1_127_1067_Closed_Image onclick="this.style.display='none'; Codehighlighter1_127_1067_Closed_Text.style.display='none'; Codehighlighter1_127_1067_Open_Image.style.display='inline'; Codehighlighter1_127_1067_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"><img src="http://www.cppblog.com/Images/dot.gif">{<br>&nbsp;8<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(G[l][r][c]==0||G[l][r][c]=='#')return&nbsp;;<br>&nbsp;9<img id=Codehighlighter1_267_323_Open_Image onclick="this.style.display='none'; Codehighlighter1_267_323_Open_Text.style.display='none'; Codehighlighter1_267_323_Closed_Image.style.display='inline'; Codehighlighter1_267_323_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img id=Codehighlighter1_267_323_Closed_Image onclick="this.style.display='none'; Codehighlighter1_267_323_Closed_Text.style.display='none'; Codehighlighter1_267_323_Open_Image.style.display='inline'; Codehighlighter1_267_323_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((G[l+1][r][c]=='.'||G[l+1][r][c]&gt;G[l][r][c])&amp;&amp;G[l+1][r][c]!=0&amp;&amp;G[l+1][r][c]!='#')&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{&nbsp;G[l+1][r][c]=G[l][r][c]+1;g[l+1][r][c]=1;dfs(l+1,r,c);}<br>10<img id=Codehighlighter1_415_471_Open_Image onclick="this.style.display='none'; Codehighlighter1_415_471_Open_Text.style.display='none'; Codehighlighter1_415_471_Closed_Image.style.display='inline'; Codehighlighter1_415_471_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img id=Codehighlighter1_415_471_Closed_Image onclick="this.style.display='none'; Codehighlighter1_415_471_Closed_Text.style.display='none'; Codehighlighter1_415_471_Open_Image.style.display='inline'; Codehighlighter1_415_471_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((G[l][r-1][c]=='.'||G[l][r-1][c]&gt;G[l][r][c])&amp;&amp;G[l][r-1][c]!=0&amp;&amp;G[l][r-1][c]!='#')&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{&nbsp;G[l][r-1][c]=G[l][r][c]+1;g[l][r-1][c]=1;dfs(l,r-1,c);}&nbsp;&nbsp;<br>11<img id=Codehighlighter1_565_621_Open_Image onclick="this.style.display='none'; Codehighlighter1_565_621_Open_Text.style.display='none'; Codehighlighter1_565_621_Closed_Image.style.display='inline'; Codehighlighter1_565_621_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img id=Codehighlighter1_565_621_Closed_Image onclick="this.style.display='none'; Codehighlighter1_565_621_Closed_Text.style.display='none'; Codehighlighter1_565_621_Open_Image.style.display='inline'; Codehighlighter1_565_621_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((G[l][r+1][c]=='.'||G[l][r+1][c]&gt;G[l][r][c])&amp;&amp;G[l][r+1][c]!=0&amp;&amp;G[l][r+1][c]!='#')&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{&nbsp;G[l][r+1][c]=G[l][r][c]+1;g[l][r+1][c]=1;dfs(l,r+1,c);}<br>12<img id=Codehighlighter1_713_769_Open_Image onclick="this.style.display='none'; Codehighlighter1_713_769_Open_Text.style.display='none'; Codehighlighter1_713_769_Closed_Image.style.display='inline'; Codehighlighter1_713_769_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img id=Codehighlighter1_713_769_Closed_Image onclick="this.style.display='none'; Codehighlighter1_713_769_Closed_Text.style.display='none'; Codehighlighter1_713_769_Open_Image.style.display='inline'; Codehighlighter1_713_769_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((G[l][r][c-1]=='.'||G[l][r][c-1]&gt;G[l][r][c])&amp;&amp;G[l][r][c-1]!=0&amp;&amp;G[l][r][c-1]!='#')&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{&nbsp;G[l][r][c-1]=G[l][r][c]+1;g[l][r][c-1]=1;dfs(l,r,c-1);}<br>13<img id=Codehighlighter1_861_917_Open_Image onclick="this.style.display='none'; Codehighlighter1_861_917_Open_Text.style.display='none'; Codehighlighter1_861_917_Closed_Image.style.display='inline'; Codehighlighter1_861_917_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img id=Codehighlighter1_861_917_Closed_Image onclick="this.style.display='none'; Codehighlighter1_861_917_Closed_Text.style.display='none'; Codehighlighter1_861_917_Open_Image.style.display='inline'; Codehighlighter1_861_917_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((G[l][r][c+1]=='.'||G[l][r][c+1]&gt;G[l][r][c])&amp;&amp;G[l][r][c+1]!=0&amp;&amp;G[l][r][c+1]!='#')&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{&nbsp;G[l][r][c+1]=G[l][r][c]+1;g[l][r][c+1]=1;dfs(l,r,c+1);}<br>14<img id=Codehighlighter1_1009_1065_Open_Image onclick="this.style.display='none'; Codehighlighter1_1009_1065_Open_Text.style.display='none'; Codehighlighter1_1009_1065_Closed_Image.style.display='inline'; Codehighlighter1_1009_1065_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img id=Codehighlighter1_1009_1065_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1009_1065_Closed_Text.style.display='none'; Codehighlighter1_1009_1065_Open_Image.style.display='inline'; Codehighlighter1_1009_1065_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((G[l-1][r][c]=='.'||G[l-1][r][c]&gt;G[l][r][c])&amp;&amp;G[l-1][r][c]!=0&amp;&amp;G[l-1][r][c]!='#')&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{&nbsp;G[l-1][r][c]=G[l][r][c]+1;g[l-1][r][c]=1;dfs(l-1,r,c);}<br>15<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}<br>16<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br>17<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">int&nbsp;main()<br>18<img id=Codehighlighter1_1081_2052_Open_Image onclick="this.style.display='none'; Codehighlighter1_1081_2052_Open_Text.style.display='none'; Codehighlighter1_1081_2052_Closed_Image.style.display='inline'; Codehighlighter1_1081_2052_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img id=Codehighlighter1_1081_2052_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1081_2052_Closed_Text.style.display='none'; Codehighlighter1_1081_2052_Open_Image.style.display='inline'; Codehighlighter1_1081_2052_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"><img src="http://www.cppblog.com/Images/dot.gif">{&nbsp;&nbsp;&nbsp;&nbsp;<br>19<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;i,j,k,l,r,c,sl,sr,sc,el,er,ec;&nbsp;&nbsp;//起点，终点&nbsp;<br>20<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(cin&gt;&gt;l&gt;&gt;r&gt;&gt;c,l||r||c)<br>21<img id=Codehighlighter1_1175_2012_Open_Image onclick="this.style.display='none'; Codehighlighter1_1175_2012_Open_Text.style.display='none'; Codehighlighter1_1175_2012_Closed_Image.style.display='inline'; Codehighlighter1_1175_2012_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img id=Codehighlighter1_1175_2012_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1175_2012_Closed_Text.style.display='none'; Codehighlighter1_1175_2012_Open_Image.style.display='inline'; Codehighlighter1_1175_2012_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{<br>22<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(G,0,sizeof&nbsp;(G));<br>23<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(g,0,sizeof&nbsp;(g));<br>24<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;&nbsp;i&lt;=l&nbsp;;i++)<br>25<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(j=1;&nbsp;j&lt;=r;&nbsp;j++)<br>26<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(k=1;&nbsp;k&lt;=c;&nbsp;k++)<br>27<img id=Codehighlighter1_1333_1542_Open_Image onclick="this.style.display='none'; Codehighlighter1_1333_1542_Open_Text.style.display='none'; Codehighlighter1_1333_1542_Closed_Image.style.display='inline'; Codehighlighter1_1333_1542_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img id=Codehighlighter1_1333_1542_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1333_1542_Closed_Text.style.display='none'; Codehighlighter1_1333_1542_Open_Image.style.display='inline'; Codehighlighter1_1333_1542_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{<br>28<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin&gt;&gt;G[i][j][k];<br>29<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(G[i][j][k]=='S')<br>30<img id=Codehighlighter1_1402_1450_Open_Image onclick="this.style.display='none'; Codehighlighter1_1402_1450_Open_Text.style.display='none'; Codehighlighter1_1402_1450_Closed_Image.style.display='inline'; Codehighlighter1_1402_1450_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img id=Codehighlighter1_1402_1450_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1402_1450_Closed_Text.style.display='none'; Codehighlighter1_1402_1450_Open_Image.style.display='inline'; Codehighlighter1_1402_1450_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{&nbsp;sl=i;&nbsp;sr=j;&nbsp;sc=j;&nbsp;G[i][j][k]=1;&nbsp;g[i][j][k]=1;&nbsp;}<br>31<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if(G[i][j][k]=='E')<br>32<img id=Codehighlighter1_1497_1532_Open_Image onclick="this.style.display='none'; Codehighlighter1_1497_1532_Open_Text.style.display='none'; Codehighlighter1_1497_1532_Closed_Image.style.display='inline'; Codehighlighter1_1497_1532_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img id=Codehighlighter1_1497_1532_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1497_1532_Closed_Text.style.display='none'; Codehighlighter1_1497_1532_Open_Image.style.display='inline'; Codehighlighter1_1497_1532_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{el=i;&nbsp;er=j;&nbsp;ec=k;&nbsp;G[i][j][k]=127;&nbsp;}<br>33<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>34<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>35<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(sl,sr,sc);&nbsp;<br>36<img id=Codehighlighter1_1594_1885_Open_Image onclick="this.style.display='none'; Codehighlighter1_1594_1885_Open_Text.style.display='none'; Codehighlighter1_1594_1885_Closed_Image.style.display='inline'; Codehighlighter1_1594_1885_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img id=Codehighlighter1_1594_1885_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1594_1885_Closed_Text.style.display='none'; Codehighlighter1_1594_1885_Open_Image.style.display='inline'; Codehighlighter1_1594_1885_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/**//*&nbsp;&nbsp;for(i=1;&nbsp;i&lt;=l&nbsp;;&nbsp;i++,cout&lt;&lt;endl)<br>37<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(j=1;&nbsp;j&lt;=r;&nbsp;j++,cout&lt;&lt;endl)<br>38<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(k=1;&nbsp;k&lt;=c;&nbsp;k++)<br>39<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;(int)G[i][j][k]&lt;&lt;"&nbsp;";<br>40<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;&nbsp;i&lt;=l&nbsp;;&nbsp;i++,cout&lt;&lt;endl)<br>41<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(j=1;&nbsp;j&lt;=r;&nbsp;j++,cout&lt;&lt;endl)<br>42<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(k=1;&nbsp;k&lt;=c;&nbsp;k++)<br>43<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;(int)g[i][j][k]&lt;&lt;"&nbsp;";<br>44<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/if(g[el][er][ec]==0)cout&lt;&lt;"Trapped!"&lt;&lt;endl;<br>45<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;cout&lt;&lt;"Escaped&nbsp;in&nbsp;"&lt;&lt;int(G[el][er][ec]-1)&lt;&lt;"&nbsp;minute(s)."&lt;&lt;endl;<br>46<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>47<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;system("pause");<br>48<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;<br>49<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</div>
<br>写个BFS终于过了，只要在平面的迷宫基础上在垂直方向往上，往下都搜两次就可以了<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 180%; PADDING-RIGHT: 5px; FONT-FAMILY: Verdana; FONT-SIZE: 12pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span>&nbsp;<span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span>&nbsp;<span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span>&nbsp;<span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">queue</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;4</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;5</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;G[</span><span style="COLOR: #000000">35</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">35</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">35</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;6</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cnt[</span><span style="COLOR: #000000">35</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">35</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">35</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;7</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;type<br></span><span style="COLOR: #008080">&nbsp;8</span>&nbsp;<span style="COLOR: #000000">{&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;l,r,c;&nbsp;};<br></span><span style="COLOR: #008080">&nbsp;9</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">10</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">11</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">12</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;l,c,r,sl,sc,sr,el,ec,er,i,j,k;<br></span><span style="COLOR: #008080">13</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;bfs(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sl,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sr,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sc);<br></span><span style="COLOR: #008080">14</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">l</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">r</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">c,l</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">c</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">r)<br></span><span style="COLOR: #008080">15</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="COLOR: #008080">16</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(G,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">&nbsp;(G));<br></span><span style="COLOR: #008080">17</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(cnt,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">&nbsp;(cnt));<br></span><span style="COLOR: #008080">18</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">l;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">19</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">r;&nbsp;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">20</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;k</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">c;&nbsp;k</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">21</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="COLOR: #008080">22</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">G[i][j][k];<br></span><span style="COLOR: #008080">23</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(G[i][j][k]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">S</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">){sl</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;&nbsp;sr</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j;&nbsp;sc</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">k;&nbsp;}<br></span><span style="COLOR: #008080">24</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(G[i][j][k]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">E</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">){el</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;&nbsp;er</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j;&nbsp;ec</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">k;&nbsp;}<br></span><span style="COLOR: #008080">25</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">26</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">27</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bfs(sl,sr,sc);<br></span><span style="COLOR: #008080">28</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;for(i=1;&nbsp;i&lt;=l;&nbsp;i++,cout&lt;&lt;endl)<br></span><span style="COLOR: #008080">29</span>&nbsp;<span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;for(j=1;&nbsp;j&lt;=r;&nbsp;j++,cout&lt;&lt;endl)<br></span><span style="COLOR: #008080">30</span>&nbsp;<span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">for(k=1;&nbsp;k&lt;=c;&nbsp;k++)<br></span><span style="COLOR: #008080">31</span>&nbsp;<span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">cout&lt;&lt;cnt[i][j][k]&lt;&lt;'&nbsp;';</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">32</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">33</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(el</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">sl</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">er</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">sr</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">ec</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">sc)cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Escaped&nbsp;in&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;minute(s).&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">34</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(cnt[el][er][ec]</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Escaped&nbsp;in&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">cnt[el][er][ec]</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;minute(s).&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">35</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Trapped!&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">36</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">37</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;system("pause");</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">38</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">39</span>&nbsp;<span style="COLOR: #000000">}<br></span><span style="COLOR: #008080">40</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">41</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;valid(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;l,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;r,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;c)<br></span><span style="COLOR: #008080">42</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">43</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;(G[l][r][c]</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">||</span><span style="COLOR: #000000">G[l][r][c]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">E</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">);&nbsp;&nbsp;<br></span><span style="COLOR: #008080">44</span>&nbsp;<span style="COLOR: #000000">}<br></span><span style="COLOR: #008080">45</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">46</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;bfs(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sl,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sr,&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sc)<br></span><span style="COLOR: #008080">47</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">48</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;queue</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;type</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;q;<br></span><span style="COLOR: #008080">49</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;type&nbsp;tem;&nbsp;tem.l</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sl;&nbsp;tem.r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sr;&nbsp;tem.c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sc;&nbsp;<br></span><span style="COLOR: #008080">50</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;q.push(tem);<br></span><span style="COLOR: #008080">51</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(q.size())<br></span><span style="COLOR: #008080">52</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;{<br></span><span style="COLOR: #008080">53</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;tem</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">q.front();&nbsp;q.pop();<br></span><span style="COLOR: #008080">54</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;l</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tem.l,r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tem.r,c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tem.c;<br></span><span style="COLOR: #008080">55</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(l,r</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,c)</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">cnt[l][r</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][c]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">){cnt[l][r</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][c]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">cnt[l][r][c]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;tem.l</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">l;&nbsp;tem.r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">r</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;tem.c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">c;&nbsp;q.push(tem);&nbsp;}<br></span><span style="COLOR: #008080">56</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(l,r</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,c)</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">cnt[l][r</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][c]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">){cnt[l][r</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][c]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">cnt[l][r][c]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;tem.l</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">l;&nbsp;tem.r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">r</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;tem.c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">c;&nbsp;q.push(tem);&nbsp;}<br></span><span style="COLOR: #008080">57</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(l,r,c</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">cnt[l][r][c</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">){cnt[l][r][c</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">cnt[l][r][c]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;tem.l</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">l;&nbsp;tem.r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">r;&nbsp;tem.c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">c</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;q.push(tem);&nbsp;}<br></span><span style="COLOR: #008080">58</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(l,r,c</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">cnt[l][r][c</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">){cnt[l][r][c</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">cnt[l][r][c]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;tem.l</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">l;&nbsp;tem.r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">r;&nbsp;tem.c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">c</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;q.push(tem);&nbsp;}<br></span><span style="COLOR: #008080">59</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(l</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,r,c)</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">cnt[l</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][r][c]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">){cnt[l</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][r][c]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">cnt[l][r][c]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;tem.l</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">l</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;tem.r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">r;&nbsp;tem.c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">c;&nbsp;q.push(tem);&nbsp;}<br></span><span style="COLOR: #008080">60</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(l</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,r,c)</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">cnt[l</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][r][c]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">){cnt[l</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][r][c]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">cnt[l][r][c]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;tem.l</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">l</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;tem.r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">r;&nbsp;tem.c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">c;&nbsp;q.push(tem);&nbsp;}<br></span><span style="COLOR: #008080">61</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">62</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">63</span>&nbsp;<span style="COLOR: #000000">}</span></div>
<img src ="http://www.cppblog.com/tianlearn-language/aggbug/116643.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tianlearn-language/" target="_blank">田兵</a> 2010-05-28 21:50 <a href="http://www.cppblog.com/tianlearn-language/archive/2010/05/28/116643.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>AOJ 1005  Hero In Maze BFS</title><link>http://www.cppblog.com/tianlearn-language/archive/2010/05/20/115973.html</link><dc:creator>田兵</dc:creator><author>田兵</author><pubDate>Thu, 20 May 2010 13:58:00 GMT</pubDate><guid>http://www.cppblog.com/tianlearn-language/archive/2010/05/20/115973.html</guid><wfw:comment>http://www.cppblog.com/tianlearn-language/comments/115973.html</wfw:comment><comments>http://www.cppblog.com/tianlearn-language/archive/2010/05/20/115973.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/tianlearn-language/comments/commentRss/115973.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tianlearn-language/services/trackbacks/115973.html</trackback:ping><description><![CDATA[<p align=center><font color=blue size=5>Hero In Maze</font></p>
<p align=center>Time Limit:JAVA/Others2000/1000MS&nbsp; Memory Limit:JAVA/Others131072/65536KB<br>Total Submit:240 Accepted:51 </p>
<p align=left><strong><font color=#333399 size=5>Description</font> </strong>
<div id=problemcontent>500年前，Jesse是我国最卓越的剑客。他英俊潇洒，而且机智过人^_^。 <br><br>突然有一天，Jesse心爱的公主被魔王困在了一个巨大的迷宫中。Jesse听说这个消息已经是两天以后了，他知道公主在迷宫中还能坚持T天，他急忙赶到迷宫，开始到处寻找公主的下落。 <br>时间一点一点的过去，Jesse还是无法找到公主。最后当他找到公主的时候，美丽的公主已经死了。从此Jesse郁郁寡欢，茶饭不思，一年后追随公主而去了。T_T <br>500年后的今天，Jesse托梦给你，希望你帮他判断一下当年他是否有机会在给定的时间内找到公主。 <br><br>他会为你提供迷宫的地图以及所剩的时间T。请你判断他是否能救出心爱的公主。</div>
<p align=left><strong><font color=#333399 size=5>Input</font> </strong>
<div id=problemcontent>题目包括多组测试数据。 <br>每组测试数据以三个整数N,M,T(0<n, t m&#8804;20,>0)开头，分别代表迷宫的长和高，以及公主能坚持的天数。 <br>紧接着有M行，N列字符，由"."，"*"，"P"，"S"组成。其中 <br>"." 代表能够行走的空地。 <br>"*" 代表墙壁，Jesse不能从此通过。 <br>"P" 是公主所在的位置。 <br>"S" 是Jesse的起始位置。 <br>每个时间段里Jesse只能选择&#8220;上、下、左、右&#8221;任意一方向走一步。 <br>输入以0 0 0结束。 <br><br></div>
<p align=left><strong><font color=#333399 size=5>Output</font> </strong>
<div id=problemcontent>如果能在规定时间内救出公主输出&#8220;YES&#8221;，否则输出&#8220;NO&#8221;。</div>
<p align=left><strong><font color=#333399 size=5>Sample Input</font> </strong>
<div id=problemcontent>
<pre>4 4 10
....
....
....
S**P
0 0 0</pre>
</div>
<p align=left><strong><font color=#333399 size=5>Sample Output</font> </strong>
<div id=problemcontent>
<pre>YES</pre>
</div>
<br>写的第一个BFS，激动！！<br>代码如下，高手飘过，你们根本不需要看，太初级了。<br>这个代码主要两个地方花了很多时间：<br>1，队列本应从队头取元素的，我从尾部取了，后来用queue的front和push可以很方便的实现。<br>2.，是没有考虑公主被包围的情况，即公主周围全是&#8216;*&#8217;，那么王子就没办法达到，这样dist[x2][y2]（x2，y2是公主的位置）一直是0,肯定小于t；<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 150%; PADDING-RIGHT: 5px; FONT-FAMILY: Verdana; FONT-SIZE: 12pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">queue</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;a[</span><span style="COLOR: #000000">105</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">105</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;f[</span><span style="COLOR: #000000">105</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">105</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;dist[</span><span style="COLOR: #000000">105</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">105</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;point&nbsp;<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img id=Codehighlighter1_126_137_Open_Image onclick="this.style.display='none'; Codehighlighter1_126_137_Open_Text.style.display='none'; Codehighlighter1_126_137_Closed_Image.style.display='inline'; Codehighlighter1_126_137_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_126_137_Closed_Image onclick="this.style.display='none'; Codehighlighter1_126_137_Closed_Text.style.display='none'; Codehighlighter1_126_137_Open_Image.style.display='inline'; Codehighlighter1_126_137_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_126_137_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_126_137_Open_Text><span style="COLOR: #000000">{&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x,y;&nbsp;}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;valid(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;y)<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img id=Codehighlighter1_165_207_Open_Image onclick="this.style.display='none'; Codehighlighter1_165_207_Open_Text.style.display='none'; Codehighlighter1_165_207_Closed_Image.style.display='inline'; Codehighlighter1_165_207_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_165_207_Closed_Image onclick="this.style.display='none'; Codehighlighter1_165_207_Closed_Text.style.display='none'; Codehighlighter1_165_207_Open_Image.style.display='inline'; Codehighlighter1_165_207_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_165_207_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_165_207_Open_Text><span style="COLOR: #000000">{&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">f[x][y]</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">a[x][y]</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">&amp;&amp;</span><span style="COLOR: #000000">a[x][y];&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;bfs(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j)<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img id=Codehighlighter1_232_861_Open_Image onclick="this.style.display='none'; Codehighlighter1_232_861_Open_Text.style.display='none'; Codehighlighter1_232_861_Closed_Image.style.display='inline'; Codehighlighter1_232_861_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_232_861_Closed_Image onclick="this.style.display='none'; Codehighlighter1_232_861_Closed_Text.style.display='none'; Codehighlighter1_232_861_Open_Image.style.display='inline'; Codehighlighter1_232_861_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_232_861_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_232_861_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[i][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;f[i][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;point&nbsp;tem;<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tem.x</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;&nbsp;tem.y</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j;<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;queue</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;point</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">q;<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.push(tem);<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(q.size())<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img id=Codehighlighter1_383_854_Open_Image onclick="this.style.display='none'; Codehighlighter1_383_854_Open_Text.style.display='none'; Codehighlighter1_383_854_Closed_Image.style.display='inline'; Codehighlighter1_383_854_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_383_854_Closed_Image onclick="this.style.display='none'; Codehighlighter1_383_854_Closed_Text.style.display='none'; Codehighlighter1_383_854_Open_Image.style.display='inline'; Codehighlighter1_383_854_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_383_854_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_383_854_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tem</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">q.front();q.pop();&nbsp;<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tem.x,y</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tem.y;<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img id=Codehighlighter1_473_546_Open_Image onclick="this.style.display='none'; Codehighlighter1_473_546_Open_Text.style.display='none'; Codehighlighter1_473_546_Closed_Image.style.display='inline'; Codehighlighter1_473_546_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_473_546_Closed_Image onclick="this.style.display='none'; Codehighlighter1_473_546_Closed_Text.style.display='none'; Codehighlighter1_473_546_Open_Image.style.display='inline'; Codehighlighter1_473_546_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,y))</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_473_546_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_473_546_Open_Text><span style="COLOR: #000000">{f[x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][y]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;&nbsp;dist[x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][y]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dist[x][y]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;tem.x</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;tem.y</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">y;&nbsp;q.push(tem);}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img id=Codehighlighter1_573_646_Open_Image onclick="this.style.display='none'; Codehighlighter1_573_646_Open_Text.style.display='none'; Codehighlighter1_573_646_Closed_Image.style.display='inline'; Codehighlighter1_573_646_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_573_646_Closed_Image onclick="this.style.display='none'; Codehighlighter1_573_646_Closed_Text.style.display='none'; Codehighlighter1_573_646_Open_Image.style.display='inline'; Codehighlighter1_573_646_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(x,y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">))</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_573_646_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_573_646_Open_Text><span style="COLOR: #000000">{f[x][y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;&nbsp;dist[x][y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dist[x][y]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;tem.x</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">x;&nbsp;tem.y</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;q.push(tem);}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img id=Codehighlighter1_673_746_Open_Image onclick="this.style.display='none'; Codehighlighter1_673_746_Open_Text.style.display='none'; Codehighlighter1_673_746_Closed_Image.style.display='inline'; Codehighlighter1_673_746_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_673_746_Closed_Image onclick="this.style.display='none'; Codehighlighter1_673_746_Closed_Text.style.display='none'; Codehighlighter1_673_746_Open_Image.style.display='inline'; Codehighlighter1_673_746_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,y))</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_673_746_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_673_746_Open_Text><span style="COLOR: #000000">{f[x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][y]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;&nbsp;dist[x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][y]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dist[x][y]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;tem.x</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;tem.y</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">y;&nbsp;q.push(tem);}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img id=Codehighlighter1_773_846_Open_Image onclick="this.style.display='none'; Codehighlighter1_773_846_Open_Text.style.display='none'; Codehighlighter1_773_846_Closed_Image.style.display='inline'; Codehighlighter1_773_846_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_773_846_Closed_Image onclick="this.style.display='none'; Codehighlighter1_773_846_Closed_Text.style.display='none'; Codehighlighter1_773_846_Open_Image.style.display='inline'; Codehighlighter1_773_846_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(valid(x,y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">))</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_773_846_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_773_846_Open_Text><span style="COLOR: #000000">{f[x][y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;&nbsp;dist[x][y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dist[x][y]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;tem.x</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">x;&nbsp;tem.y</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;q.push(tem);}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img id=Codehighlighter1_875_1476_Open_Image onclick="this.style.display='none'; Codehighlighter1_875_1476_Open_Text.style.display='none'; Codehighlighter1_875_1476_Closed_Image.style.display='inline'; Codehighlighter1_875_1476_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_875_1476_Closed_Image onclick="this.style.display='none'; Codehighlighter1_875_1476_Closed_Text.style.display='none'; Codehighlighter1_875_1476_Open_Image.style.display='inline'; Codehighlighter1_875_1476_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_875_1476_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_875_1476_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,m,t,i,j,u;<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x,y,x2,y2;<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">m</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">t,n</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">m</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">t)<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img id=Codehighlighter1_953_1456_Open_Image onclick="this.style.display='none'; Codehighlighter1_953_1456_Open_Text.style.display='none'; Codehighlighter1_953_1456_Closed_Image.style.display='inline'; Codehighlighter1_953_1456_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_953_1456_Closed_Image onclick="this.style.display='none'; Codehighlighter1_953_1456_Closed_Text.style.display='none'; Codehighlighter1_953_1456_Open_Image.style.display='inline'; Codehighlighter1_953_1456_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_953_1456_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_953_1456_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(a,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">&nbsp;a);<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(f,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">&nbsp;f);<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dist,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">&nbsp;dist);<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">x2</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">y2</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">m;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img id=Codehighlighter1_1122_1257_Open_Image onclick="this.style.display='none'; Codehighlighter1_1122_1257_Open_Text.style.display='none'; Codehighlighter1_1122_1257_Closed_Image.style.display='inline'; Codehighlighter1_1122_1257_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1122_1257_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1122_1257_Closed_Text.style.display='none'; Codehighlighter1_1122_1257_Open_Image.style.display='inline'; Codehighlighter1_1122_1257_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1122_1257_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1122_1257_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">a[i][j];<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img id=Codehighlighter1_1186_1196_Open_Image onclick="this.style.display='none'; Codehighlighter1_1186_1196_Open_Text.style.display='none'; Codehighlighter1_1186_1196_Closed_Image.style.display='inline'; Codehighlighter1_1186_1196_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1186_1196_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1186_1196_Closed_Text.style.display='none'; Codehighlighter1_1186_1196_Open_Image.style.display='inline'; Codehighlighter1_1186_1196_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(a[i][j]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">S</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1186_1196_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1186_1196_Open_Text><span style="COLOR: #000000">{x</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;&nbsp;y</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img id=Codehighlighter1_1235_1248_Open_Image onclick="this.style.display='none'; Codehighlighter1_1235_1248_Open_Text.style.display='none'; Codehighlighter1_1235_1248_Closed_Image.style.display='inline'; Codehighlighter1_1235_1248_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1235_1248_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1235_1248_Closed_Text.style.display='none'; Codehighlighter1_1235_1248_Open_Image.style.display='inline'; Codehighlighter1_1235_1248_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(a[i][j]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">P</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1235_1248_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1235_1248_Open_Text><span style="COLOR: #000000">{x2</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;&nbsp;y2</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bfs(x,y);<br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">52</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(x</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">x2</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">y2)cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">YES</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">53</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(dist[x2][y2]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">NO</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">54</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(dist[x2][y2]</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">t)cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">YES</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">55</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">NO</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">56</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">57</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;<br></span><span style="COLOR: #008080">58</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">59</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
<img src ="http://www.cppblog.com/tianlearn-language/aggbug/115973.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tianlearn-language/" target="_blank">田兵</a> 2010-05-20 21:58 <a href="http://www.cppblog.com/tianlearn-language/archive/2010/05/20/115973.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UVa 673 Parentheses Balance </title><link>http://www.cppblog.com/tianlearn-language/archive/2010/05/11/115082.html</link><dc:creator>田兵</dc:creator><author>田兵</author><pubDate>Tue, 11 May 2010 02:52:00 GMT</pubDate><guid>http://www.cppblog.com/tianlearn-language/archive/2010/05/11/115082.html</guid><wfw:comment>http://www.cppblog.com/tianlearn-language/comments/115082.html</wfw:comment><comments>http://www.cppblog.com/tianlearn-language/archive/2010/05/11/115082.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/tianlearn-language/comments/commentRss/115082.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tianlearn-language/services/trackbacks/115082.html</trackback:ping><description><![CDATA[<p>
<table border=0 cellSpacing=0 cellPadding=4 width="100%">
    <tbody>
        <tr class=sectiontableentry1>
            <td align=right><a href="http://www.cppblog.com/tianlearn-language/admin/index.php?option=com_onlinejudge&amp;page=show_problem&amp;problem=614"><u><font color=#0066cc>673</font></u></a></td>
            <td><a href="http://www.cppblog.com/tianlearn-language/admin/index.php?option=com_onlinejudge&amp;page=show_problem&amp;problem=614"><u><font color=#0066cc>Parentheses Balance</font></u></a></td>
            <td>Accepted</td>
            <td>C++</td>
            <td>0.192</td>
        </tr>
    </tbody>
</table>
<br>简单的括号匹配：<br>wa了几次<br><br></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-FAMILY: Arial; FONT-SIZE: 12pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px">
<h3 style="FONT-SIZE: 14pt"><img id=Code_Closed_Image_105100 onclick="this.style.display='none'; Code_Closed_Text_105100.style.display='none'; Code_Open_Image_105100.style.display='inline'; Code_Open_Text_105100.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 height=16><img style="DISPLAY: none" id=Code_Open_Image_105100 onclick="this.style.display='none'; Code_Open_Text_105100.style.display='none'; Code_Closed_Image_105100.style.display='inline'; Code_Closed_Text_105100.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 height=16><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Code_Closed_Text_105100>代码：</span><span style="DISPLAY: none" id=Code_Open_Text_105100><br><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stack</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img id=Codehighlighter1_84_850_Open_Image onclick="this.style.display='none'; Codehighlighter1_84_850_Open_Text.style.display='none'; Codehighlighter1_84_850_Closed_Image.style.display='inline'; Codehighlighter1_84_850_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_84_850_Closed_Image onclick="this.style.display='none'; Codehighlighter1_84_850_Closed_Text.style.display='none'; Codehighlighter1_84_850_Open_Image.style.display='inline'; Codehighlighter1_84_850_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_84_850_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_84_850_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,t;<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">t;<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;str;<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(t</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img id=Codehighlighter1_161_829_Open_Image onclick="this.style.display='none'; Codehighlighter1_161_829_Open_Text.style.display='none'; Codehighlighter1_161_829_Closed_Image.style.display='inline'; Codehighlighter1_161_829_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_161_829_Closed_Image onclick="this.style.display='none'; Codehighlighter1_161_829_Closed_Text.style.display='none'; Codehighlighter1_161_829_Open_Image.style.display='inline'; Codehighlighter1_161_829_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_161_829_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_161_829_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">cin&gt;&gt;str;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">14</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getline(cin,str);<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img id=Codehighlighter1_225_253_Open_Image onclick="this.style.display='none'; Codehighlighter1_225_253_Open_Text.style.display='none'; Codehighlighter1_225_253_Closed_Image.style.display='inline'; Codehighlighter1_225_253_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_225_253_Closed_Image onclick="this.style.display='none'; Codehighlighter1_225_253_Closed_Text.style.display='none'; Codehighlighter1_225_253_Open_Image.style.display='inline'; Codehighlighter1_225_253_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(str.size()</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_225_253_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_225_253_Open_Text><span style="COLOR: #000000">{cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Yes</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;</span><span style="COLOR: #0000ff">continue</span><span style="COLOR: #000000">;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stack</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;s;<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">str.size();i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img id=Codehighlighter1_311_728_Open_Image onclick="this.style.display='none'; Codehighlighter1_311_728_Open_Text.style.display='none'; Codehighlighter1_311_728_Closed_Image.style.display='inline'; Codehighlighter1_311_728_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_311_728_Closed_Image onclick="this.style.display='none'; Codehighlighter1_311_728_Closed_Text.style.display='none'; Codehighlighter1_311_728_Open_Image.style.display='inline'; Codehighlighter1_311_728_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_311_728_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_311_728_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(str[i]</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">||</span><span style="COLOR: #000000">str[i]</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">)s.push(str[i]);<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img id=Codehighlighter1_389_721_Open_Image onclick="this.style.display='none'; Codehighlighter1_389_721_Open_Text.style.display='none'; Codehighlighter1_389_721_Closed_Image.style.display='inline'; Codehighlighter1_389_721_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_389_721_Closed_Image onclick="this.style.display='none'; Codehighlighter1_389_721_Closed_Text.style.display='none'; Codehighlighter1_389_721_Open_Image.style.display='inline'; Codehighlighter1_389_721_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_389_721_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_389_721_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(str[i]</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">)<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img id=Codehighlighter1_433_528_Open_Image onclick="this.style.display='none'; Codehighlighter1_433_528_Open_Text.style.display='none'; Codehighlighter1_433_528_Closed_Image.style.display='inline'; Codehighlighter1_433_528_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_433_528_Closed_Image onclick="this.style.display='none'; Codehighlighter1_433_528_Closed_Text.style.display='none'; Codehighlighter1_433_528_Open_Image.style.display='inline'; Codehighlighter1_433_528_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_433_528_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_433_528_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(s.empty()</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">s.top()</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">)</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;s.pop();<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(str[i]</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">)<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img id=Codehighlighter1_577_685_Open_Image onclick="this.style.display='none'; Codehighlighter1_577_685_Open_Text.style.display='none'; Codehighlighter1_577_685_Closed_Image.style.display='inline'; Codehighlighter1_577_685_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_577_685_Closed_Image onclick="this.style.display='none'; Codehighlighter1_577_685_Closed_Text.style.display='none'; Codehighlighter1_577_685_Open_Image.style.display='inline'; Codehighlighter1_577_685_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_577_685_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_577_685_Open_Text><span style="COLOR: #000000">{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(s.empty()</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">s.top()</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">)</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;s.pop();<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;//刚开始做就是忘了这句<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(s.empty()</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">str.size())cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Yes</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">No</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></span></h3>
</div>
<img src ="http://www.cppblog.com/tianlearn-language/aggbug/115082.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tianlearn-language/" target="_blank">田兵</a> 2010-05-11 10:52 <a href="http://www.cppblog.com/tianlearn-language/archive/2010/05/11/115082.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UVa 424 Integer Inquiry  大数加法</title><link>http://www.cppblog.com/tianlearn-language/archive/2010/05/10/115034.html</link><dc:creator>田兵</dc:creator><author>田兵</author><pubDate>Mon, 10 May 2010 13:37:00 GMT</pubDate><guid>http://www.cppblog.com/tianlearn-language/archive/2010/05/10/115034.html</guid><wfw:comment>http://www.cppblog.com/tianlearn-language/comments/115034.html</wfw:comment><comments>http://www.cppblog.com/tianlearn-language/archive/2010/05/10/115034.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tianlearn-language/comments/commentRss/115034.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tianlearn-language/services/trackbacks/115034.html</trackback:ping><description><![CDATA[<p>这个破题因为数组的边界问题花了我很长时间，也怪自己没有投入里面去</p>
大数加法：<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 18pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img id=Code_Closed_Image_213524 onclick="this.style.display='none'; Code_Closed_Text_213524.style.display='none'; Code_Open_Image_213524.style.display='inline'; Code_Open_Text_213524.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 height=16><img style="DISPLAY: none" id=Code_Open_Image_213524 onclick="this.style.display='none'; Code_Open_Text_213524.style.display='none'; Code_Closed_Image_213524.style.display='inline'; Code_Closed_Text_213524.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 height=16><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Code_Closed_Text_213524>大数加法</span><span style="DISPLAY: none" id=Code_Open_Text_213524><br><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstring</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;ans[</span><span style="COLOR: #000000">10005</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;a[</span><span style="COLOR: #000000">105</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;max</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;add(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">ans,</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">a)<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_105_478_Open_Image onclick="this.style.display='none'; Codehighlighter1_105_478_Open_Text.style.display='none'; Codehighlighter1_105_478_Closed_Image.style.display='inline'; Codehighlighter1_105_478_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_105_478_Closed_Image onclick="this.style.display='none'; Codehighlighter1_105_478_Closed_Text.style.display='none'; Codehighlighter1_105_478_Open_Image.style.display='inline'; Codehighlighter1_105_478_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_105_478_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_105_478_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">strlen(a),i,j,t,s;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">a&nbsp;反向&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">j;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">,j</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img id=Codehighlighter1_188_227_Open_Image onclick="this.style.display='none'; Codehighlighter1_188_227_Open_Text.style.display='none'; Codehighlighter1_188_227_Closed_Image.style.display='inline'; Codehighlighter1_188_227_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_188_227_Closed_Image onclick="this.style.display='none'; Codehighlighter1_188_227_Closed_Text.style.display='none'; Codehighlighter1_188_227_Open_Image.style.display='inline'; Codehighlighter1_188_227_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_188_227_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_188_227_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">a[i];&nbsp;a[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">a[j];&nbsp;a[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">t;<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img id=Codehighlighter1_274_357_Open_Image onclick="this.style.display='none'; Codehighlighter1_274_357_Open_Text.style.display='none'; Codehighlighter1_274_357_Closed_Image.style.display='inline'; Codehighlighter1_274_357_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_274_357_Closed_Image onclick="this.style.display='none'; Codehighlighter1_274_357_Closed_Text.style.display='none'; Codehighlighter1_274_357_Open_Image.style.display='inline'; Codehighlighter1_274_357_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_274_357_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_274_357_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">ans[i]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">a[i]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">t;<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">s</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">s</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(t)<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img id=Codehighlighter1_376_454_Open_Image onclick="this.style.display='none'; Codehighlighter1_376_454_Open_Text.style.display='none'; Codehighlighter1_376_454_Closed_Image.style.display='inline'; Codehighlighter1_376_454_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_376_454_Closed_Image onclick="this.style.display='none'; Codehighlighter1_376_454_Closed_Text.style.display='none'; Codehighlighter1_376_454_Open_Image.style.display='inline'; Codehighlighter1_376_454_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_376_454_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_376_454_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">ans[i]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">t;<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">s</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">s</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">max)&nbsp;&nbsp;max</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img id=Codehighlighter1_491_755_Open_Image onclick="this.style.display='none'; Codehighlighter1_491_755_Open_Text.style.display='none'; Codehighlighter1_491_755_Closed_Image.style.display='inline'; Codehighlighter1_491_755_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_491_755_Closed_Image onclick="this.style.display='none'; Codehighlighter1_491_755_Closed_Text.style.display='none'; Codehighlighter1_491_755_Open_Image.style.display='inline'; Codehighlighter1_491_755_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_491_755_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_491_755_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,i;<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;memset(ans,</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(ans));<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,a),strcmp(a,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">))<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img id=Codehighlighter1_582_621_Open_Image onclick="this.style.display='none'; Codehighlighter1_582_621_Open_Text.style.display='none'; Codehighlighter1_582_621_Closed_Image.style.display='inline'; Codehighlighter1_582_621_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_582_621_Closed_Image onclick="this.style.display='none'; Codehighlighter1_582_621_Closed_Text.style.display='none'; Codehighlighter1_582_621_Open_Image.style.display='inline'; Codehighlighter1_582_621_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_582_621_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_582_621_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(ans,a);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;n</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">strlen(ans);<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">max</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img id=Codehighlighter1_673_699_Open_Image onclick="this.style.display='none'; Codehighlighter1_673_699_Open_Text.style.display='none'; Codehighlighter1_673_699_Closed_Image.style.display='inline'; Codehighlighter1_673_699_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_673_699_Closed_Image onclick="this.style.display='none'; Codehighlighter1_673_699_Closed_Text.style.display='none'; Codehighlighter1_673_699_Open_Image.style.display='inline'; Codehighlighter1_673_699_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_673_699_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_673_699_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;putchar(ans[i]);<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;putchar(</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n);<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></span></div>
<img src ="http://www.cppblog.com/tianlearn-language/aggbug/115034.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tianlearn-language/" target="_blank">田兵</a> 2010-05-10 21:37 <a href="http://www.cppblog.com/tianlearn-language/archive/2010/05/10/115034.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>401 Palindromes</title><link>http://www.cppblog.com/tianlearn-language/archive/2010/05/09/114956.html</link><dc:creator>田兵</dc:creator><author>田兵</author><pubDate>Sun, 09 May 2010 14:41:00 GMT</pubDate><guid>http://www.cppblog.com/tianlearn-language/archive/2010/05/09/114956.html</guid><wfw:comment>http://www.cppblog.com/tianlearn-language/comments/114956.html</wfw:comment><comments>http://www.cppblog.com/tianlearn-language/archive/2010/05/09/114956.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tianlearn-language/comments/commentRss/114956.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tianlearn-language/services/trackbacks/114956.html</trackback:ping><description><![CDATA[<table class=FCK__ShowTableBorders border=0 cellSpacing=0 cellPadding=4 width="100%">
    <tbody>
        <tr class=sectiontableentry1>
            <td align=right><a href="http://hi.baidu.com/fc/editor/index.php?option=com_onlinejudge&amp;page=show_problem&amp;problem=342">401</a></td>
            <td><a href="http://hi.baidu.com/fc/editor/index.php?option=com_onlinejudge&amp;page=show_problem&amp;problem=342">Palindromes</a></td>
            <td>
            <p>Accepted</p>
            </td>
        </tr>
    </tbody>
</table>
<div style="BORDER-BOTTOM: #cccccc; BORDER-LEFT: #cccccc; PADDING-BOTTOM: 8px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 8px; WIDTH: 200%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc; BORDER-RIGHT: #cccccc; PADDING-TOP: 4px"><span style="FONT-SIZE: 18pt"><font style="BACKGROUND-COLOR: #eeeeee">效</font>率还不错哦<br>&nbsp;1<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include&lt;iostream&gt;<br>&nbsp;2<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include&lt;string&gt;<br>&nbsp;3<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">using&nbsp;namespace&nbsp;std;<br>&nbsp;4<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br>&nbsp;5<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">bool&nbsp;pali(string&nbsp;str)<br>&nbsp;6<img id=Codehighlighter1_80_178_Open_Image onclick="this.style.display='none'; Codehighlighter1_80_178_Open_Text.style.display='none'; Codehighlighter1_80_178_Closed_Image.style.display='inline'; Codehighlighter1_80_178_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img id=Codehighlighter1_80_178_Closed_Image onclick="this.style.display='none'; Codehighlighter1_80_178_Closed_Text.style.display='none'; Codehighlighter1_80_178_Open_Image.style.display='inline'; Codehighlighter1_80_178_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"><img src="http://www.cppblog.com/Images/dot.gif">{<br>&nbsp;7<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string&nbsp;s="";<br>&nbsp;8<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(int&nbsp;i=str.size()-1;&nbsp;i&gt;=0;&nbsp;i--)<br>&nbsp;9<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s+=str[i];<br>10<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;str==s;<br>11<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}<br>12<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br>13<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">bool&nbsp;morr(string&nbsp;str)<br>14<img id=Codehighlighter1_203_995_Open_Image onclick="this.style.display='none'; Codehighlighter1_203_995_Open_Text.style.display='none'; Codehighlighter1_203_995_Closed_Image.style.display='inline'; Codehighlighter1_203_995_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img id=Codehighlighter1_203_995_Closed_Image onclick="this.style.display='none'; Codehighlighter1_203_995_Closed_Text.style.display='none'; Codehighlighter1_203_995_Open_Image.style.display='inline'; Codehighlighter1_203_995_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"><img src="http://www.cppblog.com/Images/dot.gif">{<br>15<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;i;<br>16<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string&nbsp;s="";<br>17<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(&nbsp;i=str.size()-1;&nbsp;i&gt;=0;&nbsp;i--)<br>18<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s+=str[i];&nbsp;&nbsp;&nbsp;<br>19<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i=0;i&lt;s.size();i++)&nbsp;<br>20<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch(s[i])<br>21<img id=Codehighlighter1_348_972_Open_Image onclick="this.style.display='none'; Codehighlighter1_348_972_Open_Text.style.display='none'; Codehighlighter1_348_972_Closed_Image.style.display='inline'; Codehighlighter1_348_972_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img id=Codehighlighter1_348_972_Closed_Image onclick="this.style.display='none'; Codehighlighter1_348_972_Closed_Text.style.display='none'; Codehighlighter1_348_972_Open_Image.style.display='inline'; Codehighlighter1_348_972_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{<br>22<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'A':break;<br>23<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'H':break;<br>24<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'I':break;<br>25<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'M':break;<br>26<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'O':break;<br>27<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'T':break;<br>28<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'U':break;<br>29<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'V':break;<br>30<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'W':break;<br>31<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'X':break;<br>32<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'Y':break;<br>33<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'1':break;<br>34<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'8':break;<br>35<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'E':&nbsp;s[i]='3';break;<br>36<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'S':s[i]='2';break;<br>37<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'J':s[i]='L';break;<br>38<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'L':s[i]='J';break;<br>39<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'Z':s[i]='5';break;<br>40<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'2':s[i]='S';break;<br>41<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'3':s[i]='E';&nbsp;break;<br>42<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'5':s[i]='Z';break;<br>43<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:&nbsp;return&nbsp;false;&nbsp;break;<br>44<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>45<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;s==str;<br>46<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}<br>47<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">int&nbsp;main()<br>48<img id=Codehighlighter1_1008_1454_Open_Image onclick="this.style.display='none'; Codehighlighter1_1008_1454_Open_Text.style.display='none'; Codehighlighter1_1008_1454_Closed_Image.style.display='inline'; Codehighlighter1_1008_1454_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img id=Codehighlighter1_1008_1454_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1008_1454_Closed_Text.style.display='none'; Codehighlighter1_1008_1454_Open_Image.style.display='inline'; Codehighlighter1_1008_1454_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"><img src="http://www.cppblog.com/Images/dot.gif">{<br>49<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;string&nbsp;str;<br>50<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;bool&nbsp;p,m;<br>51<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;while(cin&gt;&gt;str)<br>52<img id=Codehighlighter1_1064_1441_Open_Image onclick="this.style.display='none'; Codehighlighter1_1064_1441_Open_Text.style.display='none'; Codehighlighter1_1064_1441_Closed_Image.style.display='inline'; Codehighlighter1_1064_1441_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img id=Codehighlighter1_1064_1441_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1064_1441_Closed_Text.style.display='none'; Codehighlighter1_1064_1441_Open_Image.style.display='inline'; Codehighlighter1_1064_1441_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">{<br>53<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p=pali(str);<br>54<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m=morr(str);<br>55<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;str;<br>56<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(p&amp;&amp;m)cout&lt;&lt;"&nbsp;--&nbsp;is&nbsp;a&nbsp;mirrored&nbsp;palindrome."&nbsp;&lt;&lt;endl;<br>57<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if(p&amp;&amp;!m)cout&lt;&lt;"&nbsp;--&nbsp;is&nbsp;a&nbsp;regular&nbsp;palindrome."&nbsp;&lt;&lt;endl;&nbsp;<br>58<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if(!p&amp;&amp;m)cout&lt;&lt;"&nbsp;--&nbsp;is&nbsp;a&nbsp;mirrored&nbsp;string."&nbsp;&lt;&lt;endl;<br>59<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;cout&lt;&lt;"&nbsp;--&nbsp;is&nbsp;not&nbsp;a&nbsp;palindrome."&nbsp;&lt;&lt;endl;&nbsp;<br>60<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;endl;&nbsp;<br>61<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}<br>62<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br>63<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">return&nbsp;0;<br>64<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}<br>65<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<img src ="http://www.cppblog.com/tianlearn-language/aggbug/114956.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tianlearn-language/" target="_blank">田兵</a> 2010-05-09 22:41 <a href="http://www.cppblog.com/tianlearn-language/archive/2010/05/09/114956.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用C++中的vector实现的栈</title><link>http://www.cppblog.com/tianlearn-language/archive/2010/03/28/110751.html</link><dc:creator>田兵</dc:creator><author>田兵</author><pubDate>Sun, 28 Mar 2010 11:03:00 GMT</pubDate><guid>http://www.cppblog.com/tianlearn-language/archive/2010/03/28/110751.html</guid><wfw:comment>http://www.cppblog.com/tianlearn-language/comments/110751.html</wfw:comment><comments>http://www.cppblog.com/tianlearn-language/archive/2010/03/28/110751.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tianlearn-language/comments/commentRss/110751.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tianlearn-language/services/trackbacks/110751.html</trackback:ping><description><![CDATA[<h3><span style="COLOR: #ff0000">没事的时候随便写的，</span></h3>
<h3><span style="COLOR: #ff0000">模拟一下栈，</span></h3>
<h3 style="COLOR: #ff0000"><span style="COLOR: #ff0000">练习用C++写程序</span></h3>
<h3><br>&nbsp;</h3>
<h3>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-FAMILY: courier new; COLOR: #ff0000; FONT-SIZE: 12pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span>&gt;<br>&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span>&gt;<br>&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span>namespace&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;</span><span>class&nbsp;Stack&nbsp;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img id=Codehighlighter1_73_247_Open_Image onclick="this.style.display='none'; Codehighlighter1_73_247_Open_Text.style.display='none'; Codehighlighter1_73_247_Closed_Image.style.display='inline'; Codehighlighter1_73_247_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_73_247_Closed_Image onclick="this.style.display='none'; Codehighlighter1_73_247_Closed_Text.style.display='none'; Codehighlighter1_73_247_Open_Image.style.display='inline'; Codehighlighter1_73_247_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;</span><span><img src="http://www.cppblog.com/Images/dot.gif"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;</span><span>public:<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;inline&nbsp;</span><span>bool&nbsp;empty();<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span>bool&nbsp;full();<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;push(&nbsp;</span><span>int&nbsp;elem);<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span>int&nbsp;pop();<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span>void&nbsp;getmem();<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img id=Codehighlighter1_190_212_Open_Image onclick="this.style.display='none'; Codehighlighter1_190_212_Open_Text.style.display='none'; Codehighlighter1_190_212_Closed_Image.style.display='inline'; Codehighlighter1_190_212_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_190_212_Closed_Image onclick="this.style.display='none'; Codehighlighter1_190_212_Closed_Text.style.display='none'; Codehighlighter1_190_212_Open_Image.style.display='inline'; Codehighlighter1_190_212_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;size()</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_190_212_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span><span style="COLOR: #000000">{</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;_stack.size();}</span><br>13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;</span><span>private:<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span>&gt;&nbsp;_stack;<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">&nbsp;}</span>;<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;inline&nbsp;</span><span>bool&nbsp;Stack::empty()<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img id=Codehighlighter1_280_316_Open_Image onclick="this.style.display='none'; Codehighlighter1_280_316_Open_Text.style.display='none'; Codehighlighter1_280_316_Closed_Image.style.display='inline'; Codehighlighter1_280_316_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_280_316_Closed_Image onclick="this.style.display='none'; Codehighlighter1_280_316_Closed_Text.style.display='none'; Codehighlighter1_280_316_Open_Image.style.display='inline'; Codehighlighter1_280_316_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;</span><span><img src="http://www.cppblog.com/Images/dot.gif"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span>return&nbsp;_stack.empty();<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span>20<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;inline&nbsp;</span><span>bool&nbsp;Stack::full()<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img style="DISPLAY: inline" id=Codehighlighter1_349_406_Open_Image onclick="this.style.display='none'; Codehighlighter1_349_406_Open_Text.style.display='none'; Codehighlighter1_349_406_Closed_Image.style.display='inline'; Codehighlighter1_349_406_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_349_406_Closed_Image onclick="this.style.display='none'; Codehighlighter1_349_406_Closed_Text.style.display='none'; Codehighlighter1_349_406_Open_Image.style.display='inline'; Codehighlighter1_349_406_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;</span><span><img src="http://www.cppblog.com/Images/dot.gif"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;_stack.size()</span><span>==_stack.max_size();<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">&nbsp;}</span></span><span><br>25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;Stack::&nbsp;push(</span><span>int&nbsp;elem)<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img id=Codehighlighter1_438_512_Open_Image onclick="this.style.display='none'; Codehighlighter1_438_512_Open_Text.style.display='none'; Codehighlighter1_438_512_Closed_Image.style.display='inline'; Codehighlighter1_438_512_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_438_512_Closed_Image onclick="this.style.display='none'; Codehighlighter1_438_512_Closed_Text.style.display='none'; Codehighlighter1_438_512_Open_Image.style.display='inline'; Codehighlighter1_438_512_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;</span><span><img src="http://www.cppblog.com/Images/dot.gif"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span>if(full())<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span>false;<br></span><span>29<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;_stack.push_back(elem);<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span>true;<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span>32<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span>int&nbsp;Stack::&nbsp;pop(&nbsp;)<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img id=Codehighlighter1_543_639_Open_Image onclick="this.style.display='none'; Codehighlighter1_543_639_Open_Text.style.display='none'; Codehighlighter1_543_639_Closed_Image.style.display='inline'; Codehighlighter1_543_639_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_543_639_Closed_Image onclick="this.style.display='none'; Codehighlighter1_543_639_Closed_Text.style.display='none'; Codehighlighter1_543_639_Open_Image.style.display='inline'; Codehighlighter1_543_639_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;</span><span><img src="http://www.cppblog.com/Images/dot.gif"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span>if(empty())<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span>false;<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;elem</span><span>=_stack.back();<br></span><span>38<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;_stack.pop_back();<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span>return&nbsp;elem;<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">&nbsp;}</span></span><span><br>41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;</span><span>void&nbsp;Stack::getmem()<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img id=Codehighlighter1_668_809_Open_Image onclick="this.style.display='none'; Codehighlighter1_668_809_Open_Text.style.display='none'; Codehighlighter1_668_809_Closed_Image.style.display='inline'; Codehighlighter1_668_809_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_668_809_Closed_Image onclick="this.style.display='none'; Codehighlighter1_668_809_Closed_Text.style.display='none'; Codehighlighter1_668_809_Open_Image.style.display='inline'; Codehighlighter1_668_809_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;&nbsp;</span><span><img src="http://www.cppblog.com/Images/dot.gif"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span>if(empty())<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span>return&nbsp;;<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">_stack[</span><span>0];<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;ix</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;ix</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">_stack.size();</span><span>++ix)<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span><span>&lt;&lt;_stack[ix];<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span>&lt;&lt;endl;<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">&nbsp;&nbsp;&nbsp;}</span></span><span><br>50</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span>int&nbsp;main()<br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img id=Codehighlighter1_822_1026_Open_Image onclick="this.style.display='none'; Codehighlighter1_822_1026_Open_Text.style.display='none'; Codehighlighter1_822_1026_Closed_Image.style.display='inline'; Codehighlighter1_822_1026_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_822_1026_Closed_Image onclick="this.style.display='none'; Codehighlighter1_822_1026_Closed_Text.style.display='none'; Codehighlighter1_822_1026_Open_Image.style.display='inline'; Codehighlighter1_822_1026_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span><img src="http://www.cppblog.com/Images/dot.gif"><span style="COLOR: #000000">{<br></span><span>52<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;Stack&nbsp;s;<br></span><span style="COLOR: #008080">53</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s.size()</span><span>&lt;&lt;endl;<br></span><span style="COLOR: #008080">54</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span>1000;)<br></span><span style="COLOR: #008080">55</span><span style="COLOR: #000000"><img id=Codehighlighter1_888_938_Open_Image onclick="this.style.display='none'; Codehighlighter1_888_938_Open_Text.style.display='none'; Codehighlighter1_888_938_Closed_Image.style.display='inline'; Codehighlighter1_888_938_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_888_938_Closed_Image onclick="this.style.display='none'; Codehighlighter1_888_938_Closed_Text.style.display='none'; Codehighlighter1_888_938_Open_Image.style.display='inline'; Codehighlighter1_888_938_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span><img src="http://www.cppblog.com/Images/dot.gif"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">56</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;s.push(i</span><span>+j);<br></span><span style="COLOR: #008080">57</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;k</span><span>=i;<br></span><span style="COLOR: #008080">58</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;i</span><span>=j;<br></span><span style="COLOR: #008080">59</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">k</span><span>+i;<br></span><span style="COLOR: #008080">60</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;}</span></span><span><br>61</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s.size()</span><span>&lt;&lt;endl;<br></span><span>62<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;s.getmem();<br></span><span>63<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;s.pop();<br></span><span>64<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;s.pop();<br></span><span>65<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;s.getmem();<br></span><span>66<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">67</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span>0;<br></span><span style="COLOR: #008080">68</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
</h3>
<img src ="http://www.cppblog.com/tianlearn-language/aggbug/110751.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tianlearn-language/" target="_blank">田兵</a> 2010-03-28 19:03 <a href="http://www.cppblog.com/tianlearn-language/archive/2010/03/28/110751.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>