﻿<?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++博客-希望的海洋-随笔分类-POJ</title><link>http://www.cppblog.com/snowshine09/category/17458.html</link><description>Ain't about how fast I get there,ain't about what's waiting on the other side</description><language>zh-cn</language><lastBuildDate>Mon, 29 Aug 2011 09:33:27 GMT</lastBuildDate><pubDate>Mon, 29 Aug 2011 09:33:27 GMT</pubDate><ttl>60</ttl><item><title>Dp poj 1159(multiple solutions)</title><link>http://www.cppblog.com/snowshine09/archive/2011/08/19/153852.html</link><dc:creator>拥梦的小鱼</dc:creator><author>拥梦的小鱼</author><pubDate>Fri, 19 Aug 2011 07:12:00 GMT</pubDate><guid>http://www.cppblog.com/snowshine09/archive/2011/08/19/153852.html</guid><wfw:comment>http://www.cppblog.com/snowshine09/comments/153852.html</wfw:comment><comments>http://www.cppblog.com/snowshine09/archive/2011/08/19/153852.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowshine09/comments/commentRss/153852.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowshine09/services/trackbacks/153852.html</trackback:ping><description><![CDATA[<div>(1)思路：最长公共子序列：ans=L-LCS(s,^s)<br />(2)DP 状态：dp[i][j]为i to j，需要加入的字符。初始：dp[i][i]=0<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;分2种情况：s[i]==s[j]则dp[i][j]=dp[i+1][j-1]<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;s[i]!=s[j]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=Min(dp[i+1][j],dp[i][j-1])+1 
<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: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i][j]</span><span style="color: #000000">=</span><span style="color: #000000">cal(i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">,j</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;dp[i][j];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img id="Codehighlighter1_51_134_Open_Image" onclick="this.style.display='none'; Codehighlighter1_51_134_Open_Text.style.display='none'; Codehighlighter1_51_134_Closed_Image.style.display='inline'; Codehighlighter1_51_134_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_51_134_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_51_134_Closed_Text.style.display='none'; Codehighlighter1_51_134_Open_Image.style.display='inline'; Codehighlighter1_51_134_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.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_51_134_Closed_Text">/**/</span><span id="Codehighlighter1_51_134_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">dp[i][j]=MIN(cal(i+1,j-1)+2,cal(i+1,j)+1);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;dp[i][j]=MIN(dp[i][j],cal(i,j-1)+1);</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;dp[i][j]</span><span style="color: #000000">=</span><span style="color: #000000">MIN(cal(i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">,j)</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">,cal(i,j</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">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;dp[i][j];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />}<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_211_447_Open_Image" onclick="this.style.display='none'; Codehighlighter1_211_447_Open_Text.style.display='none'; Codehighlighter1_211_447_Closed_Image.style.display='inline'; Codehighlighter1_211_447_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_211_447_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_211_447_Closed_Text.style.display='none'; Codehighlighter1_211_447_Open_Image.style.display='inline'; Codehighlighter1_211_447_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_211_447_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_211_447_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j,k,mid;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">for(i=0;i&lt;N;i++)</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%s</span><span style="color: #000000">"</span><span style="color: #000000">,s);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;len</span><span style="color: #000000">=</span><span style="color: #000000">strlen(s);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">printf("%d\n",len);</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;mid</span><span style="color: #000000">=</span><span style="color: #000000">len</span><span style="color: #000000">/</span><span style="color: #000000">2</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(dp));</span><span style="color: #008000">//</span><span style="color: #008000">unsigned&nbsp;to&nbsp;the&nbsp;utmost</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;dp[mid][mid]</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,cal(</span><span style="color: #000000">0</span><span style="color: #000000">,len</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">));<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div>(3)节省空间，用dp[2][5002]的数组存放。因为i-1用过后，就不再用了<br />滚动数组最直接的想法就是利用原来第i-1行的空间来计算接下来要算的i+1行 
<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: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdlib</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cmath</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;MIN(a,b)&nbsp;(a)&lt;(b)?(a):(b)</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;s[</span><span style="color: #000000">5002</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;dp[</span><span style="color: #000000">2</span><span style="color: #000000">][</span><span style="color: #000000">5002</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_145_387_Open_Image" onclick="this.style.display='none'; Codehighlighter1_145_387_Open_Text.style.display='none'; Codehighlighter1_145_387_Closed_Image.style.display='inline'; Codehighlighter1_145_387_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_145_387_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_145_387_Closed_Text.style.display='none'; Codehighlighter1_145_387_Open_Image.style.display='inline'; Codehighlighter1_145_387_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_145_387_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_145_387_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j,k,l,N;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%s</span><span style="color: #000000">"</span><span style="color: #000000">,s);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">N</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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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">i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;</span><span style="color: #000000">N;</span><span style="color: #000000">++</span><span style="color: #000000">j)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(s[i]</span><span style="color: #000000">==</span><span style="color: #000000">s[j])dp[i</span><span style="color: #000000">&amp;</span><span style="color: #000000">1</span><span style="color: #000000">][j]</span><span style="color: #000000">=</span><span style="color: #000000">dp[(i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">)</span><span style="color: #000000">&amp;</span><span style="color: #000000">1</span><span style="color: #000000">][j</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;dp[i</span><span style="color: #000000">&amp;</span><span style="color: #000000">1</span><span style="color: #000000">][j]</span><span style="color: #000000">=</span><span style="color: #000000">MIN(dp[(i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">)</span><span style="color: #000000">&amp;</span><span style="color: #000000">1</span><span style="color: #000000">][j]</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">,dp[i</span><span style="color: #000000">&amp;</span><span style="color: #000000">1</span><span style="color: #000000">][j</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">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,dp[(i</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">2</span><span style="color: #000000">][N</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div><br />这样就可以用一个array[2][]的数组，在两行之间交替计算 <br /><br />（4）<br />特殊情况：计算array[i][j]所用到的第i-1行的元素全部都在j列的左边 <br />array[i-1][j]和array[i-1][j-1] <br />然后这种情况可以只用一维数组，按照从右到左的顺序计算<br />array[j] = array[j-1]+array[j] <br />重复i次 <br />每次从右到左将一行的元素都按照array[j] = array[j-1]+array[j]计算 <br />假设这个一维数组现在存的是i-1行的元素，计算第i行的元素时，直接将它记<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-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include&nbsp;</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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_50_452_Open_Image" onclick="this.style.display='none'; Codehighlighter1_50_452_Open_Text.style.display='none'; Codehighlighter1_50_452_Closed_Image.style.display='inline'; Codehighlighter1_50_452_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_50_452_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_50_452_Closed_Text.style.display='none'; Codehighlighter1_50_452_Open_Image.style.display='inline'; Codehighlighter1_50_452_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_50_452_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_50_452_Open_Text"><span style="color: #000000">{&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;a[</span><span style="color: #000000">5002</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;f[</span><span style="color: #000000">5002</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j,n,t,tp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%s</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">n,a</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">(f));<br /><img id="Codehighlighter1_174_408_Open_Image" onclick="this.style.display='none'; Codehighlighter1_174_408_Open_Text.style.display='none'; Codehighlighter1_174_408_Closed_Image.style.display='inline'; Codehighlighter1_174_408_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_174_408_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_174_408_Closed_Text.style.display='none'; Codehighlighter1_174_408_Open_Image.style.display='inline'; Codehighlighter1_174_408_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.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">1</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">)</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_174_408_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_174_408_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tp</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">f[i-1,j-1]复位，一开始忘记了，老是wa，郁闷</span><span style="color: #008000"><br /><img id="Codehighlighter1_233_402_Open_Image" onclick="this.style.display='none'; Codehighlighter1_233_402_Open_Text.style.display='none'; Codehighlighter1_233_402_Closed_Image.style.display='inline'; Codehighlighter1_233_402_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_233_402_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_233_402_Closed_Text.style.display='none'; Codehighlighter1_233_402_Open_Image.style.display='inline'; Codehighlighter1_233_402_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif"></span><span style="color: #000000">&nbsp;&nbsp;&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">n;j</span><span style="color: #000000">&gt;=</span><span style="color: #000000">1</span><span style="color: #000000">;j</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_233_402_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_233_402_Open_Text"><span style="color: #000000">{<br /><img id="Codehighlighter1_252_318_Open_Image" onclick="this.style.display='none'; Codehighlighter1_252_318_Open_Text.style.display='none'; Codehighlighter1_252_318_Closed_Image.style.display='inline'; Codehighlighter1_252_318_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_252_318_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_252_318_Closed_Text.style.display='none'; Codehighlighter1_252_318_Open_Image.style.display='inline'; Codehighlighter1_252_318_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;</span><span style="color: #0000ff">if</span><span style="color: #000000">(a[i]</span><span style="color: #000000">==</span><span style="color: #000000">a[j])</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_252_318_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_252_318_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t</span><span style="color: #000000">=</span><span style="color: #000000">f[j];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f[j]</span><span style="color: #000000">=</span><span style="color: #000000">tp</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">f[i,j]=f[i-1,j-1]+1</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tp</span><span style="color: #000000">=</span><span style="color: #000000">t;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img id="Codehighlighter1_327_398_Open_Image" onclick="this.style.display='none'; Codehighlighter1_327_398_Open_Text.style.display='none'; Codehighlighter1_327_398_Closed_Image.style.display='inline'; Codehighlighter1_327_398_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_327_398_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_327_398_Closed_Text.style.display='none'; Codehighlighter1_327_398_Open_Image.style.display='inline'; Codehighlighter1_327_398_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;</span><span style="color: #0000ff">else</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_327_398_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_327_398_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tp</span><span style="color: #000000">=</span><span style="color: #000000">f[j];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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[j]</span><span style="color: #000000">&lt;</span><span style="color: #000000">f[j</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">])&nbsp;f[j]</span><span style="color: #000000">=</span><span style="color: #000000">f[j</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">];&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">f[i-1,j]&lt;f[i,j-1]</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,n</span><span style="color: #000000">-</span><span style="color: #000000">f[</span><span style="color: #000000">1</span><span style="color: #000000">]);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span></div> <br /><br /><br /><br /></div><img src ="http://www.cppblog.com/snowshine09/aggbug/153852.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowshine09/" target="_blank">拥梦的小鱼</a> 2011-08-19 15:12 <a href="http://www.cppblog.com/snowshine09/archive/2011/08/19/153852.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>线性DP 两次最长上升子序列 1836_POJ</title><link>http://www.cppblog.com/snowshine09/archive/2011/08/14/153353.html</link><dc:creator>拥梦的小鱼</dc:creator><author>拥梦的小鱼</author><pubDate>Sun, 14 Aug 2011 07:30:00 GMT</pubDate><guid>http://www.cppblog.com/snowshine09/archive/2011/08/14/153353.html</guid><wfw:comment>http://www.cppblog.com/snowshine09/comments/153353.html</wfw:comment><comments>http://www.cppblog.com/snowshine09/archive/2011/08/14/153353.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowshine09/comments/commentRss/153353.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowshine09/services/trackbacks/153353.html</trackback:ping><description><![CDATA[<p>题意：<br />n个士兵，选ct个人出队，使得重新排队得到的队伍每个人都可以向左或向右看到无穷远，没有比他高或同样高的视觉障碍（人）。求ct最小值。<br /><br />分析：<br /><br />要从左无障碍的看到无限远，则需有左边的人都比他低，同理，右边也一样。故找到站到中间的一个人（or two），他的两边的最长子序列之和最大。即留下的士兵最多，即出队的人最少。</p>
<h2><font color="blue">使剩下的队列满足a1 &lt; a2 &lt; a3 ... &lt; a(i ) &lt;=&gt; a(i+1) &gt; a(i+2) &gt; .. a(n-1) &gt; a(n)</font></h2>
<p>&nbsp;</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; background-color: #eeeeee"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdlib</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />#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 /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cmath</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;d[</span><span style="color: #000000">1010</span><span style="color: #000000">],rev[</span><span style="color: #000000">1010</span><span style="color: #000000">],index[</span><span style="color: #000000">10</span><span style="color: #000000">];<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;hi[</span><span style="color: #000000">1010</span><span style="color: #000000">];<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_132_1077_Open_Image" onclick="this.style.display='none'; Codehighlighter1_132_1077_Open_Text.style.display='none'; Codehighlighter1_132_1077_Closed_Image.style.display='inline'; Codehighlighter1_132_1077_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_132_1077_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_132_1077_Closed_Text.style.display='none'; Codehighlighter1_132_1077_Open_Image.style.display='inline'; Codehighlighter1_132_1077_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_132_1077_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_132_1077_Open_Text"><span style="color: #000000">{<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,tmp</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">,i,j,max</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">,ct</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;db;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&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 /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&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">n;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /><img id="Codehighlighter1_211_277_Open_Image" onclick="this.style.display='none'; Codehighlighter1_211_277_Open_Text.style.display='none'; Codehighlighter1_211_277_Closed_Image.style.display='inline'; Codehighlighter1_211_277_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_211_277_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_211_277_Closed_Text.style.display='none'; Codehighlighter1_211_277_Open_Image.style.display='inline'; Codehighlighter1_211_277_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_211_277_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_211_277_Open_Text"><span style="color: #000000">{&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%lf</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">hi[i]);</span><span style="color: #008000">//</span><span style="color: #008000">双精度浮点数输入须为%lf，而非%f</span><span style="color: #008000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d[i]</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rev[i]</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">scanf("%f",&amp;db);<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">d[0]=1;</span><span style="color: #008000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /></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">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">n;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /><img id="Codehighlighter1_329_418_Open_Image" onclick="this.style.display='none'; Codehighlighter1_329_418_Open_Text.style.display='none'; Codehighlighter1_329_418_Closed_Image.style.display='inline'; Codehighlighter1_329_418_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_329_418_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_329_418_Closed_Text.style.display='none'; Codehighlighter1_329_418_Open_Image.style.display='inline'; Codehighlighter1_329_418_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_329_418_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_329_418_Open_Text"><span style="color: #000000">{<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&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">0</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;</span><span style="color: #000000">i;</span><span style="color: #000000">++</span><span style="color: #000000">j)<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(hi[j]</span><span style="color: #000000">&lt;</span><span style="color: #000000">hi[i]</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">d[j]</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">&gt;</span><span style="color: #000000">d[i])<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d[i]</span><span style="color: #000000">=</span><span style="color: #000000">d[j]</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">正序以i结尾的最长上升子序列长度</span><span style="color: #008000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&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">n</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">;</span><span style="color: #000000">--</span><span style="color: #000000">i)<br /><img id="Codehighlighter1_442_541_Open_Image" onclick="this.style.display='none'; Codehighlighter1_442_541_Open_Text.style.display='none'; Codehighlighter1_442_541_Closed_Image.style.display='inline'; Codehighlighter1_442_541_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_442_541_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_442_541_Closed_Text.style.display='none'; Codehighlighter1_442_541_Open_Image.style.display='inline'; Codehighlighter1_442_541_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_442_541_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_442_541_Open_Text"><span style="color: #000000">{<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&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">n</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;j</span><span style="color: #000000">&gt;</span><span style="color: #000000">i;</span><span style="color: #000000">--</span><span style="color: #000000">j)<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(hi[j]</span><span style="color: #000000">&lt;</span><span style="color: #000000">hi[i]</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">rev[i]</span><span style="color: #000000">&lt;</span><span style="color: #000000">rev[j]</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">)<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rev[i]</span><span style="color: #000000">=</span><span style="color: #000000">rev[j]</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">逆序以i结尾的最长上升子序列长度</span><span style="color: #008000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&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">n;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /><img id="Codehighlighter1_562_825_Open_Image" onclick="this.style.display='none'; Codehighlighter1_562_825_Open_Text.style.display='none'; Codehighlighter1_562_825_Closed_Image.style.display='inline'; Codehighlighter1_562_825_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_562_825_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_562_825_Closed_Text.style.display='none'; Codehighlighter1_562_825_Open_Image.style.display='inline'; Codehighlighter1_562_825_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_562_825_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_562_825_Open_Text"><span style="color: #000000">{<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(d[i]</span><span style="color: #000000">+</span><span style="color: #000000">rev[i]</span><span style="color: #000000">&gt;</span><span style="color: #000000">max)</span><span style="color: #008000">//</span><span style="color: #008000">find&nbsp;the&nbsp;highest&nbsp;one&nbsp;int&nbsp;the&nbsp;middle,keep&nbsp;its&nbsp;index&nbsp;in&nbsp;record</span><span style="color: #008000"><br /><img id="Codehighlighter1_650_699_Open_Image" onclick="this.style.display='none'; Codehighlighter1_650_699_Open_Text.style.display='none'; Codehighlighter1_650_699_Closed_Image.style.display='inline'; Codehighlighter1_650_699_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_650_699_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_650_699_Closed_Text.style.display='none'; Codehighlighter1_650_699_Open_Image.style.display='inline'; Codehighlighter1_650_699_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_650_699_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_650_699_Open_Text"><span style="color: #000000">{<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max</span><span style="color: #000000">=</span><span style="color: #000000">d[i]</span><span style="color: #000000">+</span><span style="color: #000000">rev[i];<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index[</span><span style="color: #000000">0</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">i;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&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">(d[i]</span><span style="color: #000000">+</span><span style="color: #000000">rev[i]</span><span style="color: #000000">==</span><span style="color: #000000">max</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">hi[i]</span><span style="color: #000000">==</span><span style="color: #000000">hi[index[</span><span style="color: #000000">0</span><span style="color: #000000">]])</span><span style="color: #008000">//</span><span style="color: #008000">&amp;&amp;hi[i]==hi[index[0]]漏掉则出错。此条件判断是否有两个相等的最高顶点。</span><span style="color: #008000"><br /><img id="Codehighlighter1_799_822_Open_Image" onclick="this.style.display='none'; Codehighlighter1_799_822_Open_Text.style.display='none'; Codehighlighter1_799_822_Closed_Image.style.display='inline'; Codehighlighter1_799_822_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_799_822_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_799_822_Closed_Text.style.display='none'; Codehighlighter1_799_822_Open_Image.style.display='inline'; Codehighlighter1_799_822_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_799_822_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_799_822_Open_Text"><span style="color: #000000">{<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index[</span><span style="color: #000000">++</span><span style="color: #000000">tmp]</span><span style="color: #000000">=</span><span style="color: #000000">i;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;ct</span><span style="color: #000000">=</span><span style="color: #000000">index[</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">-</span><span style="color: #000000">d[index[</span><span style="color: #000000">0</span><span style="color: #000000">]]</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">-</span><span style="color: #000000">index[tmp]</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">-</span><span style="color: #000000">rev[index[tmp]];</span><span style="color: #008000">//</span><span style="color: #008000">b4&nbsp;and&nbsp;after&nbsp;of&nbsp;3&nbsp;parts:before&nbsp;index[0];between&nbsp;o~tmp;after&nbsp;index[tmp]</span><span style="color: #008000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /></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">index[</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">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">index[tmp];</span><span style="color: #000000">++</span><span style="color: #000000">i)</span><span style="color: #008000">//</span><span style="color: #008000">intermediate&nbsp;part</span><span style="color: #008000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(hi[i]</span><span style="color: #000000">&lt;</span><span style="color: #000000">hi[index[</span><span style="color: #000000">0</span><span style="color: #000000">]])ct</span><span style="color: #000000">++</span><span style="color: #000000">;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,ct);<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&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 /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span></div>
<p><br /><br />&nbsp;</p><img src ="http://www.cppblog.com/snowshine09/aggbug/153353.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowshine09/" target="_blank">拥梦的小鱼</a> 2011-08-14 15:30 <a href="http://www.cppblog.com/snowshine09/archive/2011/08/14/153353.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>线性DP——POJ 3276</title><link>http://www.cppblog.com/snowshine09/archive/2011/08/13/153303.html</link><dc:creator>拥梦的小鱼</dc:creator><author>拥梦的小鱼</author><pubDate>Sat, 13 Aug 2011 14:13:00 GMT</pubDate><guid>http://www.cppblog.com/snowshine09/archive/2011/08/13/153303.html</guid><wfw:comment>http://www.cppblog.com/snowshine09/comments/153303.html</wfw:comment><comments>http://www.cppblog.com/snowshine09/archive/2011/08/13/153303.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowshine09/comments/commentRss/153303.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowshine09/services/trackbacks/153303.html</trackback:ping><description><![CDATA[<p><span style="color: #ff0000" color="#ff0000">问题</span></p>
<p>给定你一个字符串，和n个单词构成的字典。让你求出在字符串中最小删去几个字母，使得剩下的字符串能够由字典中的若干个单词构成。输出最少删去的字母的个数。</p><br /><span>分析<br />考虑给定字符串中第i个字符，可能删去，可能保留。&#8212;&#8212;定义划分状态的关键。<br />d[i]=Min{d[i-1]+1,make(i)}//delete vs not delete <br /><br />如果不删去，则它一定是作为某个单词的末尾。故与该位置前的状态有关系。make(i)中，要比较，所有字典中以msg[i]结尾的单词：若要保留，则此过程中删去字母个数ct,return 就是Min{d[该单词首字母的前一个字符位置]+ct} 
<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"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" />#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 /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cmath</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdlib</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;INF&nbsp;1000000</span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;MIN(a,b)&nbsp;(a)&gt;(b)?(b):(a)</span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;N,L;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;dic[</span><span style="color: #000000">600</span><span style="color: #000000">][</span><span style="color: #000000">26</span><span style="color: #000000">],msg[</span><span style="color: #000000">305</span><span style="color: #000000">];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;f[</span><span style="color: #000000">305</span><span style="color: #000000">];</span><span style="color: #008000">//</span><span style="color: #008000">记录第i位前丢弃的最少字母数</span><span style="color: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;make(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;m)<br /><img id="Codehighlighter1_204_914_Open_Image" onclick="this.style.display='none'; Codehighlighter1_204_914_Open_Text.style.display='none'; Codehighlighter1_204_914_Closed_Image.style.display='inline'; Codehighlighter1_204_914_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_204_914_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_204_914_Closed_Text.style.display='none'; Codehighlighter1_204_914_Open_Image.style.display='inline'; Codehighlighter1_204_914_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_204_914_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_204_914_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;k,j,ct</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">,n</span><span style="color: #000000">=</span><span style="color: #000000">m,t,re</span><span style="color: #000000">=</span><span style="color: #000000">INF;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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">0</span><span style="color: #000000">;k</span><span style="color: #000000">&lt;</span><span style="color: #000000">N;</span><span style="color: #000000">++</span><span style="color: #000000">k)<br /><img id="Codehighlighter1_253_795_Open_Image" onclick="this.style.display='none'; Codehighlighter1_253_795_Open_Text.style.display='none'; Codehighlighter1_253_795_Closed_Image.style.display='inline'; Codehighlighter1_253_795_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_253_795_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_253_795_Closed_Text.style.display='none'; Codehighlighter1_253_795_Open_Image.style.display='inline'; Codehighlighter1_253_795_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_253_795_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_253_795_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(dic[k][strlen(dic[k])</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">msg[m])</span><span style="color: #008000">//</span><span style="color: #008000">若写m为n此处n=0,WA</span><span style="color: #008000"><br /><img id="Codehighlighter1_311_792_Open_Image" onclick="this.style.display='none'; Codehighlighter1_311_792_Open_Text.style.display='none'; Codehighlighter1_311_792_Closed_Image.style.display='inline'; Codehighlighter1_311_792_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_311_792_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_311_792_Closed_Text.style.display='none'; Codehighlighter1_311_792_Open_Image.style.display='inline'; Codehighlighter1_311_792_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_311_792_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_311_792_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n</span><span style="color: #000000">=</span><span style="color: #000000">m;ct</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j</span><span style="color: #000000">=</span><span style="color: #000000">strlen(dic[k])</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(j</span><span style="color: #000000">&gt;=</span><span style="color: #000000">0</span><span style="color: #000000">)<br /><img id="Codehighlighter1_371_532_Open_Image" onclick="this.style.display='none'; Codehighlighter1_371_532_Open_Text.style.display='none'; Codehighlighter1_371_532_Closed_Image.style.display='inline'; Codehighlighter1_371_532_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_371_532_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_371_532_Closed_Text.style.display='none'; Codehighlighter1_371_532_Open_Image.style.display='inline'; Codehighlighter1_371_532_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_371_532_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_371_532_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(n</span><span style="color: #000000">&gt;=</span><span style="color: #000000">0</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">msg[n]</span><span style="color: #000000">!=</span><span style="color: #000000">dic[k][j])<br /><img id="Codehighlighter1_412_439_Open_Image" onclick="this.style.display='none'; Codehighlighter1_412_439_Open_Text.style.display='none'; Codehighlighter1_412_439_Closed_Image.style.display='inline'; Codehighlighter1_412_439_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_412_439_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_412_439_Closed_Text.style.display='none'; Codehighlighter1_412_439_Open_Image.style.display='inline'; Codehighlighter1_412_439_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_412_439_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_412_439_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n</span><span style="color: #000000">--</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ct</span><span style="color: #000000">++</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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">(n</span><span style="color: #000000">&gt;=</span><span style="color: #000000">0</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">msg[n]</span><span style="color: #000000">==</span><span style="color: #000000">dic[k][j])n</span><span style="color: #000000">--</span><span style="color: #000000">;</span><span style="color: #008000">//</span><span style="color: #008000">当n小于0时说明此单词比该msgd第n位前的字串长</span><span style="color: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" /></span><span style="color: #000000">&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">break</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j</span><span style="color: #000000">--</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">if(j==-1&amp;&amp;ct+f[n-1]&lt;re)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;re=ct+f[n-1];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">t=n;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">}</span><span style="color: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(j</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">说明字典中存在以msg【n】结尾的子串=该单词</span><span style="color: #008000"><br /><img id="Codehighlighter1_651_788_Open_Image" onclick="this.style.display='none'; Codehighlighter1_651_788_Open_Text.style.display='none'; Codehighlighter1_651_788_Closed_Image.style.display='inline'; Codehighlighter1_651_788_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_651_788_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_651_788_Closed_Text.style.display='none'; Codehighlighter1_651_788_Open_Image.style.display='inline'; Codehighlighter1_651_788_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_651_788_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_651_788_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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">(n</span><span style="color: #000000">&gt;=</span><span style="color: #000000">0</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">ct</span><span style="color: #000000">+</span><span style="color: #000000">f[n]</span><span style="color: #000000">&lt;</span><span style="color: #000000">re)</span><span style="color: #008000">//</span><span style="color: #008000">由于上一个if语句多减了一次1，所以此时perhaps&nbsp;n&lt;0</span><span style="color: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" /></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;re</span><span style="color: #000000">=</span><span style="color: #000000">ct</span><span style="color: #000000">+</span><span style="color: #000000">f[n];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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">(n</span><span style="color: #000000">&lt;</span><span style="color: #000000">0</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">ct</span><span style="color: #000000">&lt;</span><span style="color: #000000">re)re</span><span style="color: #000000">=</span><span style="color: #000000">ct;</span><span style="color: #008000">//</span><span style="color: #008000">此时说明msg中的该单词恰好是从msg[0]开始的</span><span style="color: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img id="Codehighlighter1_798_897_Open_Image" onclick="this.style.display='none'; Codehighlighter1_798_897_Open_Text.style.display='none'; Codehighlighter1_798_897_Closed_Image.style.display='inline'; Codehighlighter1_798_897_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_798_897_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_798_897_Closed_Text.style.display='none'; Codehighlighter1_798_897_Open_Image.style.display='inline'; Codehighlighter1_798_897_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_798_897_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff">/**/</span><span id="Codehighlighter1_798_897_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">if(re==INF)头开始误导了，想着总是加一，不对。其实是对的，会自己判断选择到达该状态的其他路径，<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;f[m-1]==0?0:f[m-1]+1;&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><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;re;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top"  alt="" />}</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_927_1429_Open_Image" onclick="this.style.display='none'; Codehighlighter1_927_1429_Open_Text.style.display='none'; Codehighlighter1_927_1429_Closed_Image.style.display='inline'; Codehighlighter1_927_1429_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_927_1429_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_927_1429_Closed_Text.style.display='none'; Codehighlighter1_927_1429_Open_Image.style.display='inline'; Codehighlighter1_927_1429_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_927_1429_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_927_1429_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;len,i,j,k;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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 /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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">len);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%s</span><span style="color: #000000">"</span><span style="color: #000000">,msg);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">for(i=0;i&lt;len;i++)调试时的代码一定要记得注释掉！！！WA两次因此。太suck了<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;printf("%c&nbsp;",msg[i]);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">printf("\n");<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">for(i=0;i&lt;len;i++)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;printf("%-2d",i);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">printf("\n");</span><span style="color: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" /></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">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">N;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /><img id="Codehighlighter1_1176_1201_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1176_1201_Open_Text.style.display='none'; Codehighlighter1_1176_1201_Closed_Image.style.display='inline'; Codehighlighter1_1176_1201_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_1176_1201_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_1176_1201_Closed_Text.style.display='none'; Codehighlighter1_1176_1201_Open_Image.style.display='inline'; Codehighlighter1_1176_1201_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_1176_1201_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1176_1201_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%s</span><span style="color: #000000">"</span><span style="color: #000000">,dic[i]);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;len</span><span style="color: #000000">=</span><span style="color: #000000">strlen(msg);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;f[</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">;</span><span style="color: #008000">//</span><span style="color: #008000">初始状态可能不为1，即第一个字符也可能作为字典单词的最后一个字母，此时该单词位单字母单词</span><span style="color: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" /></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">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">N;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(strlen(dic[i])</span><span style="color: #000000">==</span><span style="color: #000000">1</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">dic[i][</span><span style="color: #000000">0</span><span style="color: #000000">]</span><span style="color: #000000">==</span><span style="color: #000000">msg[</span><span style="color: #000000">0</span><span style="color: #000000">])f[</span><span style="color: #000000">0</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">len;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img id="Codehighlighter1_1365_1398_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1365_1398_Open_Text.style.display='none'; Codehighlighter1_1365_1398_Closed_Image.style.display='inline'; Codehighlighter1_1365_1398_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_1365_1398_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_1365_1398_Closed_Text.style.display='none'; Codehighlighter1_1365_1398_Open_Image.style.display='inline'; Codehighlighter1_1365_1398_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_1365_1398_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1365_1398_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f[i]</span><span style="color: #000000">=</span><span style="color: #000000">MIN(f[i</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">,make(i));<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,f[len</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top"  alt="" />}</span></span></div><br /><br /></span><img src ="http://www.cppblog.com/snowshine09/aggbug/153303.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowshine09/" target="_blank">拥梦的小鱼</a> 2011-08-13 22:13 <a href="http://www.cppblog.com/snowshine09/archive/2011/08/13/153303.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>简易DP——最长上升子序列 poj 2533</title><link>http://www.cppblog.com/snowshine09/archive/2011/08/04/152459.html</link><dc:creator>拥梦的小鱼</dc:creator><author>拥梦的小鱼</author><pubDate>Thu, 04 Aug 2011 11:35:00 GMT</pubDate><guid>http://www.cppblog.com/snowshine09/archive/2011/08/04/152459.html</guid><wfw:comment>http://www.cppblog.com/snowshine09/comments/152459.html</wfw:comment><comments>http://www.cppblog.com/snowshine09/archive/2011/08/04/152459.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowshine09/comments/commentRss/152459.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowshine09/services/trackbacks/152459.html</trackback:ping><description><![CDATA[<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"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /><span style="color: #008000">//</span><span style="color: #008000">状态为数组第位置k，作为每个子序列的末位，状态值为最长上升子序列长度，初始L[0]=1</span><span style="color: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdlib</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cmath</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" />#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 /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;k,n,ct,arr[</span><span style="color: #000000">1010</span><span style="color: #000000">],L[</span><span style="color: #000000">1010</span><span style="color: #000000">];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_158_470_Open_Image" onclick="this.style.display='none'; Codehighlighter1_158_470_Open_Text.style.display='none'; Codehighlighter1_158_470_Closed_Image.style.display='inline'; Codehighlighter1_158_470_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_158_470_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_158_470_Closed_Text.style.display='none'; Codehighlighter1_158_470_Open_Image.style.display='inline'; Codehighlighter1_158_470_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_158_470_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_158_470_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j,max;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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 /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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">n;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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">arr[i]);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;L[</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 /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">n;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /><img id="Codehighlighter1_260_345_Open_Image" onclick="this.style.display='none'; Codehighlighter1_260_345_Open_Text.style.display='none'; Codehighlighter1_260_345_Closed_Image.style.display='inline'; Codehighlighter1_260_345_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_260_345_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_260_345_Closed_Text.style.display='none'; Codehighlighter1_260_345_Open_Image.style.display='inline'; Codehighlighter1_260_345_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_260_345_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_260_345_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&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">0</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;</span><span style="color: #000000">i;</span><span style="color: #000000">++</span><span style="color: #000000">j)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(arr[j]</span><span style="color: #000000">&lt;</span><span style="color: #000000">arr[i]</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">L[j]</span><span style="color: #000000">&gt;</span><span style="color: #000000">max)max</span><span style="color: #000000">=</span><span style="color: #000000">L[j];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L[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">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;max</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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">n;</span><span style="color: #000000">++</span><span style="color: #000000">i)</span><span style="color: #008000">//</span><span style="color: #008000">此循环不可少，因为L[n-1]为包含最后一个数字的最大串长度，题目未加此条件</span><span style="color: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" /></span><span style="color: #000000">&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">L[i])max</span><span style="color: #000000">=</span><span style="color: #000000">L[i];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,max);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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 /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top"  alt="" />}</span></span></div><img src ="http://www.cppblog.com/snowshine09/aggbug/152459.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowshine09/" target="_blank">拥梦的小鱼</a> 2011-08-04 19:35 <a href="http://www.cppblog.com/snowshine09/archive/2011/08/04/152459.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>递归深搜——poj 1321,类八皇后</title><link>http://www.cppblog.com/snowshine09/archive/2011/08/04/152457.html</link><dc:creator>拥梦的小鱼</dc:creator><author>拥梦的小鱼</author><pubDate>Thu, 04 Aug 2011 11:08:00 GMT</pubDate><guid>http://www.cppblog.com/snowshine09/archive/2011/08/04/152457.html</guid><wfw:comment>http://www.cppblog.com/snowshine09/comments/152457.html</wfw:comment><comments>http://www.cppblog.com/snowshine09/archive/2011/08/04/152457.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowshine09/comments/commentRss/152457.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowshine09/services/trackbacks/152457.html</trackback:ping><description><![CDATA[<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"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdlib</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cmath</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" />#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 /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;k,n,ct,num_p,col[</span><span style="color: #000000">10</span><span style="color: #000000">],ans[</span><span style="color: #000000">92</span><span style="color: #000000">][</span><span style="color: #000000">10</span><span style="color: #000000">],row[</span><span style="color: #000000">10</span><span style="color: #000000">];</span><span style="color: #008000">//</span><span style="color: #008000">col记录第i行放棋子所在的列号</span><span style="color: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;bd[</span><span style="color: #000000">10</span><span style="color: #000000">][</span><span style="color: #000000">10</span><span style="color: #000000">];</span><span style="color: #008000">//</span><span style="color: #008000">棋盘布局</span><span style="color: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;queen(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i)<br /><img id="Codehighlighter1_176_605_Open_Image" onclick="this.style.display='none'; Codehighlighter1_176_605_Open_Text.style.display='none'; Codehighlighter1_176_605_Closed_Image.style.display='inline'; Codehighlighter1_176_605_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_176_605_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_176_605_Closed_Text.style.display='none'; Codehighlighter1_176_605_Open_Image.style.display='inline'; Codehighlighter1_176_605_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_176_605_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_176_605_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j,t;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(num_p</span><span style="color: #000000">==</span><span style="color: #000000">k)</span><span style="color: #008000">//</span><span style="color: #008000">不同于八皇后，k不一定为8，有题目给出</span><span style="color: #008000"><br /><img id="Codehighlighter1_225_308_Open_Image" onclick="this.style.display='none'; Codehighlighter1_225_308_Open_Text.style.display='none'; Codehighlighter1_225_308_Closed_Image.style.display='inline'; Codehighlighter1_225_308_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_225_308_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_225_308_Closed_Text.style.display='none'; Codehighlighter1_225_308_Open_Image.style.display='inline'; Codehighlighter1_225_308_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_225_308_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_225_308_Open_Text"><span style="color: #000000">{<br /><img id="Codehighlighter1_229_286_Open_Image" onclick="this.style.display='none'; Codehighlighter1_229_286_Open_Text.style.display='none'; Codehighlighter1_229_286_Closed_Image.style.display='inline'; Codehighlighter1_229_286_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_229_286_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_229_286_Closed_Text.style.display='none'; Codehighlighter1_229_286_Open_Image.style.display='inline'; Codehighlighter1_229_286_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_229_286_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff">/**/</span><span id="Codehighlighter1_229_286_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">for(j=0;j&lt;n;++j)记录可能方案情况，此题不要求<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans[ct][j]=col[j]+1;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ct</span><span style="color: #000000">++</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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">n)</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;;</span><span style="color: #008000">//</span><span style="color: #008000">少了此条件则n无限大</span><span style="color: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" /></span><span style="color: #000000">&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">0</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;</span><span style="color: #000000">n;</span><span style="color: #000000">++</span><span style="color: #000000">j)<br /><img id="Codehighlighter1_360_521_Open_Image" onclick="this.style.display='none'; Codehighlighter1_360_521_Open_Text.style.display='none'; Codehighlighter1_360_521_Closed_Image.style.display='inline'; Codehighlighter1_360_521_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_360_521_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_360_521_Closed_Text.style.display='none'; Codehighlighter1_360_521_Open_Image.style.display='inline'; Codehighlighter1_360_521_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_360_521_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_360_521_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(bd[i][j]</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">continue</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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">;t</span><span style="color: #000000">&lt;</span><span style="color: #000000">i;</span><span style="color: #000000">++</span><span style="color: #000000">t)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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">(col[t]</span><span style="color: #000000">==</span><span style="color: #000000">j)</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(t</span><span style="color: #000000">==</span><span style="color: #000000">i)<br /><img id="Codehighlighter1_449_518_Open_Image" onclick="this.style.display='none'; Codehighlighter1_449_518_Open_Text.style.display='none'; Codehighlighter1_449_518_Closed_Image.style.display='inline'; Codehighlighter1_449_518_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_449_518_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_449_518_Closed_Text.style.display='none'; Codehighlighter1_449_518_Open_Image.style.display='inline'; Codehighlighter1_449_518_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_449_518_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_449_518_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;col[i]</span><span style="color: #000000">=</span><span style="color: #000000">j;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num_p</span><span style="color: #000000">++</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;queen(i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num_p</span><span style="color: #000000">--</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;col[i]</span><span style="color: #000000">=</span><span style="color: #000000">9</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(k</span><span style="color: #000000">&lt;</span><span style="color: #000000">n)</span><span style="color: #008000">//</span><span style="color: #008000">不同于八皇后，第i行可以不放棋子，故深搜多一条支路</span><span style="color: #008000"><br /><img id="Codehighlighter1_560_603_Open_Image" onclick="this.style.display='none'; Codehighlighter1_560_603_Open_Text.style.display='none'; Codehighlighter1_560_603_Closed_Image.style.display='inline'; Codehighlighter1_560_603_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_560_603_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_560_603_Closed_Text.style.display='none'; Codehighlighter1_560_603_Open_Image.style.display='inline'; Codehighlighter1_560_603_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_560_603_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_560_603_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">num_p++;</span><span style="color: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;queen(i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">num_p--;</span><span style="color: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top"  alt="" />}</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_618_842_Open_Image" onclick="this.style.display='none'; Codehighlighter1_618_842_Open_Text.style.display='none'; Codehighlighter1_618_842_Closed_Image.style.display='inline'; Codehighlighter1_618_842_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_618_842_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_618_842_Closed_Text.style.display='none'; Codehighlighter1_618_842_Open_Image.style.display='inline'; Codehighlighter1_618_842_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_618_842_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_618_842_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">n,</span><span style="color: #000000">&amp;</span><span style="color: #000000">k)</span><span style="color: #000000">==</span><span style="color: #000000">2</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">n</span><span style="color: #000000">!=-</span><span style="color: #000000">1</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">k</span><span style="color: #000000">!=-</span><span style="color: #000000">1</span><span style="color: #000000">)<br /><img id="Codehighlighter1_676_829_Open_Image" onclick="this.style.display='none'; Codehighlighter1_676_829_Open_Text.style.display='none'; Codehighlighter1_676_829_Closed_Image.style.display='inline'; Codehighlighter1_676_829_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_676_829_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_676_829_Closed_Text.style.display='none'; Codehighlighter1_676_829_Open_Image.style.display='inline'; Codehighlighter1_676_829_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_676_829_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_676_829_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">n;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /><img id="Codehighlighter1_699_725_Open_Image" onclick="this.style.display='none'; Codehighlighter1_699_725_Open_Text.style.display='none'; Codehighlighter1_699_725_Closed_Image.style.display='inline'; Codehighlighter1_699_725_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_699_725_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_699_725_Closed_Text.style.display='none'; Codehighlighter1_699_725_Open_Image.style.display='inline'; Codehighlighter1_699_725_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_699_725_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_699_725_Open_Text"><span style="color: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%s</span><span style="color: #000000">"</span><span style="color: #000000">,bd[i]);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;queen(</span><span style="color: #000000">0</span><span style="color: #000000">);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,ct);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ct</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(col,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(col));<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(row,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(row));<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top"  alt="" />&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 /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top"  alt="" />}</span></span></div><img src ="http://www.cppblog.com/snowshine09/aggbug/152457.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowshine09/" target="_blank">拥梦的小鱼</a> 2011-08-04 19:08 <a href="http://www.cppblog.com/snowshine09/archive/2011/08/04/152457.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>POJ题目分类</title><link>http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.html</link><dc:creator>拥梦的小鱼</dc:creator><author>拥梦的小鱼</author><pubDate>Tue, 02 Aug 2011 03:03:00 GMT</pubDate><guid>http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.html</guid><wfw:comment>http://www.cppblog.com/snowshine09/comments/152272.html</wfw:comment><comments>http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowshine09/comments/commentRss/152272.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowshine09/services/trackbacks/152272.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp; 多版本的POJ分类流传最广的一种分类：初期:一.基本算法:&nbsp; &nbsp;&nbsp;&nbsp;(1)枚举. (poj1753,poj2965)&nbsp; &nbsp;&nbsp;&nbsp;(2)贪心(poj1328,poj2109,poj2586)&nbsp; &nbsp;&nbsp;&nbsp;(3)递归和分治法.&nbsp; &n...&nbsp;&nbsp;<a href='http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.html'>阅读全文</a><img src ="http://www.cppblog.com/snowshine09/aggbug/152272.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowshine09/" target="_blank">拥梦的小鱼</a> 2011-08-02 11:03 <a href="http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>背包系列 POJ——1276 多重背包</title><link>http://www.cppblog.com/snowshine09/archive/2011/08/02/152271.html</link><dc:creator>拥梦的小鱼</dc:creator><author>拥梦的小鱼</author><pubDate>Tue, 02 Aug 2011 03:00:00 GMT</pubDate><guid>http://www.cppblog.com/snowshine09/archive/2011/08/02/152271.html</guid><wfw:comment>http://www.cppblog.com/snowshine09/comments/152271.html</wfw:comment><comments>http://www.cppblog.com/snowshine09/archive/2011/08/02/152271.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowshine09/comments/commentRss/152271.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowshine09/services/trackbacks/152271.html</trackback:ping><description><![CDATA[<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: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdlib</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cmath</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;inline&nbsp;MAX(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;a,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;b)<br /><img id="Codehighlighter1_97_134_Open_Image" onclick="this.style.display='none'; Codehighlighter1_97_134_Open_Text.style.display='none'; Codehighlighter1_97_134_Closed_Image.style.display='inline'; Codehighlighter1_97_134_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_97_134_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_97_134_Closed_Text.style.display='none'; Codehighlighter1_97_134_Open_Image.style.display='inline'; Codehighlighter1_97_134_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_97_134_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_97_134_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(a</span><span style="color: #000000">&gt;</span><span style="color: #000000">b)&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;a;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;b;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;N[</span><span style="color: #000000">15</span><span style="color: #000000">],D[</span><span style="color: #000000">15</span><span style="color: #000000">],dp[</span><span style="color: #000000">100010</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_176_988_Open_Image" onclick="this.style.display='none'; Codehighlighter1_176_988_Open_Text.style.display='none'; Codehighlighter1_176_988_Closed_Image.style.display='inline'; Codehighlighter1_176_988_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_176_988_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_176_988_Closed_Text.style.display='none'; Codehighlighter1_176_988_Open_Image.style.display='inline'; Codehighlighter1_176_988_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_176_988_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_176_988_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,i,j,k,amt,cash;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">cash,</span><span style="color: #000000">&amp;</span><span style="color: #000000">n)</span><span style="color: #000000">==</span><span style="color: #000000">2</span><span style="color: #000000">)<br /><img id="Codehighlighter1_236_975_Open_Image" onclick="this.style.display='none'; Codehighlighter1_236_975_Open_Text.style.display='none'; Codehighlighter1_236_975_Closed_Image.style.display='inline'; Codehighlighter1_236_975_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_236_975_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_236_975_Closed_Text.style.display='none'; Codehighlighter1_236_975_Open_Image.style.display='inline'; Codehighlighter1_236_975_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_236_975_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_236_975_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(dp));<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">n;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /><img id="Codehighlighter1_286_338_Open_Image" onclick="this.style.display='none'; Codehighlighter1_286_338_Open_Text.style.display='none'; Codehighlighter1_286_338_Closed_Image.style.display='inline'; Codehighlighter1_286_338_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_286_338_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_286_338_Closed_Text.style.display='none'; Codehighlighter1_286_338_Open_Image.style.display='inline'; Codehighlighter1_286_338_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&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_286_338_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_286_338_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">N[i],</span><span style="color: #000000">&amp;</span><span style="color: #000000">D[i]);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[D[i]]</span><span style="color: #000000">=</span><span style="color: #000000">D[i];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">&nbsp;;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;n&nbsp;;</span><span style="color: #000000">++</span><span style="color: #000000">&nbsp;i)<br /><img id="Codehighlighter1_368_944_Open_Image" onclick="this.style.display='none'; Codehighlighter1_368_944_Open_Text.style.display='none'; Codehighlighter1_368_944_Closed_Image.style.display='inline'; Codehighlighter1_368_944_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_368_944_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_368_944_Closed_Text.style.display='none'; Codehighlighter1_368_944_Open_Image.style.display='inline'; Codehighlighter1_368_944_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&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_368_944_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_368_944_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(D[&nbsp;i&nbsp;]</span><span style="color: #000000">*</span><span style="color: #000000">N[&nbsp;i&nbsp;]</span><span style="color: #000000">&gt;=</span><span style="color: #000000">cash)</span><span style="color: #008000">//</span><span style="color: #008000">该物品最多超过了总容量，完全背包</span><span style="color: #008000"><br /><img id="Codehighlighter1_418_541_Open_Image" onclick="this.style.display='none'; Codehighlighter1_418_541_Open_Text.style.display='none'; Codehighlighter1_418_541_Closed_Image.style.display='inline'; Codehighlighter1_418_541_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_418_541_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_418_541_Closed_Text.style.display='none'; Codehighlighter1_418_541_Open_Image.style.display='inline'; Codehighlighter1_418_541_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif"></span><span style="color: #000000">&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_418_541_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_418_541_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">(&nbsp;j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;D[i];&nbsp;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">cash&nbsp;&nbsp;;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">j)</span><span style="color: #008000">//</span><span style="color: #008000">j由小变大，因为每件物品个数有无限个，可重复放入！！&lt;=中=不可少，否则WA</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></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;dp[j]</span><span style="color: #000000">=</span><span style="color: #000000">MAX(dp[j],dp[&nbsp;j&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;D[i]&nbsp;]</span><span style="color: #000000">+</span><span style="color: #000000">D[i]);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #008000">//</span><span style="color: #008000">多重背包</span><span style="color: #008000"><br /><img id="Codehighlighter1_560_940_Open_Image" onclick="this.style.display='none'; Codehighlighter1_560_940_Open_Text.style.display='none'; Codehighlighter1_560_940_Closed_Image.style.display='inline'; Codehighlighter1_560_940_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_560_940_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_560_940_Closed_Text.style.display='none'; Codehighlighter1_560_940_Open_Image.style.display='inline'; Codehighlighter1_560_940_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif"></span><span style="color: #000000">&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_560_940_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_560_940_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;amt</span><span style="color: #000000">=</span><span style="color: #000000">N[i]&nbsp;;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(&nbsp;k&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;amt&nbsp;)<br /><img id="Codehighlighter1_611_765_Open_Image" onclick="this.style.display='none'; Codehighlighter1_611_765_Open_Text.style.display='none'; Codehighlighter1_611_765_Closed_Image.style.display='inline'; Codehighlighter1_611_765_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_611_765_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_611_765_Closed_Text.style.display='none'; Codehighlighter1_611_765_Open_Image.style.display='inline'; Codehighlighter1_611_765_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="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_611_765_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_611_765_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">(&nbsp;j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;cash&nbsp;;&nbsp;j&nbsp;</span><span style="color: #000000">&gt;=</span><span style="color: #000000">k</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;D[i]&nbsp;;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">j)</span><span style="color: #008000">//</span><span style="color: #008000">j由大变小，因为后面的先改变后不会影响前面的，反之则不行！！</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></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;dp[j]</span><span style="color: #000000">=</span><span style="color: #000000">MAX(dp[j],dp[&nbsp;j&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;D[i]</span><span style="color: #000000">*</span><span style="color: #000000">k&nbsp;]</span><span style="color: #000000">+</span><span style="color: #000000">D[i]</span><span style="color: #000000">*</span><span style="color: #000000">k);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;amt</span><span style="color: #000000">-=</span><span style="color: #000000">k;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k</span><span style="color: #000000">*=</span><span style="color: #000000">2</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">(&nbsp;j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;cash&nbsp;;&nbsp;j&nbsp;</span><span style="color: #000000">&gt;=</span><span style="color: #000000">amt</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;D[i]&nbsp;;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">j)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[j]</span><span style="color: #000000">=</span><span style="color: #000000">MAX(dp[j],dp[&nbsp;j&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;amt</span><span style="color: #000000">*</span><span style="color: #000000">D[&nbsp;i&nbsp;]&nbsp;]</span><span style="color: #000000">+</span><span style="color: #000000">amt</span><span style="color: #000000">*</span><span style="color: #000000">D[&nbsp;i&nbsp;]);</span><span style="color: #008000">//</span><span style="color: #008000">头开始理解偏差，正确理解：amt现为分解为2^k后剩下的散量，<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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;&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;</span><span style="color: #008000">//</span><span style="color: #008000">即可供使用的物品数</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,dp[cash]);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span></div> <img src ="http://www.cppblog.com/snowshine09/aggbug/152271.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowshine09/" target="_blank">拥梦的小鱼</a> 2011-08-02 11:00 <a href="http://www.cppblog.com/snowshine09/archive/2011/08/02/152271.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>背包系列——poj 1837</title><link>http://www.cppblog.com/snowshine09/archive/2011/08/02/152258.html</link><dc:creator>拥梦的小鱼</dc:creator><author>拥梦的小鱼</author><pubDate>Tue, 02 Aug 2011 00:34:00 GMT</pubDate><guid>http://www.cppblog.com/snowshine09/archive/2011/08/02/152258.html</guid><wfw:comment>http://www.cppblog.com/snowshine09/comments/152258.html</wfw:comment><comments>http://www.cppblog.com/snowshine09/archive/2011/08/02/152258.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowshine09/comments/commentRss/152258.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowshine09/services/trackbacks/152258.html</trackback:ping><description><![CDATA[<div>题意：<br />就是说一个天平，给了C个距离（坐标），G个砝码，问砝码全用上，有几种让它平衡的办法。<br /><font face="隶书"><font size="5"><span style="font-family: Arial">dp[i][j]表示在挂上前i个物体的时,平衡度为j(j&gt;0时表示左边重,j=0时表示天平平衡,j&lt;0时表示右边重)时挂法的数量,而根据题意可以确定j的取值范围为:[-7500,7500],于是可以得到状态转移方程为:</span><br /></font><span style="font-family: Arial; color: #000000; font-size: 20px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dp[i][j]+=(dp[i-1][j-p[k]*g[i]]),&nbsp;&nbsp;&nbsp; p[k]表示第k个挂钩的位置,g[i]为第i个砝码的重量</span></font><br /><span style="font-family: Arial; color: #000000; font-size: 20px">&nbsp;&nbsp;&nbsp; 由于j-p[k]*g[i]可能为</span><span style="font-family: Arial; color: #000000; font-size: 20px">负数,因此统一加上7500,那么初始状态dp[0][7500]=1(此时表示天平平衡),表示不用物体且使得天平平衡时的方法只有一</span><span style="font-family: 隶书; color: #000000; font-size: 20px">种.</span><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-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #008000">//</span><span style="color: #008000">&nbsp;(1)用二维数组&nbsp;的&nbsp;01背包&nbsp;解。一个一个砝码往上放，头开始&nbsp;is[0][7500]=1，指0个砝码时为平衡7500，此时有一种方法。</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdlib</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cmath</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;wi[</span><span style="color: #000000">26</span><span style="color: #000000">],dis[</span><span style="color: #000000">26</span><span style="color: #000000">],</span><span style="color: #0000ff">is</span><span style="color: #000000">[</span><span style="color: #000000">26</span><span style="color: #000000">][</span><span style="color: #000000">15005</span><span style="color: #000000">],C,G;</span><span style="color: #008000">//</span><span style="color: #008000">力矩的极值为-7500，7500.下标为正，故计算时+7500</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;ZeroOnepack()<br /><img id="Codehighlighter1_230_662_Open_Image" onclick="this.style.display='none'; Codehighlighter1_230_662_Open_Text.style.display='none'; Codehighlighter1_230_662_Closed_Image.style.display='inline'; Codehighlighter1_230_662_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_230_662_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_230_662_Closed_Text.style.display='none'; Codehighlighter1_230_662_Open_Image.style.display='inline'; Codehighlighter1_230_662_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_230_662_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_230_662_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</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,k;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">for(i=1;i&lt;=G;i++)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(j=-7500;j&lt;=7500;j++)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(k=1;k&lt;=C;k++)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">if(j+7500&gt;=dis[k]*wi[i])<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is[i][j+7500]+=is[i-1][j+7500-dis[k]*wi[i]];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;}</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></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">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">G;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img id="Codehighlighter1_468_659_Open_Image" onclick="this.style.display='none'; Codehighlighter1_468_659_Open_Text.style.display='none'; Codehighlighter1_468_659_Closed_Image.style.display='inline'; Codehighlighter1_468_659_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_468_659_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_468_659_Closed_Text.style.display='none'; Codehighlighter1_468_659_Open_Image.style.display='inline'; Codehighlighter1_468_659_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_468_659_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_468_659_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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">7500</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">7500</span><span style="color: #000000">;j</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&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">0</span><span style="color: #000000">;&nbsp;k&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;C;k</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">if(j+7500&gt;=dis[k]*wi[i])</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">is</span><span style="color: #000000">[i][j</span><span style="color: #000000">+</span><span style="color: #000000">7500</span><span style="color: #000000">]</span><span style="color: #000000">+=</span><span style="color: #0000ff">is</span><span style="color: #000000">[i</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">][j</span><span style="color: #000000">+</span><span style="color: #000000">7500</span><span style="color: #000000">-</span><span style="color: #000000">dis[k]</span><span style="color: #000000">*</span><span style="color: #000000">wi[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">考虑放第i个砝码时的情况数，在第i-1个砝码上加。</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_675_1075_Open_Image" onclick="this.style.display='none'; Codehighlighter1_675_1075_Open_Text.style.display='none'; Codehighlighter1_675_1075_Closed_Image.style.display='inline'; Codehighlighter1_675_1075_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_675_1075_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_675_1075_Closed_Text.style.display='none'; Codehighlighter1_675_1075_Open_Image.style.display='inline'; Codehighlighter1_675_1075_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_675_1075_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_675_1075_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</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">,tem</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">,yz,neg,ws</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">,a</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">,b</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">C,</span><span style="color: #000000">&amp;</span><span style="color: #000000">G);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">for(i=1;i&lt;=C;++i)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d",&amp;dis[i]);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">}<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">for(i&nbsp;=&nbsp;1&nbsp;;&nbsp;i&nbsp;&lt;=&nbsp;G&nbsp;;&nbsp;++i&nbsp;&nbsp;&nbsp;&nbsp;)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d",&amp;wi[i]);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">}</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></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">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">C;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /><img id="Codehighlighter1_876_902_Open_Image" onclick="this.style.display='none'; Codehighlighter1_876_902_Open_Text.style.display='none'; Codehighlighter1_876_902_Closed_Image.style.display='inline'; Codehighlighter1_876_902_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_876_902_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_876_902_Closed_Text.style.display='none'; Codehighlighter1_876_902_Open_Image.style.display='inline'; Codehighlighter1_876_902_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_876_902_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_876_902_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">dis[i]);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i&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;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;G&nbsp;;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i&nbsp;&nbsp;&nbsp;&nbsp;)<br /><img id="Codehighlighter1_932_958_Open_Image" onclick="this.style.display='none'; Codehighlighter1_932_958_Open_Text.style.display='none'; Codehighlighter1_932_958_Closed_Image.style.display='inline'; Codehighlighter1_932_958_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_932_958_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_932_958_Closed_Text.style.display='none'; Codehighlighter1_932_958_Open_Image.style.display='inline'; Codehighlighter1_932_958_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_932_958_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_932_958_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&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">wi[i]);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;memset(</span><span style="color: #0000ff">is</span><span style="color: #000000">,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(</span><span style="color: #0000ff">is</span><span style="color: #000000">)</span><span style="color: #000000">/</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">));<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">is</span><span style="color: #000000">[</span><span style="color: #000000">0</span><span style="color: #000000">][</span><span style="color: #000000">7500</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ZeroOnepack();<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #0000ff">is</span><span style="color: #000000">[G][</span><span style="color: #000000">7500</span><span style="color: #000000">]);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span></div>2)用一维数组 01背包 解。须用2个一维数组以保证一个物体只能用一次，<br />因为：假如说只用到f这个数组，不用两个：那么讨论到第i个物体的时候，<br />f[j]是一个用了i这个物体来达到的状态，但是你f[k]是用f[j]来转移并且用上i的话，<br />这样f[k]就用了两次i了。二维数组就可以避免这种重复使用，或者用两个一维<br />数组，每次保证累加时使用的都是没用i时的：当我讨论到i这个物体的时候，先<br />把之前没讨论到i的状态先存下来，这样每到达一个新状态，都是用没有用到i的<br />状态tp来转移的，这样的新状态都是用了一次i<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-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #008000">//</span><span style="color: #008000">2)用一维数组&nbsp;01背包&nbsp;解。须用2个一维数组以保证一个物体只能用一次，<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">//</span><span style="color: #008000">因为：假如说只用到f这个数组，不用两个：那么讨论到第i个物体的时候，<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">//</span><span style="color: #008000">f[j]是一个用了i这个物体来达到的状态，但是你f[k]是用f[j]来转移并且用上i的话，<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">//</span><span style="color: #008000">这样f[k]就用了两次i了。二维数组就可以避免这种重复使用，或者用两个一维<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">//</span><span style="color: #008000">数组，每次保证累加时使用的都是没用i时的：当我讨论到i这个物体的时候，先<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">//</span><span style="color: #008000">把之前没讨论到i的状态先存下来，这样每到达一个新状态，都是用没有用到i的<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">//</span><span style="color: #008000">状态tp来转移的，这样的新状态都是用了一次i</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdlib</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;dp[</span><span style="color: #000000">15002</span><span style="color: #000000">],tp[</span><span style="color: #000000">15002</span><span style="color: #000000">],C,G;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_380_739_Open_Image" onclick="this.style.display='none'; Codehighlighter1_380_739_Open_Text.style.display='none'; Codehighlighter1_380_739_Closed_Image.style.display='inline'; Codehighlighter1_380_739_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_380_739_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_380_739_Closed_Text.style.display='none'; Codehighlighter1_380_739_Open_Image.style.display='inline'; Codehighlighter1_380_739_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_380_739_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_380_739_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j,k,pos[</span><span style="color: #000000">26</span><span style="color: #000000">],wi[</span><span style="color: #000000">26</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">C,</span><span style="color: #000000">&amp;</span><span style="color: #000000">G);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">C;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">pos[i]);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">G;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">wi[i]);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;dp[</span><span style="color: #000000">7500</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">G;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /><img id="Codehighlighter1_545_696_Open_Image" onclick="this.style.display='none'; Codehighlighter1_545_696_Open_Text.style.display='none'; Codehighlighter1_545_696_Closed_Image.style.display='inline'; Codehighlighter1_545_696_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_545_696_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_545_696_Closed_Text.style.display='none'; Codehighlighter1_545_696_Open_Image.style.display='inline'; Codehighlighter1_545_696_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&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_545_696_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_545_696_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memcpy(tp,dp,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(dp));<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(dp,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(dp));<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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">7500</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">7500</span><span style="color: #000000">;j</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">(k</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;k</span><span style="color: #000000">&lt;</span><span style="color: #000000">C;</span><span style="color: #000000">++</span><span style="color: #000000">k)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[j</span><span style="color: #000000">+</span><span style="color: #000000">7500</span><span style="color: #000000">]</span><span style="color: #000000">+=</span><span style="color: #000000">tp[j</span><span style="color: #000000">+</span><span style="color: #000000">7500</span><span style="color: #000000">-</span><span style="color: #000000">pos[k]</span><span style="color: #000000">*</span><span style="color: #000000">wi[i]];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,dp[</span><span style="color: #000000">7500</span><span style="color: #000000">]);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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 /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div></div><img src ="http://www.cppblog.com/snowshine09/aggbug/152258.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowshine09/" target="_blank">拥梦的小鱼</a> 2011-08-02 08:34 <a href="http://www.cppblog.com/snowshine09/archive/2011/08/02/152258.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>