﻿<?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++博客-yx-随笔分类-模拟</title><link>http://www.cppblog.com/csu-yx-2013/category/18794.html</link><description>Algorithm Study And So On</description><language>zh-cn</language><lastBuildDate>Sat, 14 Sep 2013 13:30:36 GMT</lastBuildDate><pubDate>Sat, 14 Sep 2013 13:30:36 GMT</pubDate><ttl>60</ttl><item><title>CSU OJ - 1183: 计算表达式的值</title><link>http://www.cppblog.com/csu-yx-2013/archive/2012/03/19/168324.html</link><dc:creator>yx</dc:creator><author>yx</author><pubDate>Mon, 19 Mar 2012 08:26:00 GMT</pubDate><guid>http://www.cppblog.com/csu-yx-2013/archive/2012/03/19/168324.html</guid><wfw:comment>http://www.cppblog.com/csu-yx-2013/comments/168324.html</wfw:comment><comments>http://www.cppblog.com/csu-yx-2013/archive/2012/03/19/168324.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/csu-yx-2013/comments/commentRss/168324.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/csu-yx-2013/services/trackbacks/168324.html</trackback:ping><description><![CDATA[&nbsp; &nbsp;题目意思很简单就是计算含括号的四则运算表达式的值。这个题目很坑爹，刚做的时候，题目描述里面只说里面会有空格，<br />后面居然把题目描述改了。所以，这个题无论怎么改，都是不对。因为，不知道是哪个坑爹的出题人，把数据里面加了\t，<br />难道出题人以为\t也是空格。估计，后面修改题目描述，也是发现这个问题后才改的。这可是还是哥了，改了无数多遍，<br />不处理非法数据就超时，略过非常数据当然直接WA了。好坑爹。<br />&nbsp; &nbsp;计算表达式的值，以前严蔚敏书上就说用栈构造出来后缀表达式后再计算值。但是这个方法未免太那个了，首先太麻烦了，<br />虽然算法思路不麻烦。我的做法是直接递归计算即可。碰到左括号递归计算新的表达式，<strong>右括号作为函数终止条件</strong>。否则，按照<br />四则运算的优先级计算当前的表达式。递归算法中需要记录前一个运算符合的优先级，<strong>如果前一个运算符的优先级比现在碰到的<br />运算符的优先级高，那么就应该直接返回答案了</strong>，当前碰到的运算符的计算交给下一次循环好了。<br /><br />&nbsp; &nbsp;代码如下：<br /><br /><div><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include&nbsp;&lt;stdio.h&gt;<br /><span style="color: #0000FF; ">#define</span>&nbsp;MAX&nbsp;(100&nbsp;+&nbsp;10)<br /><span style="color: #0000FF; ">char</span>&nbsp;szData[MAX];<br /><br /><span style="color: #0000FF; ">void</span>&nbsp;TrimSpace(<span style="color: #0000FF; ">char</span>*&nbsp;pszData)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">char</span>*&nbsp;pszRead&nbsp;=&nbsp;pszData;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">char</span>*&nbsp;pszWrite&nbsp;=&nbsp;pszData;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(*pszRead)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">由于数据中有\t,与先前题目描述不符合,不处理掉就直接超时了</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(*pszRead&nbsp;!=&nbsp;'&nbsp;'&nbsp;&amp;&amp;&nbsp;*pszRead&nbsp;!=&nbsp;'\t')<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*pszWrite++&nbsp;=&nbsp;*pszRead;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++pszRead;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;*pszWrite&nbsp;=&nbsp;'\0';<br />}<br /><br />//nKind代表前一个运算符合的优先级,开始时是0,+-是1,*/是2<br /><span style="color: #0000FF; ">double</span>&nbsp;Cal(<span style="color: #0000FF; ">char</span>*&amp;&nbsp;pszData,&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;nKind)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">double</span>&nbsp;fAns&nbsp;=&nbsp;0.0;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(*pszData&nbsp;&amp;&amp;&nbsp;*pszData&nbsp;!=&nbsp;')')<span style="color: #008000; ">//</span><span style="color: #008000; ">表达式终止的条件是到达'\0'或者碰到右括号</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(*pszData&nbsp;&gt;=&nbsp;'0'&nbsp;&amp;&amp;&nbsp;*pszData&nbsp;&lt;=&nbsp;'9')<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fAns&nbsp;=&nbsp;10&nbsp;*&nbsp;fAns&nbsp;+&nbsp;*pszData&nbsp;-&nbsp;'0';<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++pszData;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(*pszData&nbsp;==&nbsp;'+')<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(nKind&nbsp;&gt;=&nbsp;1)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;fAns;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++pszData;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fAns&nbsp;+=&nbsp;Cal(pszData,&nbsp;1);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(*pszData&nbsp;==&nbsp;'-')<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(nKind&nbsp;&gt;=&nbsp;1)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;fAns;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++pszData;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fAns&nbsp;-=&nbsp;Cal(pszData,&nbsp;1);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(*pszData&nbsp;==&nbsp;'*')<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(nKind&nbsp;&gt;=&nbsp;2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;fAns;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++pszData;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fAns&nbsp;*=&nbsp;Cal(pszData,&nbsp;2);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(*pszData&nbsp;==&nbsp;'/')<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(nKind&nbsp;&gt;=&nbsp;2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;fAns;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++pszData;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fAns&nbsp;/=&nbsp;Cal(pszData,&nbsp;2);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(*pszData&nbsp;==&nbsp;'(')<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++pszData;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fAns&nbsp;=&nbsp;Cal(pszData,&nbsp;0);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++pszData;<span style="color: #008000; ">//</span><span style="color: #008000; ">移到')'后面</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;fAns;<span style="color: #008000; ">//</span><span style="color: #008000; ">一个括号内的是一个完整的表达式,因此直接返回</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;fAns;<br />}<br /><br /><span style="color: #0000FF; ">int</span>&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(gets(szData))<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TrimSpace(szData);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">char</span>*&nbsp;pszData&nbsp;=&nbsp;szData;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%.4f\n",&nbsp;Cal(pszData,&nbsp;0));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}</div>&nbsp; &nbsp;一个递归函数能计算出表达式的值，而且能处理优先级和括号，如果是以前的话，我应该是写不出来的。再把算法的实现细节改改，<br />应该也能计算出浮点数的表达式了。<br />&nbsp;&nbsp;&nbsp;</div><img src ="http://www.cppblog.com/csu-yx-2013/aggbug/168324.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/csu-yx-2013/" target="_blank">yx</a> 2012-03-19 16:26 <a href="http://www.cppblog.com/csu-yx-2013/archive/2012/03/19/168324.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>生成排列的算法(POJ - 1256 和 POJ百练 - 1833)</title><link>http://www.cppblog.com/csu-yx-2013/archive/2011/12/26/162852.html</link><dc:creator>yx</dc:creator><author>yx</author><pubDate>Mon, 26 Dec 2011 07:53:00 GMT</pubDate><guid>http://www.cppblog.com/csu-yx-2013/archive/2011/12/26/162852.html</guid><wfw:comment>http://www.cppblog.com/csu-yx-2013/comments/162852.html</wfw:comment><comments>http://www.cppblog.com/csu-yx-2013/archive/2011/12/26/162852.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/csu-yx-2013/comments/commentRss/162852.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/csu-yx-2013/services/trackbacks/162852.html</trackback:ping><description><![CDATA[题目1描述:<br />输入:一个序列s,该序列里面可能会有同样的字符,不一定有序<br />输出:打乱输入中的序列,可能产生的所有新的序列<br />题目2描述:<br /><div>输入:一个序列s,该序列里面可能会有同样的字符,不一定有序 和 一个整数k<br />输出:该序列往后计算第k个序列,所有序列是以字典序排序的<br /><br />如果会有序搜索的童鞋自然而然能立刻做出来第一个题目,可是第二个题目在s较长的情况下,却需要用模拟而不是搜索...<br />大家都知道STL里面有个泛函模版, prev_permutation和next_permutation,用法也很简单,实现的就是题目2的功能...<br />但是算法最好得靠自己想出来,自己想出来的才是自己的,碰到新的问题才能产生思想的火花...<br /><br />废话少说,题目1的解法就是深搜,不过需要加上一个bool数组标记和一个函数确定不同字符之间的大小(有可能这个大小还不是Ascii码就能决定的),<br />大致描述下搜索过程,比如输入序列是12345,那么我搜索的过程大致是第一层按顺序选取1-5,进入第二层的时候也是按顺序选取1-5,<br />以此类推,但是每一层里面都只能选前面的层次没有选过的数,而且因为有重复字符,算法还必须保证每一层里面按顺序选取的字符必须是升序的,<br />熟悉顺序搜索和回溯的同学,很自然就会产生这样的想法...<br />POJ - 1256的代码如下:<br /><div><div>#include &lt;stdio.h&gt;</div><div>#include &lt;string.h&gt;</div><div>#include &lt;ctype.h&gt;</div><div>#include &lt;stdlib.h&gt;</div><div>#include &lt;algorithm&gt;</div><div>#define MAX (13 + 10)</div><div>using namespace std;</div><div></div><div>bool bUsed[MAX];</div><div>char szAns[MAX];</div><div>char szInput[MAX];</div><div></div><div>bool CmpChar(char chOne, char chTwo)</div><div>{</div><div>&nbsp; &nbsp; if (abs(chOne - chTwo) != 'a' - 'A')</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return tolower(chOne) - tolower(chTwo) &lt; 0;</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp; return chOne - chTwo &lt; 0;</div><div>}</div><div></div><div>bool Greater(char chOne, char chTwo)</div><div>{</div><div>&nbsp; &nbsp; if (abs(chOne - chTwo) != 'a' - 'A')</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return tolower(chOne) - tolower(chTwo) &gt; 0;</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp; return chOne - chTwo &gt; 0;</div><div>}</div><div></div><div>void Gen(int nDepth, int nLen)</div><div>{</div><div>&nbsp; &nbsp; if (nDepth == nLen)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; szAns[nLen] = '\0';</div><div>&nbsp; &nbsp; &nbsp; &nbsp; printf("%s\n", szAns);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return;</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; char chLast = '\0';</div><div>&nbsp; &nbsp; for (int i = 0; i &lt; nLen; ++i)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (!bUsed[i] &amp;&amp; Greater(szInput[i], chLast))</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bUsed[i] = true;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; szAns[nDepth] = szInput[i];</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gen(nDepth + 1, nLen);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bUsed[i] = false;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chLast = szInput[i];</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div>}</div><div></div><div>int main()</div><div>{</div><div>&nbsp; &nbsp; int nCases;</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; scanf("%d", &amp;nCases);</div><div>&nbsp; &nbsp; while (nCases--)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; scanf("%s", szInput);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; int nLen = strlen(szInput);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; sort(szInput, szInput + nLen, CmpChar);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Gen(0, nLen);</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; return 0;</div><div>}</div></div><div></div>题目2的解法是模拟,功能类似与STL的那2个泛型模版函数,算法的大致过程是想办法从当前序列进入下一个刚好比其大或者刚好比其小的序列...很自然我们想到要把序列后面大的字符交和前面小的字符交换就会使序列变大,为了使其刚好变大,可以把交换后的字符从交换位置起至最后都排序一下,现在的问题是我们如何选取2个字符交换...正确的想法是,我们从最后面开始往前面看,寻找一个最长的递增序列,找到之后,我们只需要选取递增序列前面的那个字符chBefore和递增序列里面的一个最小的比chBefore大的字符交换即可...交换之后,将新的递增序列排序一下即可...<br />为什么这样做了,因为从后往前看的递增序列,是不能交换2个字符让当前序列变大的,所以必须选取最长递增序列前面的那个字符交换...<br /><br />POJ百练 - 1833 的代码如下:<br /><div><div>#include &lt;stdio.h&gt;</div><div>#include &lt;string.h&gt;</div><div>#include &lt;algorithm&gt;</div><div>#define MAX (1024 + 10)</div><div>using namespace std;</div><div></div><div>int nInput[MAX];</div><div></div><div>void GetNext(int* nInput, int nLen)</div><div>{</div><div>&nbsp; &nbsp; int i = nLen - 2;</div><div>&nbsp; &nbsp; while (i &gt;= 0)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (nInput[i] &gt;= nInput[i + 1])</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --i;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; else</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int k = i + 1;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = nLen - 1; j &gt; i; --j)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (nInput[j] &gt; nInput[i] &amp;&amp; nInput[j] &lt; nInput[k])</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k = j;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swap(nInput[i], nInput[k]);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sort(nInput + i + 1, nInput + nLen);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; sort(nInput, nInput + nLen);</div><div>}</div><div></div><div>int main()</div><div>{</div><div>&nbsp; &nbsp; int nCases;</div><div></div><div>&nbsp; &nbsp; scanf("%d", &amp;nCases);</div><div>&nbsp; &nbsp; while (nCases--)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; int nLen;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; int nK;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; scanf("%d%d", &amp;nLen, &amp;nK);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; nLen; ++i)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf("%d", &amp;nInput[i]);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; nK; ++i)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetNext(nInput, nLen);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; nLen; ++i)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("%d%s", nInput[i], i == nLen - 1 ? "\n" : " ");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div></div><div>&nbsp; &nbsp; return 0;</div><div>}</div></div><div></div></div><img src ="http://www.cppblog.com/csu-yx-2013/aggbug/162852.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/csu-yx-2013/" target="_blank">yx</a> 2011-12-26 15:53 <a href="http://www.cppblog.com/csu-yx-2013/archive/2011/12/26/162852.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>POJ百练 - 2814:拨钟问题</title><link>http://www.cppblog.com/csu-yx-2013/archive/2011/11/28/161082.html</link><dc:creator>yx</dc:creator><author>yx</author><pubDate>Mon, 28 Nov 2011 11:55:00 GMT</pubDate><guid>http://www.cppblog.com/csu-yx-2013/archive/2011/11/28/161082.html</guid><wfw:comment>http://www.cppblog.com/csu-yx-2013/comments/161082.html</wfw:comment><comments>http://www.cppblog.com/csu-yx-2013/archive/2011/11/28/161082.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/csu-yx-2013/comments/commentRss/161082.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/csu-yx-2013/services/trackbacks/161082.html</trackback:ping><description><![CDATA[链接:&nbsp;<a href="http://poj.grids.cn/practice/2814">http://poj.grids.cn/practice/2814<br /><br />这</a>个题目可以枚举或者直接暴力。但是,这之前必须弄明白答案的解空间。。。也就是解可能的情况。。。很简单,一共有9种移动方案。也很了然的知道对于某种方案使用N次的效果等同于N%4的效果,也就是说某种方案只可能使用0,1,2,3次。。。一共有9种方案,那么一共就只有4^9种可能的解。。。这么小的解空间,无论用什么方法都不会超时了。。。暴力可以才用9重循环,或者深搜,当时觉得写9重循环是件很糗的事情,就果断深搜了。。。<br />如果这题才用枚举的方法的话,思考方式还是那样先确定假设解的部分情况,通过已经知道的规则确定解的其它情况,然后求出这个解,判断这个解是否满足题目要求。。。比如,我们可以枚举1,2,3号方案的情况,根据规则确定其它方案的使用情况,求出所有方案的使用情况后,判断假设的解是否满足要求就可以了...<br /><br />我才用的是dfs+剪枝,这个题目其实题意或者说答案有误,因为答案是搜索找到第一个解,而不是所谓的最短序列的解,当然如果数据使得2者都是一样的话,那么题意就无误了...我的代码是假设找到的第一个就是最短序列的,这种情况下才能使用剪枝,因为找到一个解后就不需要继续找了...<br /><br /><br />代码如下:<br /><div><div>#include &lt;stdio.h&gt;</div><div></div><div>int nMinTimes;</div><div>int nPath[40];</div><div>bool bFind = false;</div><div></div><div>char* szMoves[10] =</div><div>{</div><div>&nbsp; &nbsp; NULL,</div><div>&nbsp; &nbsp; "ABDE",</div><div>&nbsp; &nbsp; "ABC",</div><div>&nbsp; &nbsp; "BCEF",</div><div>&nbsp; &nbsp; "ADG",</div><div>&nbsp; &nbsp; "BDEFH",</div><div>&nbsp; &nbsp; "CFI",</div><div>&nbsp; &nbsp; "DEGH",</div><div>&nbsp; &nbsp; "GHI",</div><div>&nbsp; &nbsp; "EFHI"</div><div>};</div><div></div><div>bool IsPosOK(int* nPos)</div><div>{</div><div>&nbsp; &nbsp; for (int i = 0; i &lt; 9; ++i)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (nPos[i])</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp; return true;</div><div>}</div><div></div><div>void Move(int nChoose, int nTimes, int* nPos)</div><div>{</div><div>&nbsp; &nbsp; if (nTimes &gt; 0)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; char* pszStr = szMoves[nChoose];</div><div>&nbsp; &nbsp; &nbsp; &nbsp; while (*pszStr)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nPos[*pszStr - 'A'] = (nPos[*pszStr - 'A'] + nTimes) % 4;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++pszStr;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div>}</div><div></div><div>void MoveBack(int nChoose, int nTimes, int* nPos)</div><div>{</div><div>&nbsp; &nbsp; if (nTimes &gt; 0)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; char* pszStr = szMoves[nChoose];</div><div>&nbsp; &nbsp; &nbsp; &nbsp; while (*pszStr)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nPos[*pszStr - 'A'] = (nPos[*pszStr - 'A'] - nTimes + 4) % 4;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++pszStr;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div>}</div><div></div><div>void Cal(int nChoose, int* nPos, int* nUsed, int nUsedTimes)</div><div>{</div><div>&nbsp; &nbsp; if (nChoose == 10)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (IsPosOK(nPos) &amp;&amp; !bFind)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nMinTimes = nUsedTimes;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; nMinTimes; ++i)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nPath[i] = nUsed[i];</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bFind = true;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div></div><div>&nbsp; &nbsp; &nbsp; &nbsp; return;</div><div>&nbsp; &nbsp; }</div><div></div><div>&nbsp; &nbsp; for (int i = 0; i &lt;= 3; ++i)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Move(nChoose, i, nPos);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j &lt; i; ++j)//放入i次的nChoose</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nUsed[nUsedTimes + j] = nChoose;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (!bFind)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cal(nChoose + 1, nPos, nUsed, nUsedTimes + i);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; MoveBack(nChoose, i, nPos);</div><div>&nbsp; &nbsp; }</div><div>}</div><div></div><div>int main()</div><div>{</div><div>&nbsp; &nbsp; int nPos[9];</div><div>&nbsp; &nbsp; int nUsed[40];</div><div></div><div>&nbsp; &nbsp; for (int i = 0; i &lt; 9; ++i)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; scanf("%d", &amp;nPos[i]);</div><div>&nbsp; &nbsp; }</div><div></div><div>&nbsp; &nbsp; Cal(1, nPos, nUsed, 0);</div><div>&nbsp; &nbsp; for (int i = 0; i &lt; nMinTimes; ++i)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; printf("%d", nPath[i]);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (i != nMinTimes - 1)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; putchar(' ');</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; else</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; putchar('\n');</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; return 0;</div><div>}<br /><br />这道题其实我wa了近10次,原因就是Move和MoveBack写错了,没有移动nTimes次,而前面一直写成了1,昨晚wa得实在无语了...今天晚上检查才突然发现的...<br />这半个多月做了60道题了,都没有改动这低级的bug习惯...实在无语...递归,回溯,剪枝都写上了...唉...实在无语...还不如直接9重循环,多省心...真不该歧视某种方法的...</div></div><div></div><img src ="http://www.cppblog.com/csu-yx-2013/aggbug/161082.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/csu-yx-2013/" target="_blank">yx</a> 2011-11-28 19:55 <a href="http://www.cppblog.com/csu-yx-2013/archive/2011/11/28/161082.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>