<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>别摸我｜catch the digital flow &#187; C/C++</title>
	<atom:link href="http://hellobmw.com/category/programs/cc/feed" rel="self" type="application/rss+xml" />
	<link>http://hellobmw.com</link>
	<description>信强哥，有房住</description>
	<lastBuildDate>Sun, 15 Aug 2010 07:14:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Visual Studio 用 debug 启动调试挂起，提示 Visual Studio Is Busy</title>
		<link>http://hellobmw.com/archives/visual-studio-debugging-hangs-with-busy-message.html</link>
		<comments>http://hellobmw.com/archives/visual-studio-debugging-hangs-with-busy-message.html#comments</comments>
		<pubDate>Wed, 11 Aug 2010 03:27:08 +0000</pubDate>
		<dc:creator>北极冰仔</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[程序设计]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://hellobmw.com/?p=1253</guid>
		<description><![CDATA[<p>最近遇到了很多件周围人从来没遇到过的事，很是费解。</p>
<p>其中一件就是我的 Visual Studio 以 debug 方式启动调试某个工程，经常会挂起，output 显示正在加载某个 dll 导出的符号，然后就不动了，系统托盘有时会同步提示“Visual Studio is busy”，概率达到 90% 以上，经过再三验证，确实是我的 Visual Studio 出问题了，一开始以为是我之前改过 include/lib/bin 的默认路径，编译依赖和运行依赖与别人不同所致，事实证明不是这个原因。</p>
<p>刚才同样的情况再次发生，终于找到了<a href="http://www.developmentnow.com/g/51_2006_12_0_0_864396/VS-2005-Hangs-with-Microsoft-Visual-Studio-Is-Busy-message.htm"  class="external">有价值的线索</a>：</p>
<blockquote><p>
This might be related with symbol server configuration.<br />
Check that valid symbol server path is used, and that network access works<br />
properly. Alternatively, you can try to disable symbol server access completely,<br />
to see if the problem goes away. (Tools | Options | Debugging | etc., as well as<br />
remove _NT_SYMBOL_PATH environment variable if it exists)
</p></blockquote>
<p>猛地想起，之前另外一个项目中，为了跟踪一个 bug，在调试 dump 的时候配置了符号服务器。禁用之，搞定。另外一个诡异的现象也随之消失：同样配置的机器，我启动调试比别人会慢很多。 <img src='http://hellobmw.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<div class="entry-related"><h3>Related Posts</h3><ul><li><a href="http://hellobmw.com/archives/restore-vs2008-2005-file-association-after-vc6-installed.html"  title="[解决]安装 VC2008(2005) 后安装 VC6 导致 .sln 图标关联错误">[解决]安装 VC2008(2005) 后安装 VC6 导致 .sln 图标关联错误</a></li></ul></div>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2010.｜<a href="http://hellobmw.com/archives/visual-studio-debugging-hangs-with-busy-message.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/visual-studio-debugging-hangs-with-busy-message.html#comments" >No comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>
]]></description>
			<content:encoded><![CDATA[<p>最近遇到了很多件周围人从来没遇到过的事，很是费解。</p>
<p>其中一件就是我的 Visual Studio 以 debug 方式启动调试某个工程，经常会挂起，output 显示正在加载某个 dll 导出的符号，然后就不动了，系统托盘有时会同步提示“Visual Studio is busy”，概率达到 90% 以上，经过再三验证，确实是我的 Visual Studio 出问题了，一开始以为是我之前改过 include/lib/bin 的默认路径，编译依赖和运行依赖与别人不同所致，事实证明不是这个原因。</p>
<p>刚才同样的情况再次发生，终于找到了<a href="http://www.developmentnow.com/g/51_2006_12_0_0_864396/VS-2005-Hangs-with-Microsoft-Visual-Studio-Is-Busy-message.htm"  class="external">有价值的线索</a>：</p>
<blockquote><p>
This might be related with symbol server configuration.<br />
Check that valid symbol server path is used, and that network access works<br />
properly. Alternatively, you can try to disable symbol server access completely,<br />
to see if the problem goes away. (Tools | Options | Debugging | etc., as well as<br />
remove _NT_SYMBOL_PATH environment variable if it exists)
</p></blockquote>
<p>猛地想起，之前另外一个项目中，为了跟踪一个 bug，在调试 dump 的时候配置了符号服务器。禁用之，搞定。另外一个诡异的现象也随之消失：同样配置的机器，我启动调试比别人会慢很多。 <img src='http://hellobmw.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<div class="entry-related"><h3>Related Posts</h3><ul><li><a href="http://hellobmw.com/archives/restore-vs2008-2005-file-association-after-vc6-installed.html"  title="[解决]安装 VC2008(2005) 后安装 VC6 导致 .sln 图标关联错误">[解决]安装 VC2008(2005) 后安装 VC6 导致 .sln 图标关联错误</a></li></ul></div>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2010.｜<a href="http://hellobmw.com/archives/visual-studio-debugging-hangs-with-busy-message.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/visual-studio-debugging-hangs-with-busy-message.html#comments" >No comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>]]></content:encoded>
			<wfw:commentRss>http://hellobmw.com/archives/visual-studio-debugging-hangs-with-busy-message.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>float 转换 DWORD 的陷阱</title>
		<link>http://hellobmw.com/archives/traps-in-casting-float-to-dword.html</link>
		<comments>http://hellobmw.com/archives/traps-in-casting-float-to-dword.html#comments</comments>
		<pubDate>Tue, 21 Jul 2009 07:14:52 +0000</pubDate>
		<dc:creator>北极冰仔</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Direct3D]]></category>
		<category><![CDATA[程序设计]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[traps]]></category>

		<guid isPermaLink="false">http://hellobmw.com/?p=1237</guid>
		<description><![CDATA[<p>本以为是个很简单的东西，但其中隐藏了一个很大的陷阱：不能使用强制转换。亲手试试下面的几行代码就知道为什么了：</p>
<div class="syntax_hilite">
<div id="cpp-1">
<div class="cpp"><span style="color: #339900;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339900;">#include &lt;Windows.h&gt;</span></p>
<p><span style="color: #0000ff;">int</span> main<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>* argv<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">float</span> f = <span style="color: #0000dd;">2</span>.0f;<br />
&nbsp; byte content<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #000000;">&#93;</span>;<br />
&nbsp; ::<span style="color: #00eeff;">ZeroMemory</span><span style="color: #000000;">&#40;</span>content, <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span>content<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000dd;">memcpy</span><span style="color: #000000;">&#40;</span>content, <span style="color: #000000;">&#40;</span>byte*<span style="color: #000000;">&#41;</span>&amp;f, <span style="color: #0000dd;">4</span><span style="color: #000000;">&#41;</span>; <span style="color: #ff0000;">// float 原始内容</span></p>
<p>&nbsp; DWORD dw = <span style="color: #000000;">&#40;</span>DWORD<span style="color: #000000;">&#41;</span>f; <span style="color: #ff0000;">// C 风格强制转换，错误</span><br />
&nbsp; <span style="color: #0000dd;">memcpy</span><span style="color: #000000;">&#40;</span>content, <span style="color: #000000;">&#40;</span>byte*<span style="color: #000000;">&#41;</span>&amp;dw, <span style="color: #0000dd;">4</span><span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; DWORD dw2 = *<span style="color: #000000;">&#40;</span>DWORD*<span style="color: #000000;">&#41;</span>&amp;f; <span style="color: #ff0000;">// 转换的正确做法</span><br />
&nbsp; <span style="color: #0000dd;">memcpy</span><span style="color: #000000;">&#40;</span>content, <span style="color: #000000;">&#40;</span>byte*<span style="color: #000000;">&#41;</span>&amp;dw2, <span style="color: #0000dd;">4</span><span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; DWORD dw3 = static_cast&lt;DWORD&gt;<span style="color: #000000;">&#40;</span>f<span style="color: #000000;">&#41;</span>; <span style="color: #ff0000;">// C++ 风格强制转换，错误</span><br />
&nbsp; <span style="color: #0000dd;">memcpy</span><span style="color: #000000;">&#40;</span>content, <span style="color: #000000;">&#40;</span>byte*<span style="color: #000000;">&#41;</span>&amp;dw3, <span style="color: #0000dd;">4</span><span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;<br />
<span style="color: #000000;">&#125;</span></div>
</div>
</div>
<p></p>
<p>今天一整天全栽在这个上面了。</p>
<div class="entry-related"><h3>Related Posts</h3><ul><li><a href="http://hellobmw.com/archives/alchemy-cc-to-as3-compiler.html"  title="神器 Alchemy！C/C++ 到 AS3 的编译器">神器 Alchemy！C/C++ 到 AS3 的编译器</a></li><li><a href="http://hellobmw.com/archives/why-put-constant-at-the-left-side-of-equal-to-operator.html"  title="[C/C++] 为什么把常量写在 == 运算符的左边">[C/C++] 为什么把常量写在 == 运算符的左边</a></li><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-2.html"  title="Google C++ 编程风格指南（1）：头文件（2）">Google C++ 编程风格指南（1）：头文件（2）</a></li><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-1.html"  title="Google C++ 编程风格指南（1）：头文件（1）">Google C++ 编程风格指南（1）：头文件（1）</a></li><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-0-background.html"  title="Google C++ 编程风格指南（0）：背景">Google C++ 编程风格指南（0）：背景</a></li><li><a href="http://hellobmw.com/archives/protocol-buffers-performance.html"  title="Protocol Buffers 性能测试">Protocol Buffers 性能测试</a></li></ul></div>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2009.｜<a href="http://hellobmw.com/archives/traps-in-casting-float-to-dword.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/traps-in-casting-float-to-dword.html#comments" >2 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>
]]></description>
			<content:encoded><![CDATA[<p>本以为是个很简单的东西，但其中隐藏了一个很大的陷阱：不能使用强制转换。亲手试试下面的几行代码就知道为什么了：</p>
<div class="syntax_hilite">
<div id="cpp-2">
<div class="cpp"><span style="color: #339900;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339900;">#include &lt;Windows.h&gt;</span></p>
<p><span style="color: #0000ff;">int</span> main<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>* argv<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">float</span> f = <span style="color: #0000dd;">2</span>.0f;<br />
&nbsp; byte content<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #000000;">&#93;</span>;<br />
&nbsp; ::<span style="color: #00eeff;">ZeroMemory</span><span style="color: #000000;">&#40;</span>content, <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span>content<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000dd;">memcpy</span><span style="color: #000000;">&#40;</span>content, <span style="color: #000000;">&#40;</span>byte*<span style="color: #000000;">&#41;</span>&amp;f, <span style="color: #0000dd;">4</span><span style="color: #000000;">&#41;</span>; <span style="color: #ff0000;">// float 原始内容</span></p>
<p>&nbsp; DWORD dw = <span style="color: #000000;">&#40;</span>DWORD<span style="color: #000000;">&#41;</span>f; <span style="color: #ff0000;">// C 风格强制转换，错误</span><br />
&nbsp; <span style="color: #0000dd;">memcpy</span><span style="color: #000000;">&#40;</span>content, <span style="color: #000000;">&#40;</span>byte*<span style="color: #000000;">&#41;</span>&amp;dw, <span style="color: #0000dd;">4</span><span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; DWORD dw2 = *<span style="color: #000000;">&#40;</span>DWORD*<span style="color: #000000;">&#41;</span>&amp;f; <span style="color: #ff0000;">// 转换的正确做法</span><br />
&nbsp; <span style="color: #0000dd;">memcpy</span><span style="color: #000000;">&#40;</span>content, <span style="color: #000000;">&#40;</span>byte*<span style="color: #000000;">&#41;</span>&amp;dw2, <span style="color: #0000dd;">4</span><span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; DWORD dw3 = static_cast&lt;DWORD&gt;<span style="color: #000000;">&#40;</span>f<span style="color: #000000;">&#41;</span>; <span style="color: #ff0000;">// C++ 风格强制转换，错误</span><br />
&nbsp; <span style="color: #0000dd;">memcpy</span><span style="color: #000000;">&#40;</span>content, <span style="color: #000000;">&#40;</span>byte*<span style="color: #000000;">&#41;</span>&amp;dw3, <span style="color: #0000dd;">4</span><span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;<br />
<span style="color: #000000;">&#125;</span></div>
</div>
</div>
<p></p>
<p>今天一整天全栽在这个上面了。</p>
<div class="entry-related"><h3>Related Posts</h3><ul><li><a href="http://hellobmw.com/archives/alchemy-cc-to-as3-compiler.html"  title="神器 Alchemy！C/C++ 到 AS3 的编译器">神器 Alchemy！C/C++ 到 AS3 的编译器</a></li><li><a href="http://hellobmw.com/archives/why-put-constant-at-the-left-side-of-equal-to-operator.html"  title="[C/C++] 为什么把常量写在 == 运算符的左边">[C/C++] 为什么把常量写在 == 运算符的左边</a></li><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-2.html"  title="Google C++ 编程风格指南（1）：头文件（2）">Google C++ 编程风格指南（1）：头文件（2）</a></li><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-1.html"  title="Google C++ 编程风格指南（1）：头文件（1）">Google C++ 编程风格指南（1）：头文件（1）</a></li><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-0-background.html"  title="Google C++ 编程风格指南（0）：背景">Google C++ 编程风格指南（0）：背景</a></li><li><a href="http://hellobmw.com/archives/protocol-buffers-performance.html"  title="Protocol Buffers 性能测试">Protocol Buffers 性能测试</a></li></ul></div>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2009.｜<a href="http://hellobmw.com/archives/traps-in-casting-float-to-dword.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/traps-in-casting-float-to-dword.html#comments" >2 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>]]></content:encoded>
			<wfw:commentRss>http://hellobmw.com/archives/traps-in-casting-float-to-dword.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Google 在 Twitter 上的二进制问候是什么</title>
		<link>http://hellobmw.com/archives/google-said-hi-on-twitter.html</link>
		<comments>http://hellobmw.com/archives/google-said-hi-on-twitter.html#comments</comments>
		<pubDate>Thu, 09 Apr 2009 02:27:20 +0000</pubDate>
		<dc:creator>北极冰仔</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[程序设计]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://hellobmw.com/?p=1231</guid>
		<description><![CDATA[<p><img src="https://s3.amazonaws.com/twitter_production/profile_images/77186109/favicon_bigger.png" alt="Google on Twitter" style="float:right" /> 第一眼看到 Google 在 Twitter 上用二进制向人们打招呼的时候，就感觉应该说的是一句话，只是没时间去搞清楚是什么。正好最近实在无聊地厉害，为了避免生命进一步浪费在如此阳光明媚狗熊撒欢的季节，就写了几行代码看看 Google 到底说了什么。</p>
<div class="syntax_hilite">
<div id="cpp-3">
<div class="cpp"><span style="color: #339900;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339900;">#include &lt;string.h&gt;</span><br />
<span style="color: #339900;">#include &lt;malloc.h&gt;</span><br />
<span style="color: #339900;">#include &lt;memory.h&gt;</span></p>
<p>
<span style="color: #0000ff;">char</span> strchar2char<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">char</span> buf<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #0000ff;">void</span> parse<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* src, <span style="color: #0000ff;">char</span>* dst<span style="color: #000000;">&#41;</span>;</p>
<p>
<span style="color: #0000ff;">int</span> main<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>* argv<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* src = <span style="color: #666666;">"01100110 01100101 01100101 01101100 01101001 01101110 01100111 00100000 01101100 01110101 01100011 01101011 01111001 00001010"</span>;<br />
&nbsp; <span style="color: #0000ff;">size_t</span> len_dst = <span style="color: #000000;">&#40;</span><span style="color: #0000dd;">strlen</span><span style="color: #000000;">&#40;</span>src<span style="color: #000000;">&#41;</span> + <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span> / <span style="color: #0000dd;">9</span>;</p>
<p>&nbsp; <span style="color: #0000ff;">char</span>* dst = <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">char</span>*<span style="color: #000000;">&#41;</span><span style="color: #0000dd;">malloc</span><span style="color: #000000;">&#40;</span>len_dst + <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">memset</span><span style="color: #000000;">&#40;</span>dst, <span style="color: #0000dd;">0</span>, len_dst + <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; parse<span style="color: #000000;">&#40;</span>src, dst<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"src: %s<span style="color: #666666; font-weight: bold;">\n</span>dst: %s<span style="color: #666666; font-weight: bold;">\n</span>"</span>, src, dst<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000dd;">free</span><span style="color: #000000;">&#40;</span>dst<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;<br />
<span style="color: #000000;">&#125;</span></p>
<p>
<span style="color: #0000ff;">char</span> strchar2char<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">char</span> buf<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> mask = 0x80, ret = <span style="color: #0000dd;">0</span>, i;</p>
<p>&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;<span style="color: #0000dd;">8</span>; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #666666;">'1'</span> == buf<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; ret |= mask;</p>
<p>&nbsp; &nbsp; mask&gt;&gt;= <span style="color: #0000dd;">1</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span></p>
<p>&nbsp; <span style="color: #0000ff;">return</span> ret;<br />
<span style="color: #000000;">&#125;</span></p>
<p>
<span style="color: #0000ff;">void</span> parse<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* src, <span style="color: #0000ff;">char</span>* dst<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">char</span> buf<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">8</span><span style="color: #000000;">&#93;</span>;<br />
&nbsp; <span style="color: #0000ff;">int</span> i;</p>
<p>&nbsp; <span style="color: #0000ff;">while</span> <span style="color: #000000;">&#40;</span>*src<span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #666666;">' '</span> == *src<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; src++;</p>
<p>&nbsp; &nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;<span style="color: #0000dd;">8</span>; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; buf<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = src<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>;</p>
<p>&nbsp; &nbsp; *dst = strchar2char<span style="color: #000000;">&#40;</span>buf<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; &nbsp; src += <span style="color: #0000dd;">8</span>;<br />
&nbsp; &nbsp; dst++;<br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div>
</div>
</div>
<p></p>
<p><span style="color:#fff">答案揭晓：feeling lucky</span></p>
<div class="entry-related"><h3>Related Posts</h3><ul><li><a href="http://hellobmw.com/archives/follow-me-on-twitter.html"  title="Follow me on Twitter">Follow me on Twitter</a></li><li><a href="http://hellobmw.com/archives/google-pagerank-checker-with-ajax.html"  title="Google PageRank Checker with Ajax">Google PageRank Checker with Ajax</a></li><li><a href="http://hellobmw.com/archives/google-pagerank-checker.html"  title="Google PageRank Checker">Google PageRank Checker</a></li><li><a href="http://hellobmw.com/archives/google-friend-connect.html"  title="本站也添加 Google Friend Connect">本站也添加 Google Friend Connect</a></li><li><a href="http://hellobmw.com/archives/recommended-google-chrome-cartoon-in-chinese.html"  title="[强推] Google Chrome 漫画书中文版">[强推] Google Chrome 漫画书中文版</a></li><li><a href="http://hellobmw.com/archives/google-pagerank-updated.html"  title="Google PageRank 貌似又更新了">Google PageRank 貌似又更新了</a></li></ul></div>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2009.｜<a href="http://hellobmw.com/archives/google-said-hi-on-twitter.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/google-said-hi-on-twitter.html#comments" >8 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>
]]></description>
			<content:encoded><![CDATA[<p><img src="https://s3.amazonaws.com/twitter_production/profile_images/77186109/favicon_bigger.png" alt="Google on Twitter" style="float:right" /> 第一眼看到 Google 在 Twitter 上用二进制向人们打招呼的时候，就感觉应该说的是一句话，只是没时间去搞清楚是什么。正好最近实在无聊地厉害，为了避免生命进一步浪费在如此阳光明媚狗熊撒欢的季节，就写了几行代码看看 Google 到底说了什么。</p>
<div class="syntax_hilite">
<div id="cpp-4">
<div class="cpp"><span style="color: #339900;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339900;">#include &lt;string.h&gt;</span><br />
<span style="color: #339900;">#include &lt;malloc.h&gt;</span><br />
<span style="color: #339900;">#include &lt;memory.h&gt;</span></p>
<p>
<span style="color: #0000ff;">char</span> strchar2char<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">char</span> buf<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #0000ff;">void</span> parse<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* src, <span style="color: #0000ff;">char</span>* dst<span style="color: #000000;">&#41;</span>;</p>
<p>
<span style="color: #0000ff;">int</span> main<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>* argv<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* src = <span style="color: #666666;">"01100110 01100101 01100101 01101100 01101001 01101110 01100111 00100000 01101100 01110101 01100011 01101011 01111001 00001010"</span>;<br />
&nbsp; <span style="color: #0000ff;">size_t</span> len_dst = <span style="color: #000000;">&#40;</span><span style="color: #0000dd;">strlen</span><span style="color: #000000;">&#40;</span>src<span style="color: #000000;">&#41;</span> + <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span> / <span style="color: #0000dd;">9</span>;</p>
<p>&nbsp; <span style="color: #0000ff;">char</span>* dst = <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">char</span>*<span style="color: #000000;">&#41;</span><span style="color: #0000dd;">malloc</span><span style="color: #000000;">&#40;</span>len_dst + <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">memset</span><span style="color: #000000;">&#40;</span>dst, <span style="color: #0000dd;">0</span>, len_dst + <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; parse<span style="color: #000000;">&#40;</span>src, dst<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"src: %s<span style="color: #666666; font-weight: bold;">\n</span>dst: %s<span style="color: #666666; font-weight: bold;">\n</span>"</span>, src, dst<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000dd;">free</span><span style="color: #000000;">&#40;</span>dst<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;<br />
<span style="color: #000000;">&#125;</span></p>
<p>
<span style="color: #0000ff;">char</span> strchar2char<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">char</span> buf<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> mask = 0x80, ret = <span style="color: #0000dd;">0</span>, i;</p>
<p>&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;<span style="color: #0000dd;">8</span>; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #666666;">'1'</span> == buf<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; ret |= mask;</p>
<p>&nbsp; &nbsp; mask&gt;&gt;= <span style="color: #0000dd;">1</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span></p>
<p>&nbsp; <span style="color: #0000ff;">return</span> ret;<br />
<span style="color: #000000;">&#125;</span></p>
<p>
<span style="color: #0000ff;">void</span> parse<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* src, <span style="color: #0000ff;">char</span>* dst<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">char</span> buf<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">8</span><span style="color: #000000;">&#93;</span>;<br />
&nbsp; <span style="color: #0000ff;">int</span> i;</p>
<p>&nbsp; <span style="color: #0000ff;">while</span> <span style="color: #000000;">&#40;</span>*src<span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #666666;">' '</span> == *src<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; src++;</p>
<p>&nbsp; &nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;<span style="color: #0000dd;">8</span>; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; buf<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = src<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>;</p>
<p>&nbsp; &nbsp; *dst = strchar2char<span style="color: #000000;">&#40;</span>buf<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; &nbsp; src += <span style="color: #0000dd;">8</span>;<br />
&nbsp; &nbsp; dst++;<br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div>
</div>
</div>
<p></p>
<p><span style="color:#fff">答案揭晓：feeling lucky</span></p>
<div class="entry-related"><h3>Related Posts</h3><ul><li><a href="http://hellobmw.com/archives/follow-me-on-twitter.html"  title="Follow me on Twitter">Follow me on Twitter</a></li><li><a href="http://hellobmw.com/archives/google-pagerank-checker-with-ajax.html"  title="Google PageRank Checker with Ajax">Google PageRank Checker with Ajax</a></li><li><a href="http://hellobmw.com/archives/google-pagerank-checker.html"  title="Google PageRank Checker">Google PageRank Checker</a></li><li><a href="http://hellobmw.com/archives/google-friend-connect.html"  title="本站也添加 Google Friend Connect">本站也添加 Google Friend Connect</a></li><li><a href="http://hellobmw.com/archives/recommended-google-chrome-cartoon-in-chinese.html"  title="[强推] Google Chrome 漫画书中文版">[强推] Google Chrome 漫画书中文版</a></li><li><a href="http://hellobmw.com/archives/google-pagerank-updated.html"  title="Google PageRank 貌似又更新了">Google PageRank 貌似又更新了</a></li></ul></div>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2009.｜<a href="http://hellobmw.com/archives/google-said-hi-on-twitter.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/google-said-hi-on-twitter.html#comments" >8 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>]]></content:encoded>
			<wfw:commentRss>http://hellobmw.com/archives/google-said-hi-on-twitter.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>DirectX Software Development Kit (November 2008) 下载</title>
		<link>http://hellobmw.com/archives/directx-software-development-kit-november-2008-download.html</link>
		<comments>http://hellobmw.com/archives/directx-software-development-kit-november-2008-download.html#comments</comments>
		<pubDate>Fri, 14 Nov 2008 06:13:31 +0000</pubDate>
		<dc:creator>北极冰仔</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Direct3D]]></category>
		<category><![CDATA[程序设计]]></category>
		<category><![CDATA[DirectX]]></category>
		<category><![CDATA[SDK]]></category>

		<guid isPermaLink="false">http://hellobmw.com/?p=1146</guid>
		<description><![CDATA[<h3>ORIGIN</h3>
<p><a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=5493F76A-6D37-478D-BA17-28B1CCA4865A&#038;displaylang=en"  class="external">http://www.microsoft.com/downloads/details.aspx?FamilyId=5493F76A-6D37-478D-BA17-28B1CCA4865A&#038;displaylang=en</a></p>
<h3>下载地址（直接用迅雷）</h3>
<p><a href="http://www.microsoft.com/downloads/info.aspx?na=90&#038;p=&#038;SrcDisplayLang=en&#038;SrcCategoryId=&#038;SrcFamilyId=5493f76a-6d37-478d-ba17-28b1cca4865a&#038;u=http%3a%2f%2fdownload.microsoft.com%2fdownload%2f5%2f8%2f2%2f58223f79-689d-47ae-bdd0-056116ee8d16%2fDXSDK_Nov08.exe"  class="external">DXSDK_Nov08.exe</a></p>
<h3>Brief Description</h3>
<p>November 2008<br />
Download the complete DirectX SDK, which contains the DirectX Runtime and all DirectX software required to create DirectX compliant applications.</p>
<h3>Quick Details</h3>
<p>File Name:	DXSDK_Nov08.exe<br />
Version:	9.25.1476<br />
Date Published:	11/7/2008<br />
Language:	English<br />
Download Size:	483.2 MB<br />
Estimated Download Time:	19 hr 39 min 56K<br />
Overview<br />
This DirectX SDK release contains updates to tools, utilities, samples, documentation, and runtime debug files for x64 and x86 platforms. </p>
<p>For additional information please see Microsoft DirectX Developer Center along with reviewing the Readme for last-minute updates.</p>
<h3>System Requirements</h3>
<ul>
<li>Supported Operating Systems: Windows Home Server; Windows Server 2003; Windows Server 2003 R2 (32-Bit x86); Windows Server 2003 R2 Datacenter Edition (32-Bit x86); Windows Server 2003 R2 Datacenter x64 Edition; Windows Server 2003 R2 Enterprise Edition (32-Bit x86); Windows Server 2003 R2 Enterprise x64 Edition; Windows Server 2003 R2 Standard Edition (32-bit x86); Windows Server 2003 R2 Standard x64 Edition ; Windows Server 2003 R2 x64 editions; Windows Server 2003 Service Pack 1; Windows Server 2003 Service Pack 2; Windows Server 2003 Service Pack 2 x64 Edition; Windows Server 2003 x64 editions; Windows Server 2003, Datacenter Edition (32-bit x86); Windows Server 2003, Datacenter x64 Edition; Windows Server 2003, Enterprise Edition (32-bit x86); Windows Server 2003, Enterprise x64 Edition; Windows Server 2003, Standard Edition (32-bit x86); Windows Server 2003, Standard x64 Edition; Windows Server 2003, Web Edition; Windows Server 2008; Windows Server 2008 Datacenter; Windows Server 2008 Datacenter without Hyper-V; Windows Server 2008 Enterprise; Windows Server 2008 Enterprise without Hyper-V; Windows Server 2008 Standard; Windows Server 2008 Standard without Hyper-V; Windows Small Business Server 2003 ; Windows Small Business Server 2008 Premium; Windows Small Business Server 2008 Standard ; Windows Vista; Windows Vista 64-bit Editions Service Pack 1; Windows Vista Business; Windows Vista Business 64-bit edition; Windows Vista Business N; Windows Vista Enterprise; Windows Vista Enterprise 64-bit edition; Windows Vista Home Basic; Windows Vista Home Basic 64-bit edition; Windows Vista Home Basic N; Windows Vista Home Premium; Windows Vista Home Premium 64-bit edition; Windows Vista Service Pack 1; Windows Vista Starter; Windows Vista Starter N; Windows Vista Ultimate; Windows Vista Ultimate 64-bit edition; Windows Web Server 2008; Windows XP; Windows XP 64-bit; Windows XP Home Edition ; Windows XP Home Edition N; Windows XP Media Center Edition; Windows XP Media Center Edition 2005 Update Rollup 2; Windows XP Professional Edition ; Windows XP Professional N; Windows XP Professional x64 Edition ; Windows XP Service Pack 1; Windows XP Service Pack 2; Windows XP Service Pack 3; Windows XP Starter Edition; Windows XP Tablet PC Edition</li>
</ul>
<h3>Additional Information</h3>
<p>This section contains the following:</p>
<p>What's New in the November 2008 DirectX SDK<br />
Known Issues with the November 2008 SDK </p>
<p>What's New in the November 2008 DirectX SDK</p>
<p>This version of the DirectX SDK contains the following new features, tools, and documentation.</p>
<p>PIX for Windows</p>
<p>PIX for Windows supports basic stream capture and single frame capture for Direct3D 11, however the new Direct3D 11 hardware features like tessellation, compute shader, dynamic shader linkage, and new resource formats, are not supported. In addition, advanced features like shader debugging and mesh viewing are not supported. These will be added in a future release. </p>
<p>XACT3 Now Supports the Same xWMA Bitrates Supported by XAudio2</p>
<p>XACT3 now supports the same xWMA bit rates that are supported by XAudio2. The exposed lower bit rates allow titles to take advantage of more aggressive compression.</p>
<p>Updated Audio Sample</p>
<p>The XAudio2 Sound3D sample has been updated to better demonstrate the capabilities of XAudio2 and X3DAudio as well as highlighting best practices.</p>
<p>Direct3D 11 Technical Preview</p>
<p>Included in the November 2008 DirectX SDK is a technical preview of Direct3D 11 and associated components and tools. Direct3D 11 is an update to Direct3D 10.1 enabling new hardware features as well as improving the breadth of configurations supported by Direct3D. As such, Direct3D 11 enables developers to create applications and games that work on Direct3D 10, Direct3D 10.1, and Direct3D 11 hardware when it becomes available. With the addition of WARP and Direct3D 10 Level 9, Direct3D 10.1 and Direct3D 11 have the ability to target fast software rasterization and Direct3D 9 hardware.</p>
<p>This version of the Direct3D 11 technical preview includes support for the following:</p>
<p>Tessellation<br />
Compute Shaders<br />
Multithreaded Rendering<br />
Dynamic Shader Linkage<br />
Windows Advanced Rasterizer (WARP)<br />
Direct3D 10 and Direct3D 11 on Direct3D 9 Hardware (D3D10 Level 9)<br />
Runtime Binaries<br />
D3DX11<br />
Completely Updated HLSL and Direct3D Compiler<br />
D3D11 Reference Rasterizer<br />
D3D11 SDK Layers<br />
In addition, there are four new samples that highlight tessellation, computer shaders, mulithreaded rendering, and dynamic shader linkage.</p>
<p>Tessellation</p>
<p>Direct3D 11 provides additional pipeline stages to support real-time tessellation of high order primitives. With extensively programmable capabilities, this feature allows many different methods for evaluating high-order surfaces, including subdivision surfaces using approximation techniques, Bezier patches, adaptive tessellation, and displacement mapping. This feature will only be available on Direct3D 11-class hardware, so in order to evaluate this feature you will need to use the Reference Rasterizer. For a demo of tessellation in action, check out the SubD11 sample available through the Sample Browser.</p>
<p>Compute Shader</p>
<p>The Compute Shader is an additional stage independent of the Direct3D 11 pipeline that enables general purpose computing on the GPU. In addition to all shader features provided by the unified shader core, the Compute Shader also supports scattered reads and writes to resources through Unordered Access Views, a shared memory pool within a group of executing threads, synchronization primitives, atomic operators, and many other advanced data-parallel features. A variant of the Direct3D 11 Compute Shader has been enabled in this release that can operate on Direct3D 10-class hardware. It is therefore possible to developing Compute Shaders on actual hardware, but an updated driver is required. The full functionality of the Direct3D 11 Compute Shader will is intended for support of Direct3D 11-class hardware, so in order to evaluate the full functionality you will need to use the Reference Rasterizer until such hardware is available. For a demo of the Compute Shader in action, check out the HDRToneMappingCS11 sample available through the Sample Browser.</p>
<p>Multithreaded Rendering</p>
<p>The key API difference from Direct3D 10 in Direct3D 11 is the addition of deferred contexts, which enables scalable execution of Direct3D commands distributed over multiple cores. A Deferred Context captures and assembles actions like state changes and draw submissions that can be executed on the actual device at a later time. By utilizing Deferred Contexts on multiple threads, an application can distribute the CPU overhead needed in the Direct3D11 runtime and the driver to multiple cores, enabling better use of an end-user's machine configuration. This feature is available for use on current Direct3D 10 hardware as well as the reference rasterizer. For a demonstration of API usage, check out the MultithreadedRendering11 sample available through the Sample Browser.</p>
<p>Dynamic Shader Linkage </p>
<p>In order to address the combinatorial explosion problem seen in specializing shaders for performance, Direct3D 11 introduces a limited form of runtime shader linkage that allows for near-optimal shader specialization during execution of an application. This is achieved by specifying the implementations of specific functions in shader code when the shader is assigned to the pipeline, allowing the driver to inline native shader instructions quickly rather than forcing the driver to recompile the intermediate language into native instructions with the new configuration. Shader development is exposed through the introduction of classes and interfaces to HLSL. For a demonstration, check out the Dynamic Shader Linkage 11 sample available through the Sample Browser.</p>
<p>Windows Advanced Rasterizer Platform (WARP)</p>
<p>Available in this SDK through Direct3D 11 and eventually also through Direct3D 10.1, WARP is a fast, multi-core scaling rasterizer that is fully Direct3D 10.1 compliant. Utilizing this technology is as simple as passing the D3D10_DRIVER_TYPE_WARP flag in your device creation.</p>
<p>Direct3D 10 and Direct3D 11 on Direct3D 9 Hardware (D3D10 Level 9)</p>
<p>Available in this SDK through Direct3D 11 and eventually also through Direct3D 10.1, the Direct3D API can target most Direct3D 9 hardware as well as Direct3D 10, Direct3D 10.1 and Direct3D 11 hardware. This is achieved by providing the Feature Level mechanism, which groups hardware into six categories depending on functionality: 9_1, 9_2, 9_3, 10_0, 10_1 and 11_0. A card only meets a feature level if it is fully compliant to that level, and each level is a strict super-set of those below it. Functionality is minimally emulated to assure no unexpected performance cliffs are encountered. Thus, features like Geometry Shaders are not available for Direct3D 9 level targets. </p>
<p>Runtime Binaries</p>
<p>All runtime binaries provided in the Direct3D 11 tech preview that will be available on Windows 7 and Windows Vista SP1 are installed with the SDK and are labeled as "Beta" components (i.e. D3D11_beta.DLL). All beta-labeled components are time-bombed. To create projects to evaluate these new components, you must link to their equivalent beta-labeled import libraries (i.e. D3D11_beta.lib). If you have a PDC copy of Windows 7, the headers, libs, and pdbs provided in the Windows SDK with the build are appropriate for development using the Direct3D 11 components providing in Windows 7. Please reserve the use of the headers, libs, and pdbs in this SDK to the beta components provided herein.</p>
<p>D3DX11</p>
<p>D3DX11 current only supports texture loading functions for Direct3D 11 resources. In the future, this component will provide many of the technologies available in D3DX10 like a mesh container format as well as the Effects 11 system. An important difference between D3DX11 and D3DX10 is that D3DX11 does not contain shader compilation functionality. Instead this is provided directly through the D3D Compiler component, described below.</p>
<p>HLSL and Direct3D Compiler</p>
<p>The HLSL compiler has several new features for supporting the new technologies available in Direct3D 11. This includes object oriented programming through interfaces and classes, a direct indexing syntax for resource loads, and the 'precise' keyword for ensuring that all operations performed with a specific variable adhere to the strict floating point rules. Almost all new linguistic features have valid functionality on existing shader targets. In addition to supporting all Direct3D 9, Direct3D 10, Direct3D 10.1, and Direct3D 11 shaders the HLSL compiler also supports the special targets needed to write shaders for Direct3D 10 Level 9 targets. The D3D Compiler is now directly accessible outside of D3DX10 and D3DX11 through D3DCompiler.H and D3DCompiler.lib. With these new files, an application is not required to link to D3DX in order to perform runtime compilation, and an application is not required to include the compiler if only D3DX functionality is needed.</p>
<p>D3D11 Reference Rasterizer</p>
<p>The Reference Rasterizer provides a gold-standard rasterization implementation for evaluation of Direct3D 11 features not yet available in hardware. The Reference Rasterizer is also provided as a way to verify a specific hardware implementation's accuracy to the rasterization standard. The reference rasterizer is designed for correctness, not performance. To create a reference device, simply pass the D3D_DRIVER_TYPE_REFERENCE flag at device creation. </p>
<p>D3D11 SDK Layers</p>
<p>Direct3D11 SDK Layers provide a mechanism for tracking the operation of the Direct3D 11 runtime during development. Currently this is used for providing useful debug information, which not only includes errors on improper use but also warnings that recommend best practice use of the runtime and often provides in-depth, useful information for debugging. It is highly recommended that the debug output from D3D11 SDK Layers is turned on at all times during development and an application generates no debug output during execution before it is released or used with PIX for Windows for profiling. Enabling the debug layer is as simple as passing the D3D11_CREATE_DEVICE_DEBUG flag at device creation time. Developers are strongly encouraged to use layers in debug builds. Layers are not recommended for use in profile or release builds.</p>
<p>New Samples Demonstrating D3D11</p>
<p>This release has four new samples that demonstrate D3D11.</p>
<p>The Dynamic Shader Linkage 11 sample demonstrates use of Shader Model 5 shader interfaces and Direct3D 11 support for linking shader interface methods at runtime.<br />
The HDRToneMappingCS11 sample demonstrates how to setup and run the Compute Shader(CS for short later on), which is one of the most exciting new features of Direct3D 11. Although the sample only utilizes the CS to implement HDR tone-mapping, the concept should extend easily to other post-processing algorithms as well as more general calculations.<br />
The MultithreadedRendering11 sample demonstrates how to split rendering among multiple threads, with very low overhead.<br />
The new SubD11 sample is very similar to the SubD10 sample in the DirectX SDK, except that it has been enhanced to take advantage of three new Direct3D 11 pipeline stages: the hull shader, the tessellator, and the domain shader. </p>
<p>Known Issues with the November 2008 SDK</p>
<p>Building Samples Using the Visual Studio 2005 Project Files Results In Harmless Warning</p>
<p>Developers who build the samples may see the following harmless warning:</p>
<p>manifest authoring warning 81010002: Unrecognized Element "requestedPrivileges" in namespace "urn:schemas-microsoft-com:asm.v2".</p>
<p>This is a known issue with the manifest tool (mt.exe) that ships in Visual Studio 2005 SP1. In order to remove this warning, developers should update their versions of mt.exe to the version that ships in the Windows SDK. Developers should update the three instances of mt.exe that ship in Visual Studio 2005 SP1:</p>
<p>VC\bin\<br />
Common7\tools\bin\<br />
SDK\v2.0\bin\<br />
Reflect and DisassembleXXXXX Moved from D3DX10_40.dll to D3DCompiler_40.dll</p>
<p>In the November 2008 DirectX SDK three functions were moved from D3DX10_40.dll to D3DCompiler_40.dll:</p>
<p>D3DX10ReflectShader (renamed D3DReflect)<br />
D3DX10DisassembleShader (renamed D3DDisassemble)<br />
D3DX10DisassembleEffect (renamed D3DDisassemble10Effect)<br />
As part of this change, these three functions are now defined in d3dcompiler.h.</p>
<p>These three new functions use D3D11 interfaces that are fully compatible with D3D10 and D3D10.1 runtimes without taking dependencies that are currently in pre-release state.</p>
<p>Don't Mix Beta DLLs With Their Correspondingly Named Non-Beta DLLs</p>
<p>The Direct3D 11 Technical Preview in the November 2008 DirectX SDK provides a dxgi_beta.dll that is different from the dxgi.dll included in Windows Vista. Developers should not use interfaces from one of these DLLs with the other as it may cause issues that are difficult to track down. The recommendation is that if you are using a beta DLL, don't link to the correspondingly named non-beta DLL.</p>
<p>ID3DXSPMesh Interface Is Deprecated</p>
<p>The ID3DXSPMesh interface is deprecated and will be removed in a future release of the DirectX SDK. Developers should investigate using Direct3D 11 tessellation as an altertnative to the ID3DXSPMesh interface.</p>
<p>D3DXGatherFragments Is Deprecated</p>
<p>The D3DXGatherFragments function is deprecated and will be removed in a future release of the DirectX SDK. Developers should investigate using Direct3D 11 dynamic shaders.</p>
<p>IXACT3Engine::PrepareWave Doesn't Support xWMA</p>
<p>In the March 2008 and later SDKs, the IXACT3Engine::PrepareWave doesn't support xWMA. Titles wishing to use xWMA in XACT can prepare those waves using one of the following methods:</p>
<p>IXACT3WaveBank::Prepare<br />
IXACT3SoundBank::Prepare<br />
IXACT3Engine::PrepareInMemoryWave<br />
IXACT3Engine::PrepareStreamingWave<br />
xWMA Compression Doesn't Support AIFF Sources</p>
<p>In the March 2008 and later SDKs, content creators must use WAV files if they want to use xWMA compression. The AIFF file format can be used for ADPCM and XMA compression. </p>
<p>XAudio2: Queries about Volume, Pitch, Frequency Ratio, and Filter Parameters May Not Return Currently Set Values</p>
<p>In the August 2007 and later SDKs, changing state (such as the volume, pitch, frequency ratio, filter parameters, and others) is deferred. The following methods will all return the most recently set value, even if that value hasn't yet been applied to the sounds.</p>
<p>IXAudio2Voice::GetChannelVolumes<br />
IXAudio2Voice::GetOutputMatrix<br />
IXAudio2Voice::GetVolume<br />
IXAudio2SourceVoice::GetFilterParameters<br />
IXAudio2SourceVoice::GetFrequencyRatio<br />
XACT Fails Silently When Saving to Protected Directories on Windows Vista</p>
<p>On Windows Vista, XACT fails silently when a user tries to save XACT project files to a protected directory such as "Program Files"; to the user, it will look like the save was successful. </p>
<p>XACT Platform-Specific Compression Restrictions</p>
<p>In XACT, users should ensure that compression presets are valid for a given wave on all platforms; if compression fails, the wave reverts to the use of a no compression preset, and, therefore, is uncompressed on all platforms. For Windows exclusive titles, ensure that the Xbox 360 format setting is PCM to avoid this issue. On Xbox 360, ensure that all XMA-compressed waves are 16-bit. On Windows and Xbox 360, ensure that all xWMA-compressed waves use xWMA's supported sampling rates. </p>
<p>Symbols for DirectX</p>
<p>The latest and most complete source for debugging symbols is the Microsoft Symbol Server. We recommend using the symbol server rather than using the included partial symbols package. For instructions about using the Microsoft Symbol Server, see http://www.microsoft.com/whdc/devtools/debugging/debugstart.mspx.</p>
<p>Installation Notes for All Platforms</p>
<p>This SDK installs on Windows XP, Windows Server® 2003, Windows Server 2008, and Windows Vista using Visual Studio .NET 2005 or 2008.<br />
Before installing the current DirectX SDK, remove previous versions of the DirectX SDK.<br />
Some samples require that you install the latest Microsoft Windows SDK (Platform SDK) on your system.<br />
If you are using Visual Studio and run the Platform SDK Integration tool, all custom include and library paths will be erased, including those added by the DirectX SDK. To fix the issue, developers will need to either reinstall the DirectX SDK or manually add the paths to Visual Studio.<br />
If you encounter compilation issues related to the DirectX headers, make sure that the include directories in Visual Studio are set correctly. On the Win32 platform, make sure that there is a reference to the DirectX headers. "$(DXSDK_DIR)include" should appear in the include directory "$(VCInstallDir)Include."<br />
Several virus protection software applications interfere with SDK installation. They may require you to disable virus protection software until SDK installation is completed.<br />
If you encounter the error message "A cabinet file is necessary for installation and cannot be trusted" during installation, your system may be corrupted, or cryptographic services may be disabled. To resolve the problem, try the following:</p>
<p>Enable cryptographic services. On the Start menu, right-click My Computer, and then click Manage. The Computer Management window appears. In the left navigation pane, click Services and Applications. In the right pane, double click Services, and then double-click Cryptographic Services. The Cryptographic Services Properties property sheet appears. On the General tab, ensure that Service Status is Started, and that Startup Type is Automatic.<br />
If you are running Windows on a FAT32 drive, run scandisk.<br />
Try the resolution steps in Detecting digital signing issues in Windows XP.<br />
Try the resolution steps in You cannot install some updates or programs.<br />
Remove temporary files in %temp%. If you are installing end-user runtime, also remove "%windir%\system32\directx\websetup Error."<br />
External firewall programs may indicate that the file InstallDeveloperFiles.exe wants to access the Internet. This is by design, and should be enabled.</p>
<div class="entry-related"><h3>Related Posts</h3><ul></li>No Related Posts</li></ul>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2008.｜<a href="http://hellobmw.com/archives/directx-software-development-kit-november-2008-download.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/directx-software-development-kit-november-2008-download.html#comments" >5 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>
]]></description>
			<content:encoded><![CDATA[<h3>ORIGIN</h3>
<p><a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=5493F76A-6D37-478D-BA17-28B1CCA4865A&#038;displaylang=en"  class="external">http://www.microsoft.com/downloads/details.aspx?FamilyId=5493F76A-6D37-478D-BA17-28B1CCA4865A&#038;displaylang=en</a></p>
<h3>下载地址（直接用迅雷）</h3>
<p><a href="http://www.microsoft.com/downloads/info.aspx?na=90&#038;p=&#038;SrcDisplayLang=en&#038;SrcCategoryId=&#038;SrcFamilyId=5493f76a-6d37-478d-ba17-28b1cca4865a&#038;u=http%3a%2f%2fdownload.microsoft.com%2fdownload%2f5%2f8%2f2%2f58223f79-689d-47ae-bdd0-056116ee8d16%2fDXSDK_Nov08.exe"  class="external">DXSDK_Nov08.exe</a></p>
<h3>Brief Description</h3>
<p>November 2008<br />
Download the complete DirectX SDK, which contains the DirectX Runtime and all DirectX software required to create DirectX compliant applications.</p>
<h3>Quick Details</h3>
<p>File Name:	DXSDK_Nov08.exe<br />
Version:	9.25.1476<br />
Date Published:	11/7/2008<br />
Language:	English<br />
Download Size:	483.2 MB<br />
Estimated Download Time:	19 hr 39 min 56K<br />
Overview<br />
This DirectX SDK release contains updates to tools, utilities, samples, documentation, and runtime debug files for x64 and x86 platforms. </p>
<p>For additional information please see Microsoft DirectX Developer Center along with reviewing the Readme for last-minute updates.</p>
<h3>System Requirements</h3>
<ul>
<li>Supported Operating Systems: Windows Home Server; Windows Server 2003; Windows Server 2003 R2 (32-Bit x86); Windows Server 2003 R2 Datacenter Edition (32-Bit x86); Windows Server 2003 R2 Datacenter x64 Edition; Windows Server 2003 R2 Enterprise Edition (32-Bit x86); Windows Server 2003 R2 Enterprise x64 Edition; Windows Server 2003 R2 Standard Edition (32-bit x86); Windows Server 2003 R2 Standard x64 Edition ; Windows Server 2003 R2 x64 editions; Windows Server 2003 Service Pack 1; Windows Server 2003 Service Pack 2; Windows Server 2003 Service Pack 2 x64 Edition; Windows Server 2003 x64 editions; Windows Server 2003, Datacenter Edition (32-bit x86); Windows Server 2003, Datacenter x64 Edition; Windows Server 2003, Enterprise Edition (32-bit x86); Windows Server 2003, Enterprise x64 Edition; Windows Server 2003, Standard Edition (32-bit x86); Windows Server 2003, Standard x64 Edition; Windows Server 2003, Web Edition; Windows Server 2008; Windows Server 2008 Datacenter; Windows Server 2008 Datacenter without Hyper-V; Windows Server 2008 Enterprise; Windows Server 2008 Enterprise without Hyper-V; Windows Server 2008 Standard; Windows Server 2008 Standard without Hyper-V; Windows Small Business Server 2003 ; Windows Small Business Server 2008 Premium; Windows Small Business Server 2008 Standard ; Windows Vista; Windows Vista 64-bit Editions Service Pack 1; Windows Vista Business; Windows Vista Business 64-bit edition; Windows Vista Business N; Windows Vista Enterprise; Windows Vista Enterprise 64-bit edition; Windows Vista Home Basic; Windows Vista Home Basic 64-bit edition; Windows Vista Home Basic N; Windows Vista Home Premium; Windows Vista Home Premium 64-bit edition; Windows Vista Service Pack 1; Windows Vista Starter; Windows Vista Starter N; Windows Vista Ultimate; Windows Vista Ultimate 64-bit edition; Windows Web Server 2008; Windows XP; Windows XP 64-bit; Windows XP Home Edition ; Windows XP Home Edition N; Windows XP Media Center Edition; Windows XP Media Center Edition 2005 Update Rollup 2; Windows XP Professional Edition ; Windows XP Professional N; Windows XP Professional x64 Edition ; Windows XP Service Pack 1; Windows XP Service Pack 2; Windows XP Service Pack 3; Windows XP Starter Edition; Windows XP Tablet PC Edition</li>
</ul>
<h3>Additional Information</h3>
<p>This section contains the following:</p>
<p>What's New in the November 2008 DirectX SDK<br />
Known Issues with the November 2008 SDK </p>
<p>What's New in the November 2008 DirectX SDK</p>
<p>This version of the DirectX SDK contains the following new features, tools, and documentation.</p>
<p>PIX for Windows</p>
<p>PIX for Windows supports basic stream capture and single frame capture for Direct3D 11, however the new Direct3D 11 hardware features like tessellation, compute shader, dynamic shader linkage, and new resource formats, are not supported. In addition, advanced features like shader debugging and mesh viewing are not supported. These will be added in a future release. </p>
<p>XACT3 Now Supports the Same xWMA Bitrates Supported by XAudio2</p>
<p>XACT3 now supports the same xWMA bit rates that are supported by XAudio2. The exposed lower bit rates allow titles to take advantage of more aggressive compression.</p>
<p>Updated Audio Sample</p>
<p>The XAudio2 Sound3D sample has been updated to better demonstrate the capabilities of XAudio2 and X3DAudio as well as highlighting best practices.</p>
<p>Direct3D 11 Technical Preview</p>
<p>Included in the November 2008 DirectX SDK is a technical preview of Direct3D 11 and associated components and tools. Direct3D 11 is an update to Direct3D 10.1 enabling new hardware features as well as improving the breadth of configurations supported by Direct3D. As such, Direct3D 11 enables developers to create applications and games that work on Direct3D 10, Direct3D 10.1, and Direct3D 11 hardware when it becomes available. With the addition of WARP and Direct3D 10 Level 9, Direct3D 10.1 and Direct3D 11 have the ability to target fast software rasterization and Direct3D 9 hardware.</p>
<p>This version of the Direct3D 11 technical preview includes support for the following:</p>
<p>Tessellation<br />
Compute Shaders<br />
Multithreaded Rendering<br />
Dynamic Shader Linkage<br />
Windows Advanced Rasterizer (WARP)<br />
Direct3D 10 and Direct3D 11 on Direct3D 9 Hardware (D3D10 Level 9)<br />
Runtime Binaries<br />
D3DX11<br />
Completely Updated HLSL and Direct3D Compiler<br />
D3D11 Reference Rasterizer<br />
D3D11 SDK Layers<br />
In addition, there are four new samples that highlight tessellation, computer shaders, mulithreaded rendering, and dynamic shader linkage.</p>
<p>Tessellation</p>
<p>Direct3D 11 provides additional pipeline stages to support real-time tessellation of high order primitives. With extensively programmable capabilities, this feature allows many different methods for evaluating high-order surfaces, including subdivision surfaces using approximation techniques, Bezier patches, adaptive tessellation, and displacement mapping. This feature will only be available on Direct3D 11-class hardware, so in order to evaluate this feature you will need to use the Reference Rasterizer. For a demo of tessellation in action, check out the SubD11 sample available through the Sample Browser.</p>
<p>Compute Shader</p>
<p>The Compute Shader is an additional stage independent of the Direct3D 11 pipeline that enables general purpose computing on the GPU. In addition to all shader features provided by the unified shader core, the Compute Shader also supports scattered reads and writes to resources through Unordered Access Views, a shared memory pool within a group of executing threads, synchronization primitives, atomic operators, and many other advanced data-parallel features. A variant of the Direct3D 11 Compute Shader has been enabled in this release that can operate on Direct3D 10-class hardware. It is therefore possible to developing Compute Shaders on actual hardware, but an updated driver is required. The full functionality of the Direct3D 11 Compute Shader will is intended for support of Direct3D 11-class hardware, so in order to evaluate the full functionality you will need to use the Reference Rasterizer until such hardware is available. For a demo of the Compute Shader in action, check out the HDRToneMappingCS11 sample available through the Sample Browser.</p>
<p>Multithreaded Rendering</p>
<p>The key API difference from Direct3D 10 in Direct3D 11 is the addition of deferred contexts, which enables scalable execution of Direct3D commands distributed over multiple cores. A Deferred Context captures and assembles actions like state changes and draw submissions that can be executed on the actual device at a later time. By utilizing Deferred Contexts on multiple threads, an application can distribute the CPU overhead needed in the Direct3D11 runtime and the driver to multiple cores, enabling better use of an end-user's machine configuration. This feature is available for use on current Direct3D 10 hardware as well as the reference rasterizer. For a demonstration of API usage, check out the MultithreadedRendering11 sample available through the Sample Browser.</p>
<p>Dynamic Shader Linkage </p>
<p>In order to address the combinatorial explosion problem seen in specializing shaders for performance, Direct3D 11 introduces a limited form of runtime shader linkage that allows for near-optimal shader specialization during execution of an application. This is achieved by specifying the implementations of specific functions in shader code when the shader is assigned to the pipeline, allowing the driver to inline native shader instructions quickly rather than forcing the driver to recompile the intermediate language into native instructions with the new configuration. Shader development is exposed through the introduction of classes and interfaces to HLSL. For a demonstration, check out the Dynamic Shader Linkage 11 sample available through the Sample Browser.</p>
<p>Windows Advanced Rasterizer Platform (WARP)</p>
<p>Available in this SDK through Direct3D 11 and eventually also through Direct3D 10.1, WARP is a fast, multi-core scaling rasterizer that is fully Direct3D 10.1 compliant. Utilizing this technology is as simple as passing the D3D10_DRIVER_TYPE_WARP flag in your device creation.</p>
<p>Direct3D 10 and Direct3D 11 on Direct3D 9 Hardware (D3D10 Level 9)</p>
<p>Available in this SDK through Direct3D 11 and eventually also through Direct3D 10.1, the Direct3D API can target most Direct3D 9 hardware as well as Direct3D 10, Direct3D 10.1 and Direct3D 11 hardware. This is achieved by providing the Feature Level mechanism, which groups hardware into six categories depending on functionality: 9_1, 9_2, 9_3, 10_0, 10_1 and 11_0. A card only meets a feature level if it is fully compliant to that level, and each level is a strict super-set of those below it. Functionality is minimally emulated to assure no unexpected performance cliffs are encountered. Thus, features like Geometry Shaders are not available for Direct3D 9 level targets. </p>
<p>Runtime Binaries</p>
<p>All runtime binaries provided in the Direct3D 11 tech preview that will be available on Windows 7 and Windows Vista SP1 are installed with the SDK and are labeled as "Beta" components (i.e. D3D11_beta.DLL). All beta-labeled components are time-bombed. To create projects to evaluate these new components, you must link to their equivalent beta-labeled import libraries (i.e. D3D11_beta.lib). If you have a PDC copy of Windows 7, the headers, libs, and pdbs provided in the Windows SDK with the build are appropriate for development using the Direct3D 11 components providing in Windows 7. Please reserve the use of the headers, libs, and pdbs in this SDK to the beta components provided herein.</p>
<p>D3DX11</p>
<p>D3DX11 current only supports texture loading functions for Direct3D 11 resources. In the future, this component will provide many of the technologies available in D3DX10 like a mesh container format as well as the Effects 11 system. An important difference between D3DX11 and D3DX10 is that D3DX11 does not contain shader compilation functionality. Instead this is provided directly through the D3D Compiler component, described below.</p>
<p>HLSL and Direct3D Compiler</p>
<p>The HLSL compiler has several new features for supporting the new technologies available in Direct3D 11. This includes object oriented programming through interfaces and classes, a direct indexing syntax for resource loads, and the 'precise' keyword for ensuring that all operations performed with a specific variable adhere to the strict floating point rules. Almost all new linguistic features have valid functionality on existing shader targets. In addition to supporting all Direct3D 9, Direct3D 10, Direct3D 10.1, and Direct3D 11 shaders the HLSL compiler also supports the special targets needed to write shaders for Direct3D 10 Level 9 targets. The D3D Compiler is now directly accessible outside of D3DX10 and D3DX11 through D3DCompiler.H and D3DCompiler.lib. With these new files, an application is not required to link to D3DX in order to perform runtime compilation, and an application is not required to include the compiler if only D3DX functionality is needed.</p>
<p>D3D11 Reference Rasterizer</p>
<p>The Reference Rasterizer provides a gold-standard rasterization implementation for evaluation of Direct3D 11 features not yet available in hardware. The Reference Rasterizer is also provided as a way to verify a specific hardware implementation's accuracy to the rasterization standard. The reference rasterizer is designed for correctness, not performance. To create a reference device, simply pass the D3D_DRIVER_TYPE_REFERENCE flag at device creation. </p>
<p>D3D11 SDK Layers</p>
<p>Direct3D11 SDK Layers provide a mechanism for tracking the operation of the Direct3D 11 runtime during development. Currently this is used for providing useful debug information, which not only includes errors on improper use but also warnings that recommend best practice use of the runtime and often provides in-depth, useful information for debugging. It is highly recommended that the debug output from D3D11 SDK Layers is turned on at all times during development and an application generates no debug output during execution before it is released or used with PIX for Windows for profiling. Enabling the debug layer is as simple as passing the D3D11_CREATE_DEVICE_DEBUG flag at device creation time. Developers are strongly encouraged to use layers in debug builds. Layers are not recommended for use in profile or release builds.</p>
<p>New Samples Demonstrating D3D11</p>
<p>This release has four new samples that demonstrate D3D11.</p>
<p>The Dynamic Shader Linkage 11 sample demonstrates use of Shader Model 5 shader interfaces and Direct3D 11 support for linking shader interface methods at runtime.<br />
The HDRToneMappingCS11 sample demonstrates how to setup and run the Compute Shader(CS for short later on), which is one of the most exciting new features of Direct3D 11. Although the sample only utilizes the CS to implement HDR tone-mapping, the concept should extend easily to other post-processing algorithms as well as more general calculations.<br />
The MultithreadedRendering11 sample demonstrates how to split rendering among multiple threads, with very low overhead.<br />
The new SubD11 sample is very similar to the SubD10 sample in the DirectX SDK, except that it has been enhanced to take advantage of three new Direct3D 11 pipeline stages: the hull shader, the tessellator, and the domain shader. </p>
<p>Known Issues with the November 2008 SDK</p>
<p>Building Samples Using the Visual Studio 2005 Project Files Results In Harmless Warning</p>
<p>Developers who build the samples may see the following harmless warning:</p>
<p>manifest authoring warning 81010002: Unrecognized Element "requestedPrivileges" in namespace "urn:schemas-microsoft-com:asm.v2".</p>
<p>This is a known issue with the manifest tool (mt.exe) that ships in Visual Studio 2005 SP1. In order to remove this warning, developers should update their versions of mt.exe to the version that ships in the Windows SDK. Developers should update the three instances of mt.exe that ship in Visual Studio 2005 SP1:</p>
<p>VC\bin\<br />
Common7\tools\bin\<br />
SDK\v2.0\bin\<br />
Reflect and DisassembleXXXXX Moved from D3DX10_40.dll to D3DCompiler_40.dll</p>
<p>In the November 2008 DirectX SDK three functions were moved from D3DX10_40.dll to D3DCompiler_40.dll:</p>
<p>D3DX10ReflectShader (renamed D3DReflect)<br />
D3DX10DisassembleShader (renamed D3DDisassemble)<br />
D3DX10DisassembleEffect (renamed D3DDisassemble10Effect)<br />
As part of this change, these three functions are now defined in d3dcompiler.h.</p>
<p>These three new functions use D3D11 interfaces that are fully compatible with D3D10 and D3D10.1 runtimes without taking dependencies that are currently in pre-release state.</p>
<p>Don't Mix Beta DLLs With Their Correspondingly Named Non-Beta DLLs</p>
<p>The Direct3D 11 Technical Preview in the November 2008 DirectX SDK provides a dxgi_beta.dll that is different from the dxgi.dll included in Windows Vista. Developers should not use interfaces from one of these DLLs with the other as it may cause issues that are difficult to track down. The recommendation is that if you are using a beta DLL, don't link to the correspondingly named non-beta DLL.</p>
<p>ID3DXSPMesh Interface Is Deprecated</p>
<p>The ID3DXSPMesh interface is deprecated and will be removed in a future release of the DirectX SDK. Developers should investigate using Direct3D 11 tessellation as an altertnative to the ID3DXSPMesh interface.</p>
<p>D3DXGatherFragments Is Deprecated</p>
<p>The D3DXGatherFragments function is deprecated and will be removed in a future release of the DirectX SDK. Developers should investigate using Direct3D 11 dynamic shaders.</p>
<p>IXACT3Engine::PrepareWave Doesn't Support xWMA</p>
<p>In the March 2008 and later SDKs, the IXACT3Engine::PrepareWave doesn't support xWMA. Titles wishing to use xWMA in XACT can prepare those waves using one of the following methods:</p>
<p>IXACT3WaveBank::Prepare<br />
IXACT3SoundBank::Prepare<br />
IXACT3Engine::PrepareInMemoryWave<br />
IXACT3Engine::PrepareStreamingWave<br />
xWMA Compression Doesn't Support AIFF Sources</p>
<p>In the March 2008 and later SDKs, content creators must use WAV files if they want to use xWMA compression. The AIFF file format can be used for ADPCM and XMA compression. </p>
<p>XAudio2: Queries about Volume, Pitch, Frequency Ratio, and Filter Parameters May Not Return Currently Set Values</p>
<p>In the August 2007 and later SDKs, changing state (such as the volume, pitch, frequency ratio, filter parameters, and others) is deferred. The following methods will all return the most recently set value, even if that value hasn't yet been applied to the sounds.</p>
<p>IXAudio2Voice::GetChannelVolumes<br />
IXAudio2Voice::GetOutputMatrix<br />
IXAudio2Voice::GetVolume<br />
IXAudio2SourceVoice::GetFilterParameters<br />
IXAudio2SourceVoice::GetFrequencyRatio<br />
XACT Fails Silently When Saving to Protected Directories on Windows Vista</p>
<p>On Windows Vista, XACT fails silently when a user tries to save XACT project files to a protected directory such as "Program Files"; to the user, it will look like the save was successful. </p>
<p>XACT Platform-Specific Compression Restrictions</p>
<p>In XACT, users should ensure that compression presets are valid for a given wave on all platforms; if compression fails, the wave reverts to the use of a no compression preset, and, therefore, is uncompressed on all platforms. For Windows exclusive titles, ensure that the Xbox 360 format setting is PCM to avoid this issue. On Xbox 360, ensure that all XMA-compressed waves are 16-bit. On Windows and Xbox 360, ensure that all xWMA-compressed waves use xWMA's supported sampling rates. </p>
<p>Symbols for DirectX</p>
<p>The latest and most complete source for debugging symbols is the Microsoft Symbol Server. We recommend using the symbol server rather than using the included partial symbols package. For instructions about using the Microsoft Symbol Server, see http://www.microsoft.com/whdc/devtools/debugging/debugstart.mspx.</p>
<p>Installation Notes for All Platforms</p>
<p>This SDK installs on Windows XP, Windows Server® 2003, Windows Server 2008, and Windows Vista using Visual Studio .NET 2005 or 2008.<br />
Before installing the current DirectX SDK, remove previous versions of the DirectX SDK.<br />
Some samples require that you install the latest Microsoft Windows SDK (Platform SDK) on your system.<br />
If you are using Visual Studio and run the Platform SDK Integration tool, all custom include and library paths will be erased, including those added by the DirectX SDK. To fix the issue, developers will need to either reinstall the DirectX SDK or manually add the paths to Visual Studio.<br />
If you encounter compilation issues related to the DirectX headers, make sure that the include directories in Visual Studio are set correctly. On the Win32 platform, make sure that there is a reference to the DirectX headers. "$(DXSDK_DIR)include" should appear in the include directory "$(VCInstallDir)Include."<br />
Several virus protection software applications interfere with SDK installation. They may require you to disable virus protection software until SDK installation is completed.<br />
If you encounter the error message "A cabinet file is necessary for installation and cannot be trusted" during installation, your system may be corrupted, or cryptographic services may be disabled. To resolve the problem, try the following:</p>
<p>Enable cryptographic services. On the Start menu, right-click My Computer, and then click Manage. The Computer Management window appears. In the left navigation pane, click Services and Applications. In the right pane, double click Services, and then double-click Cryptographic Services. The Cryptographic Services Properties property sheet appears. On the General tab, ensure that Service Status is Started, and that Startup Type is Automatic.<br />
If you are running Windows on a FAT32 drive, run scandisk.<br />
Try the resolution steps in Detecting digital signing issues in Windows XP.<br />
Try the resolution steps in You cannot install some updates or programs.<br />
Remove temporary files in %temp%. If you are installing end-user runtime, also remove "%windir%\system32\directx\websetup Error."<br />
External firewall programs may indicate that the file InstallDeveloperFiles.exe wants to access the Internet. This is by design, and should be enabled.</p>
<div class="entry-related"><h3>Related Posts</h3><ul></li>No Related Posts</li></ul>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2008.｜<a href="http://hellobmw.com/archives/directx-software-development-kit-november-2008-download.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/directx-software-development-kit-november-2008-download.html#comments" >5 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>]]></content:encoded>
			<wfw:commentRss>http://hellobmw.com/archives/directx-software-development-kit-november-2008-download.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>[算法] 动态规划：最长公共子串 LCS</title>
		<link>http://hellobmw.com/archives/dynamic-programming-longest-common-substring.html</link>
		<comments>http://hellobmw.com/archives/dynamic-programming-longest-common-substring.html#comments</comments>
		<pubDate>Tue, 28 Oct 2008 14:43:20 +0000</pubDate>
		<dc:creator>北极冰仔</dc:creator>
				<category><![CDATA[Algorithmics]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[程序设计]]></category>
		<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[Longest Common Substring]]></category>
		<category><![CDATA[动态规划]]></category>
		<category><![CDATA[最长公共子串]]></category>

		<guid isPermaLink="false">http://hellobmw.com/?p=1141</guid>
		<description><![CDATA[<p>我的算法学得好差，尤其是动态规划这一块，在这里吃了不少亏。</p>
<p>这次写的跟面试题有关。老早就知道最长公共子串（<a href="http://en.wikipedia.org/wiki/Longest_common_substring_problem"  class="external">Longest Common Substring</a>）这个问题，但从没认真搞过，这些天又过着伪充实的繁忙生活，直到今天才终于有点时间和心情（很重要）学习学习了。</p>
<p>原理啥的我还是不讲了，听我讲不如点击上面的链接自行阅读维基。下面我还是贴贴代码吧，写这点东西还是花了点时间的。</p>
<p>For the example strings "ABAB" and "BABA":</p>
<table class="wikitable" style="text-align:center">
<tr>
<th width="15"></th>
<th width="15"></th>
<th width="15">A</th>
<th width="15">B</th>
<th width="15">A</th>
<th width="15">B</th>
</tr>
<tr>
<td></td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>B</th>
<td>0</td>
<td>0</td>
<td style="color:red">1</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<th>A</th>
<td>0</td>
<td style="color:red">1</td>
<td>0</td>
<td style="color:red">2</td>
<td>0</td>
</tr>
<tr>
<th>B</th>
<td>0</td>
<td>0</td>
<td style="color:red">2</td>
<td>0</td>
<td style="color:red">3</td>
</tr>
<tr>
<th>A</th>
<td>0</td>
<td>1</td>
<td>0</td>
<td style="color:red">3</td>
<td>0</td>
</tr>
</table>
<p><em>貌似这里应该配个图表</em></p>
<div class="syntax_hilite">
<div id="cpp-5">
<div class="cpp"><span style="color: #ff0000; font-style: italic;">/************************************************************************<br />
The following tricks can be used to reduce the memory usage of an implementation:<br />
1) Keep only the last and current row of the DP table to save memory (O(min(m,n))<br />
&nbsp; &nbsp;instead of O(mn)).<br />
2) Store only non-zero values in the rows. You can do this by using hash tables<br />
&nbsp; &nbsp;instead of arrays. This is useful for large alphabets.<br />
&nbsp; &nbsp;[http://en.wikipedia.org/wiki/Longest_common_substring_problem]<br />
/************************************************************************/</span></p>
<p><span style="color: #339900;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339900;">#include &lt;string.h&gt;</span><br />
<span style="color: #339900;">#include &lt;malloc.h&gt;</span><br />
<span style="color: #339900;">#include &lt;memory.h&gt;</span><br />
<span style="color: #339900;">#include &lt;assert.h&gt;</span></p>
<p><span style="color: #339900;">#define GET_MAX(a, b) ((a)&gt; (b) ? (a) : (b))</span><br />
<span style="color: #339900;">#define GET_MIN(a, b) ((a) &lt;(b) ? (a) : (b))</span></p>
<p><span style="color: #339900;">#define MAX_LEN 256</span></p>
<p><span style="color: #339900;">#define OUTPUT</span></p>
<p><span style="color: #ff0000; font-style: italic;">/* must free() return value manually */</span><br />
<span style="color: #ff0000; font-style: italic;">/* 时间复杂度为O(mn) */</span></p>
<p><span style="color: #ff0000; font-style: italic;">/* 空间复杂度为O(mn) */</span><br />
<span style="color: #0000ff;">char</span>* longest_common_substring<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* lhs, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* rhs<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">size_t</span> llen, rlen, **lens, maxlen = <span style="color: #0000dd;">0</span>, flag = <span style="color: #0000dd;">0</span>;<br />
&nbsp; <span style="color: #0000ff;">size_t</span> i, j;<br />
&nbsp; <span style="color: #0000ff;">char</span>* buf;</p>
<p>&nbsp; <span style="color: #0000dd;">assert</span><span style="color: #000000;">&#40;</span>lhs &amp;&amp; rhs<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; llen = <span style="color: #0000dd;">strlen</span><span style="color: #000000;">&#40;</span>lhs<span style="color: #000000;">&#41;</span>;<br />
&nbsp; rlen = <span style="color: #0000dd;">strlen</span><span style="color: #000000;">&#40;</span>rhs<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; lens = <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span>**<span style="color: #000000;">&#41;</span><span style="color: #0000dd;">malloc</span><span style="color: #000000;">&#40;</span>llen * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span>*<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">memset</span><span style="color: #000000;">&#40;</span>lens, <span style="color: #0000dd;">0</span>, llen * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span>*<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;llen; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span>*<span style="color: #000000;">&#41;</span><span style="color: #0000dd;">malloc</span><span style="color: #000000;">&#40;</span>rlen * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000dd;">memset</span><span style="color: #000000;">&#40;</span>lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>, <span style="color: #0000dd;">0</span>, rlen * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span></p>
<p>&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;llen; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>j = <span style="color: #0000dd;">0</span>; j &lt;rlen; j++<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>lhs<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> == rhs<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0000dd;">0</span> == i || <span style="color: #0000dd;">0</span> == j<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> = <span style="color: #0000dd;">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> = lens<span style="color: #000000;">&#91;</span>i - <span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j - <span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span> + <span style="color: #0000dd;">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>&gt; maxlen<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxlen = lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = i;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #339900;">#ifdef OUTPUT</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"%2d"</span>, lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"<span style="color: #666666; font-weight: bold;">\n</span>"</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #339900;">#else</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #339900;">#endif</span><br />
&nbsp; <span style="color: #000000;">&#125;</span></p>
<p>&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;llen; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">free</span><span style="color: #000000;">&#40;</span>lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">free</span><span style="color: #000000;">&#40;</span>lens<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; buf = <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">char</span>*<span style="color: #000000;">&#41;</span><span style="color: #0000dd;">malloc</span><span style="color: #000000;">&#40;</span>maxlen + <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">memset</span><span style="color: #000000;">&#40;</span>buf, <span style="color: #0000dd;">0</span>, maxlen + <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; strncpy_s<span style="color: #000000;">&#40;</span>buf, maxlen + <span style="color: #0000dd;">1</span>, lhs + flag - maxlen + <span style="color: #0000dd;">1</span>, maxlen<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000ff;">return</span> buf;<br />
<span style="color: #000000;">&#125;</span></p>
<p><span style="color: #ff0000; font-style: italic;">/* 空间复杂度为O(max(m, n))，还没实现O(min(m, n)) */</span><br />
<span style="color: #0000ff;">char</span>* longest_common_substring_2<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* lhs, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* rhs<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">size_t</span> llen, rlen, max, maxlen = <span style="color: #0000dd;">0</span>, flag = <span style="color: #0000dd;">0</span>;<br />
&nbsp; <span style="color: #0000ff;">size_t</span> *rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #000000;">&#93;</span>, i, j;<br />
&nbsp; <span style="color: #0000ff;">char</span>* buf;</p>
<p>&nbsp; <span style="color: #0000dd;">assert</span><span style="color: #000000;">&#40;</span>lhs &amp;&amp; rhs<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; llen = <span style="color: #0000dd;">strlen</span><span style="color: #000000;">&#40;</span>lhs<span style="color: #000000;">&#41;</span>;<br />
&nbsp; rlen = <span style="color: #0000dd;">strlen</span><span style="color: #000000;">&#40;</span>rhs<span style="color: #000000;">&#41;</span>;<br />
&nbsp; max = GET_MAX<span style="color: #000000;">&#40;</span>llen, rlen<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;<span style="color: #0000dd;">2</span>; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; rows<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span>*<span style="color: #000000;">&#41;</span><span style="color: #0000dd;">malloc</span><span style="color: #000000;">&#40;</span>max * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000dd;">memset</span><span style="color: #000000;">&#40;</span>rows<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>, <span style="color: #0000dd;">0</span>, max * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span></p>
<p>&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;llen; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>j = <span style="color: #0000dd;">0</span>; j &lt;rlen; j++<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>lhs<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> == rhs<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0000dd;">0</span> == i || <span style="color: #0000dd;">0</span> == j<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> = <span style="color: #0000dd;">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> = rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j - <span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span> + <span style="color: #0000dd;">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>&gt; maxlen<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxlen = rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = i;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #339900;">#ifdef OUTPUT</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"%2d"</span>, rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"<span style="color: #666666; font-weight: bold;">\n</span>"</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #339900;">#else</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #339900;">#endif</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">memcpy</span><span style="color: #000000;">&#40;</span>rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #000000;">&#93;</span>, rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span>, max * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000dd;">memset</span><span style="color: #000000;">&#40;</span>rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span>, <span style="color: #0000dd;">0</span>, max * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #ff0000; font-style: italic;">/* 少了这行搞死我了 */</span><br />
&nbsp; <span style="color: #000000;">&#125;</span></p>
<p>&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;<span style="color: #0000dd;">2</span>; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">free</span><span style="color: #000000;">&#40;</span>rows<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; buf = <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">char</span>*<span style="color: #000000;">&#41;</span><span style="color: #0000dd;">malloc</span><span style="color: #000000;">&#40;</span>maxlen + <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">memset</span><span style="color: #000000;">&#40;</span>buf, <span style="color: #0000dd;">0</span>, maxlen + <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; strncpy_s<span style="color: #000000;">&#40;</span>buf, maxlen + <span style="color: #0000dd;">1</span>, lhs + flag - maxlen + <span style="color: #0000dd;">1</span>, maxlen<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000ff;">return</span> buf;<br />
<span style="color: #000000;">&#125;</span></p>
<p><span style="color: #0000ff;">int</span> main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">char</span> lhs<span style="color: #000000;">&#91;</span>MAX_LEN<span style="color: #000000;">&#93;</span>, rhs<span style="color: #000000;">&#91;</span>MAX_LEN<span style="color: #000000;">&#93;</span>, *ret;</p>
<p>&nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"input string 1: "</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; gets_s<span style="color: #000000;">&#40;</span>lhs, MAX_LEN<span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>!*lhs<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; strcpy_s<span style="color: #000000;">&#40;</span>lhs, MAX_LEN, <span style="color: #666666;">"AABBCCDD"</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"input string 2: "</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; gets_s<span style="color: #000000;">&#40;</span>rhs, MAX_LEN<span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>!*rhs<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; strcpy_s<span style="color: #000000;">&#40;</span>rhs, MAX_LEN, <span style="color: #666666;">"BBCCDDAA"</span><span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; ret = longest_common_substring<span style="color: #000000;">&#40;</span>lhs, rhs<span style="color: #000000;">&#41;</span>; <span style="color: #ff0000; font-style: italic;">/* free() manually */</span><br />
&nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"lcs:&nbsp; %s<span style="color: #666666; font-weight: bold;">\n</span>"</span>, ret<span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">free</span><span style="color: #000000;">&#40;</span>ret<span style="color: #000000;">&#41;</span>;<br />
&nbsp; ret = longest_common_substring_2<span style="color: #000000;">&#40;</span>lhs, rhs<span style="color: #000000;">&#41;</span>; <span style="color: #ff0000; font-style: italic;">/* free() manually */</span><br />
&nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"lcs2: %s<span style="color: #666666; font-weight: bold;">\n</span>"</span>, ret<span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">free</span><span style="color: #000000;">&#40;</span>ret<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;<br />
<span style="color: #000000;">&#125;</span></div>
</div>
</div>
<p></p>
<div class="entry-related"><h3>Related Posts</h3><ul></li>No Related Posts</li></ul>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2008.｜<a href="http://hellobmw.com/archives/dynamic-programming-longest-common-substring.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/dynamic-programming-longest-common-substring.html#comments" >3 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>
]]></description>
			<content:encoded><![CDATA[<p>我的算法学得好差，尤其是动态规划这一块，在这里吃了不少亏。</p>
<p>这次写的跟面试题有关。老早就知道最长公共子串（<a href="http://en.wikipedia.org/wiki/Longest_common_substring_problem"  class="external">Longest Common Substring</a>）这个问题，但从没认真搞过，这些天又过着伪充实的繁忙生活，直到今天才终于有点时间和心情（很重要）学习学习了。</p>
<p>原理啥的我还是不讲了，听我讲不如点击上面的链接自行阅读维基。下面我还是贴贴代码吧，写这点东西还是花了点时间的。</p>
<p>For the example strings "ABAB" and "BABA":</p>
<table class="wikitable" style="text-align:center">
<tr>
<th width="15"></th>
<th width="15"></th>
<th width="15">A</th>
<th width="15">B</th>
<th width="15">A</th>
<th width="15">B</th>
</tr>
<tr>
<td></td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>B</th>
<td>0</td>
<td>0</td>
<td style="color:red">1</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<th>A</th>
<td>0</td>
<td style="color:red">1</td>
<td>0</td>
<td style="color:red">2</td>
<td>0</td>
</tr>
<tr>
<th>B</th>
<td>0</td>
<td>0</td>
<td style="color:red">2</td>
<td>0</td>
<td style="color:red">3</td>
</tr>
<tr>
<th>A</th>
<td>0</td>
<td>1</td>
<td>0</td>
<td style="color:red">3</td>
<td>0</td>
</tr>
</table>
<p><em>貌似这里应该配个图表</em></p>
<div class="syntax_hilite">
<div id="cpp-6">
<div class="cpp"><span style="color: #ff0000; font-style: italic;">/************************************************************************<br />
The following tricks can be used to reduce the memory usage of an implementation:<br />
1) Keep only the last and current row of the DP table to save memory (O(min(m,n))<br />
&nbsp; &nbsp;instead of O(mn)).<br />
2) Store only non-zero values in the rows. You can do this by using hash tables<br />
&nbsp; &nbsp;instead of arrays. This is useful for large alphabets.<br />
&nbsp; &nbsp;[http://en.wikipedia.org/wiki/Longest_common_substring_problem]<br />
/************************************************************************/</span></p>
<p><span style="color: #339900;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339900;">#include &lt;string.h&gt;</span><br />
<span style="color: #339900;">#include &lt;malloc.h&gt;</span><br />
<span style="color: #339900;">#include &lt;memory.h&gt;</span><br />
<span style="color: #339900;">#include &lt;assert.h&gt;</span></p>
<p><span style="color: #339900;">#define GET_MAX(a, b) ((a)&gt; (b) ? (a) : (b))</span><br />
<span style="color: #339900;">#define GET_MIN(a, b) ((a) &lt;(b) ? (a) : (b))</span></p>
<p><span style="color: #339900;">#define MAX_LEN 256</span></p>
<p><span style="color: #339900;">#define OUTPUT</span></p>
<p><span style="color: #ff0000; font-style: italic;">/* must free() return value manually */</span><br />
<span style="color: #ff0000; font-style: italic;">/* 时间复杂度为O(mn) */</span></p>
<p><span style="color: #ff0000; font-style: italic;">/* 空间复杂度为O(mn) */</span><br />
<span style="color: #0000ff;">char</span>* longest_common_substring<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* lhs, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* rhs<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">size_t</span> llen, rlen, **lens, maxlen = <span style="color: #0000dd;">0</span>, flag = <span style="color: #0000dd;">0</span>;<br />
&nbsp; <span style="color: #0000ff;">size_t</span> i, j;<br />
&nbsp; <span style="color: #0000ff;">char</span>* buf;</p>
<p>&nbsp; <span style="color: #0000dd;">assert</span><span style="color: #000000;">&#40;</span>lhs &amp;&amp; rhs<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; llen = <span style="color: #0000dd;">strlen</span><span style="color: #000000;">&#40;</span>lhs<span style="color: #000000;">&#41;</span>;<br />
&nbsp; rlen = <span style="color: #0000dd;">strlen</span><span style="color: #000000;">&#40;</span>rhs<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; lens = <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span>**<span style="color: #000000;">&#41;</span><span style="color: #0000dd;">malloc</span><span style="color: #000000;">&#40;</span>llen * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span>*<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">memset</span><span style="color: #000000;">&#40;</span>lens, <span style="color: #0000dd;">0</span>, llen * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span>*<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;llen; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span>*<span style="color: #000000;">&#41;</span><span style="color: #0000dd;">malloc</span><span style="color: #000000;">&#40;</span>rlen * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000dd;">memset</span><span style="color: #000000;">&#40;</span>lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>, <span style="color: #0000dd;">0</span>, rlen * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span></p>
<p>&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;llen; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>j = <span style="color: #0000dd;">0</span>; j &lt;rlen; j++<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>lhs<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> == rhs<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0000dd;">0</span> == i || <span style="color: #0000dd;">0</span> == j<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> = <span style="color: #0000dd;">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> = lens<span style="color: #000000;">&#91;</span>i - <span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j - <span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span> + <span style="color: #0000dd;">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>&gt; maxlen<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxlen = lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = i;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #339900;">#ifdef OUTPUT</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"%2d"</span>, lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"<span style="color: #666666; font-weight: bold;">\n</span>"</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #339900;">#else</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #339900;">#endif</span><br />
&nbsp; <span style="color: #000000;">&#125;</span></p>
<p>&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;llen; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">free</span><span style="color: #000000;">&#40;</span>lens<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">free</span><span style="color: #000000;">&#40;</span>lens<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; buf = <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">char</span>*<span style="color: #000000;">&#41;</span><span style="color: #0000dd;">malloc</span><span style="color: #000000;">&#40;</span>maxlen + <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">memset</span><span style="color: #000000;">&#40;</span>buf, <span style="color: #0000dd;">0</span>, maxlen + <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; strncpy_s<span style="color: #000000;">&#40;</span>buf, maxlen + <span style="color: #0000dd;">1</span>, lhs + flag - maxlen + <span style="color: #0000dd;">1</span>, maxlen<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000ff;">return</span> buf;<br />
<span style="color: #000000;">&#125;</span></p>
<p><span style="color: #ff0000; font-style: italic;">/* 空间复杂度为O(max(m, n))，还没实现O(min(m, n)) */</span><br />
<span style="color: #0000ff;">char</span>* longest_common_substring_2<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* lhs, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* rhs<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">size_t</span> llen, rlen, max, maxlen = <span style="color: #0000dd;">0</span>, flag = <span style="color: #0000dd;">0</span>;<br />
&nbsp; <span style="color: #0000ff;">size_t</span> *rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #000000;">&#93;</span>, i, j;<br />
&nbsp; <span style="color: #0000ff;">char</span>* buf;</p>
<p>&nbsp; <span style="color: #0000dd;">assert</span><span style="color: #000000;">&#40;</span>lhs &amp;&amp; rhs<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; llen = <span style="color: #0000dd;">strlen</span><span style="color: #000000;">&#40;</span>lhs<span style="color: #000000;">&#41;</span>;<br />
&nbsp; rlen = <span style="color: #0000dd;">strlen</span><span style="color: #000000;">&#40;</span>rhs<span style="color: #000000;">&#41;</span>;<br />
&nbsp; max = GET_MAX<span style="color: #000000;">&#40;</span>llen, rlen<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;<span style="color: #0000dd;">2</span>; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; rows<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span>*<span style="color: #000000;">&#41;</span><span style="color: #0000dd;">malloc</span><span style="color: #000000;">&#40;</span>max * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000dd;">memset</span><span style="color: #000000;">&#40;</span>rows<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>, <span style="color: #0000dd;">0</span>, max * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span></p>
<p>&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;llen; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>j = <span style="color: #0000dd;">0</span>; j &lt;rlen; j++<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>lhs<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> == rhs<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0000dd;">0</span> == i || <span style="color: #0000dd;">0</span> == j<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> = <span style="color: #0000dd;">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> = rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j - <span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span> + <span style="color: #0000dd;">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>&gt; maxlen<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxlen = rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = i;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #339900;">#ifdef OUTPUT</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"%2d"</span>, rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"<span style="color: #666666; font-weight: bold;">\n</span>"</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #339900;">#else</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #339900;">#endif</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">memcpy</span><span style="color: #000000;">&#40;</span>rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #000000;">&#93;</span>, rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span>, max * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000dd;">memset</span><span style="color: #000000;">&#40;</span>rows<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span>, <span style="color: #0000dd;">0</span>, max * <span style="color: #0000dd;">sizeof</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">size_t</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #ff0000; font-style: italic;">/* 少了这行搞死我了 */</span><br />
&nbsp; <span style="color: #000000;">&#125;</span></p>
<p>&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #0000dd;">0</span>; i &lt;<span style="color: #0000dd;">2</span>; i++<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">free</span><span style="color: #000000;">&#40;</span>rows<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; buf = <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">char</span>*<span style="color: #000000;">&#41;</span><span style="color: #0000dd;">malloc</span><span style="color: #000000;">&#40;</span>maxlen + <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">memset</span><span style="color: #000000;">&#40;</span>buf, <span style="color: #0000dd;">0</span>, maxlen + <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; strncpy_s<span style="color: #000000;">&#40;</span>buf, maxlen + <span style="color: #0000dd;">1</span>, lhs + flag - maxlen + <span style="color: #0000dd;">1</span>, maxlen<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000ff;">return</span> buf;<br />
<span style="color: #000000;">&#125;</span></p>
<p><span style="color: #0000ff;">int</span> main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">char</span> lhs<span style="color: #000000;">&#91;</span>MAX_LEN<span style="color: #000000;">&#93;</span>, rhs<span style="color: #000000;">&#91;</span>MAX_LEN<span style="color: #000000;">&#93;</span>, *ret;</p>
<p>&nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"input string 1: "</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; gets_s<span style="color: #000000;">&#40;</span>lhs, MAX_LEN<span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>!*lhs<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; strcpy_s<span style="color: #000000;">&#40;</span>lhs, MAX_LEN, <span style="color: #666666;">"AABBCCDD"</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"input string 2: "</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; gets_s<span style="color: #000000;">&#40;</span>rhs, MAX_LEN<span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>!*rhs<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; strcpy_s<span style="color: #000000;">&#40;</span>rhs, MAX_LEN, <span style="color: #666666;">"BBCCDDAA"</span><span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; ret = longest_common_substring<span style="color: #000000;">&#40;</span>lhs, rhs<span style="color: #000000;">&#41;</span>; <span style="color: #ff0000; font-style: italic;">/* free() manually */</span><br />
&nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"lcs:&nbsp; %s<span style="color: #666666; font-weight: bold;">\n</span>"</span>, ret<span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">free</span><span style="color: #000000;">&#40;</span>ret<span style="color: #000000;">&#41;</span>;<br />
&nbsp; ret = longest_common_substring_2<span style="color: #000000;">&#40;</span>lhs, rhs<span style="color: #000000;">&#41;</span>; <span style="color: #ff0000; font-style: italic;">/* free() manually */</span><br />
&nbsp; <span style="color: #0000dd;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"lcs2: %s<span style="color: #666666; font-weight: bold;">\n</span>"</span>, ret<span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000dd;">free</span><span style="color: #000000;">&#40;</span>ret<span style="color: #000000;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;<br />
<span style="color: #000000;">&#125;</span></div>
</div>
</div>
<p></p>
<div class="entry-related"><h3>Related Posts</h3><ul></li>No Related Posts</li></ul>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2008.｜<a href="http://hellobmw.com/archives/dynamic-programming-longest-common-substring.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/dynamic-programming-longest-common-substring.html#comments" >3 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>]]></content:encoded>
			<wfw:commentRss>http://hellobmw.com/archives/dynamic-programming-longest-common-substring.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>[C/C++] 为什么把常量写在 == 运算符的左边</title>
		<link>http://hellobmw.com/archives/why-put-constant-at-the-left-side-of-equal-to-operator.html</link>
		<comments>http://hellobmw.com/archives/why-put-constant-at-the-left-side-of-equal-to-operator.html#comments</comments>
		<pubDate>Wed, 20 Aug 2008 07:04:09 +0000</pubDate>
		<dc:creator>北极冰仔</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[程序设计]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[cpp]]></category>

		<guid isPermaLink="false">http://hellobmw.com/?p=1132</guid>
		<description><![CDATA[<p>当然，把常量写在 == 运算符的左或右对运算本身没有任何影响，它们仅仅是两种不同的书写风格。所以我敢保证很多人并不会因为看到某些帖子宣称“应该将常量放在左边”就真的改变自己固有的风格，说到底，习惯而已。</p>
<p>不过，将常量写在运算符左边在某种情况下可能会更容易查错。看下面的例子：</p>
<p>片段一，当且仅当 <code>flag == 5</code> 时执行循环体中的内容：</p>
<div class="syntax_hilite">
<div id="c-7">
<div class="c"><span style="color: #993333;">int</span> flag;<br />
...<br />
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>flag == <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #808080; font-style: italic;">/* do something */</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>片段二，手快敲掉了一个 =，将会导致死循环，因为 <code>flag = 5</code> 总为真，当代码量很大的时候，要 debug 出这样的错误恐怕不太容易：</p>
<div class="syntax_hilite">
<div id="c-8">
<div class="c"><span style="color: #993333;">int</span> flag;<br />
...<br />
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>flag = <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #808080; font-style: italic;">/* do something */</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>片段三，常量放在左边，即使侧漏了一个 = 号也很安全，编译器会告诉你这里 Error 了：</p>
<div class="syntax_hilite">
<div id="c-9">
<div class="c"><span style="color: #993333;">int</span> flag;<br />
...<br />
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span> = flag<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #808080; font-style: italic;">/* do something */</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>用一句话总结：<strong>良好的书写风格可以让臭虫在编译时而不是运行时现身</strong>。</p>
<div class="entry-related"><h3>Related Posts</h3><ul><li><a href="http://hellobmw.com/archives/alchemy-cc-to-as3-compiler.html"  title="神器 Alchemy！C/C++ 到 AS3 的编译器">神器 Alchemy！C/C++ 到 AS3 的编译器</a></li><li><a href="http://hellobmw.com/archives/traps-in-casting-float-to-dword.html"  title="float 转换 DWORD 的陷阱">float 转换 DWORD 的陷阱</a></li><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-2.html"  title="Google C++ 编程风格指南（1）：头文件（2）">Google C++ 编程风格指南（1）：头文件（2）</a></li><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-1.html"  title="Google C++ 编程风格指南（1）：头文件（1）">Google C++ 编程风格指南（1）：头文件（1）</a></li><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-0-background.html"  title="Google C++ 编程风格指南（0）：背景">Google C++ 编程风格指南（0）：背景</a></li><li><a href="http://hellobmw.com/archives/protocol-buffers-performance.html"  title="Protocol Buffers 性能测试">Protocol Buffers 性能测试</a></li></ul></div>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2008.｜<a href="http://hellobmw.com/archives/why-put-constant-at-the-left-side-of-equal-to-operator.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/why-put-constant-at-the-left-side-of-equal-to-operator.html#comments" >10 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>
]]></description>
			<content:encoded><![CDATA[<p>当然，把常量写在 == 运算符的左或右对运算本身没有任何影响，它们仅仅是两种不同的书写风格。所以我敢保证很多人并不会因为看到某些帖子宣称“应该将常量放在左边”就真的改变自己固有的风格，说到底，习惯而已。</p>
<p>不过，将常量写在运算符左边在某种情况下可能会更容易查错。看下面的例子：</p>
<p>片段一，当且仅当 <code>flag == 5</code> 时执行循环体中的内容：</p>
<div class="syntax_hilite">
<div id="c-10">
<div class="c"><span style="color: #993333;">int</span> flag;<br />
...<br />
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>flag == <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #808080; font-style: italic;">/* do something */</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>片段二，手快敲掉了一个 =，将会导致死循环，因为 <code>flag = 5</code> 总为真，当代码量很大的时候，要 debug 出这样的错误恐怕不太容易：</p>
<div class="syntax_hilite">
<div id="c-11">
<div class="c"><span style="color: #993333;">int</span> flag;<br />
...<br />
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>flag = <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #808080; font-style: italic;">/* do something */</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>片段三，常量放在左边，即使侧漏了一个 = 号也很安全，编译器会告诉你这里 Error 了：</p>
<div class="syntax_hilite">
<div id="c-12">
<div class="c"><span style="color: #993333;">int</span> flag;<br />
...<br />
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span> = flag<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #808080; font-style: italic;">/* do something */</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>用一句话总结：<strong>良好的书写风格可以让臭虫在编译时而不是运行时现身</strong>。</p>
<div class="entry-related"><h3>Related Posts</h3><ul><li><a href="http://hellobmw.com/archives/alchemy-cc-to-as3-compiler.html"  title="神器 Alchemy！C/C++ 到 AS3 的编译器">神器 Alchemy！C/C++ 到 AS3 的编译器</a></li><li><a href="http://hellobmw.com/archives/traps-in-casting-float-to-dword.html"  title="float 转换 DWORD 的陷阱">float 转换 DWORD 的陷阱</a></li><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-2.html"  title="Google C++ 编程风格指南（1）：头文件（2）">Google C++ 编程风格指南（1）：头文件（2）</a></li><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-1.html"  title="Google C++ 编程风格指南（1）：头文件（1）">Google C++ 编程风格指南（1）：头文件（1）</a></li><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-0-background.html"  title="Google C++ 编程风格指南（0）：背景">Google C++ 编程风格指南（0）：背景</a></li><li><a href="http://hellobmw.com/archives/protocol-buffers-performance.html"  title="Protocol Buffers 性能测试">Protocol Buffers 性能测试</a></li></ul></div>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2008.｜<a href="http://hellobmw.com/archives/why-put-constant-at-the-left-side-of-equal-to-operator.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/why-put-constant-at-the-left-side-of-equal-to-operator.html#comments" >10 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>]]></content:encoded>
			<wfw:commentRss>http://hellobmw.com/archives/why-put-constant-at-the-left-side-of-equal-to-operator.html/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>[算法] 回溯法：八皇后问题</title>
		<link>http://hellobmw.com/archives/backtracking-algorithms-eight-queens-puzzle.html</link>
		<comments>http://hellobmw.com/archives/backtracking-algorithms-eight-queens-puzzle.html#comments</comments>
		<pubDate>Sat, 09 Aug 2008 13:07:06 +0000</pubDate>
		<dc:creator>北极冰仔</dc:creator>
				<category><![CDATA[Algorithmics]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[程序设计]]></category>
		<category><![CDATA[Backtracking]]></category>
		<category><![CDATA[Eight Queens]]></category>

		<guid isPermaLink="false">http://hellobmw.com/?p=1115</guid>
		<description><![CDATA[<p>继续引用维基的图来说明（<a href="http://en.wikipedia.org/wiki/Eight_queens_puzzle"  class="external">八皇后问题</a>中的）<a href="http://en.wikipedia.org/wiki/Backtracking"  class="external">回溯</a>：<br />
<img src="http://hellobmw.com/wp-content/uploads/eight-queens-animation.gif" alt="" title="Backtracking of Eight Queens Puzzle" width="352" height="352" class="alignnone size-full wp-image-1116" /></p>
<p>下面还是贴代码，程序会对树进行遍历（共 2057 个节点），从中找出所有可能的解（不同解共 92 个，非唯一解）。</p>
<div class="syntax_hilite">
<div id="c-13">
<div class="c"><span style="color: #339933;">#include &lt;stdio.h&gt;</span></p>
<p><span style="color: #339933;">#define N 8</span></p>
<p><span style="color: #993333;">int</span> solution<span style="color: #66cc66;">&#91;</span>N<span style="color: #66cc66;">&#93;</span>, j, k, count, sols;</p>
<p><span style="color: #993333;">int</span> place<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> row, <span style="color: #993333;">int</span> col<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &lt;row; j++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>row - j == solution<span style="color: #66cc66;">&#91;</span>row<span style="color: #66cc66;">&#93;</span> - solution<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> || row + solution<span style="color: #66cc66;">&#91;</span>row<span style="color: #66cc66;">&#93;</span> == j + solution<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> || solution<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> == solution<span style="color: #66cc66;">&#91;</span>row<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;<br />
&nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">1</span>;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #993333;">void</span> backtrack<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> row<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; count++;<br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>N == row<span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; sols++;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>k = <span style="color: #cc66cc;">0</span>; k &lt;N; k++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"  class="external"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"%d<span style="color: #000099; font-weight: bold;">\t</span>"</span>, solution<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"  class="external"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> i;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i &lt;N; i++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; solution<span style="color: #66cc66;">&#91;</span>row<span style="color: #66cc66;">&#93;</span> = i;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>place<span style="color: #66cc66;">&#40;</span>row, i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; backtrack<span style="color: #66cc66;">&#40;</span>row + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #993333;">void</span> queens<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; backtrack<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #993333;">int</span> main<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">void</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; queens<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"  class="external"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"Total Solutions: %d<span style="color: #000099; font-weight: bold;">\n</span>"</span>, sols<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<div class="entry-related"><h3>Related Posts</h3><ul></li>No Related Posts</li></ul>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2008.｜<a href="http://hellobmw.com/archives/backtracking-algorithms-eight-queens-puzzle.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/backtracking-algorithms-eight-queens-puzzle.html#comments" >10 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>
]]></description>
			<content:encoded><![CDATA[<p>继续引用维基的图来说明（<a href="http://en.wikipedia.org/wiki/Eight_queens_puzzle"  class="external">八皇后问题</a>中的）<a href="http://en.wikipedia.org/wiki/Backtracking"  class="external">回溯</a>：<br />
<img src="http://hellobmw.com/wp-content/uploads/eight-queens-animation.gif" alt="" title="Backtracking of Eight Queens Puzzle" width="352" height="352" class="alignnone size-full wp-image-1116" /></p>
<p>下面还是贴代码，程序会对树进行遍历（共 2057 个节点），从中找出所有可能的解（不同解共 92 个，非唯一解）。</p>
<div class="syntax_hilite">
<div id="c-14">
<div class="c"><span style="color: #339933;">#include &lt;stdio.h&gt;</span></p>
<p><span style="color: #339933;">#define N 8</span></p>
<p><span style="color: #993333;">int</span> solution<span style="color: #66cc66;">&#91;</span>N<span style="color: #66cc66;">&#93;</span>, j, k, count, sols;</p>
<p><span style="color: #993333;">int</span> place<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> row, <span style="color: #993333;">int</span> col<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &lt;row; j++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>row - j == solution<span style="color: #66cc66;">&#91;</span>row<span style="color: #66cc66;">&#93;</span> - solution<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> || row + solution<span style="color: #66cc66;">&#91;</span>row<span style="color: #66cc66;">&#93;</span> == j + solution<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> || solution<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> == solution<span style="color: #66cc66;">&#91;</span>row<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;<br />
&nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">1</span>;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #993333;">void</span> backtrack<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> row<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; count++;<br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>N == row<span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; sols++;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>k = <span style="color: #cc66cc;">0</span>; k &lt;N; k++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"  class="external"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"%d<span style="color: #000099; font-weight: bold;">\t</span>"</span>, solution<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"  class="external"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> i;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i &lt;N; i++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; solution<span style="color: #66cc66;">&#91;</span>row<span style="color: #66cc66;">&#93;</span> = i;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>place<span style="color: #66cc66;">&#40;</span>row, i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; backtrack<span style="color: #66cc66;">&#40;</span>row + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #993333;">void</span> queens<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; backtrack<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #993333;">int</span> main<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">void</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; queens<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"  class="external"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"Total Solutions: %d<span style="color: #000099; font-weight: bold;">\n</span>"</span>, sols<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<div class="entry-related"><h3>Related Posts</h3><ul></li>No Related Posts</li></ul>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2008.｜<a href="http://hellobmw.com/archives/backtracking-algorithms-eight-queens-puzzle.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/backtracking-algorithms-eight-queens-puzzle.html#comments" >10 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>]]></content:encoded>
			<wfw:commentRss>http://hellobmw.com/archives/backtracking-algorithms-eight-queens-puzzle.html/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>[算法] 分治策略：快速排序</title>
		<link>http://hellobmw.com/archives/divide-and-conque-quick-sort-algorithm.html</link>
		<comments>http://hellobmw.com/archives/divide-and-conque-quick-sort-algorithm.html#comments</comments>
		<pubDate>Thu, 07 Aug 2008 06:34:52 +0000</pubDate>
		<dc:creator>北极冰仔</dc:creator>
				<category><![CDATA[Algorithmics]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[程序设计]]></category>
		<category><![CDATA[Divide and Conque]]></category>
		<category><![CDATA[Quick Sort]]></category>
		<category><![CDATA[Sort Algorithm]]></category>

		<guid isPermaLink="false">http://hellobmw.com/?p=1106</guid>
		<description><![CDATA[<p>先引用<a href="http://en.wikipedia.org/wiki/Quicksort"  title="Quick Sort" class="external">维基</a>上的一个图，它很形象地展示了快速排序的整个过程：<br />
<a href="http://en.wikipedia.org/wiki/Quicksort"  class="external"><img src="http://hellobmw.com/wp-content/uploads/sorting_quicksort_anim.gif" alt="快速排序算法图解" title="快速排序算法图解" width="280" height="214" class="size-full wp-image-1107" /></a></p>
<p>快速排序的原理可参考上面的链接或任意一本算法教材，让我来讲也讲不清楚，我也是花好久才彻底看明白（基础太差了，汗），所以就贴贴代码好了，有人会说了，快排的代码网上多得很，还要你来贴干嘛。因为这些代码是我看着《算法基础》这本书上的原理和图自己琢磨出来的（完工后发现跟别人的代码惊人地雷同，汗汗汗），只有递归的终止条件那里参考了一下现成的代码，留下来当作笔记吧。</p>
<div class="syntax_hilite">
<div id="c-15">
<div class="c"><span style="color: #339933;">#include &lt;stdio.h&gt;</span></p>
<p><span style="color: #993333;">int</span> pivot<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> a<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #993333;">int</span> left_, <span style="color: #993333;">int</span> right_<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> i, j, p, k, tmp;<br />
&nbsp; &nbsp; k = i = left_;<br />
&nbsp; &nbsp; j = right_ + <span style="color: #cc66cc;">1</span>;<br />
&nbsp; &nbsp; p = a<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#91;</span>++i<span style="color: #66cc66;">&#93;</span> &lt;= p &amp;&amp; i &lt;right_<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#91;</span>--j<span style="color: #66cc66;">&#93;</span>&gt; p &amp;&amp; j&gt;= left_<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>i &lt;j<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp = a<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> = a<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> = tmp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; tmp = a<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; a<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span> = a<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; a<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> = tmp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> j;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #993333;">void</span> quick_sort<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> a<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #993333;">int</span> left_, <span style="color: #993333;">int</span> right_<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>left_ &lt;right_<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> p = pivot<span style="color: #66cc66;">&#40;</span>a, left_, right_<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; quick_sort<span style="color: #66cc66;">&#40;</span>a, left_, p - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; quick_sort<span style="color: #66cc66;">&#40;</span>a, p + <span style="color: #cc66cc;">1</span>, right_<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #993333;">int</span> main<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">void</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> a<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">9</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">8</span>, <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#125;</span>;<br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> len = <span style="color: #993333;">sizeof</span><span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> / <span style="color: #993333;">sizeof</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; quick_sort<span style="color: #66cc66;">&#40;</span>a, <span style="color: #cc66cc;">0</span>, len - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<div class="entry-related"><h3>Related Posts</h3><ul></li>No Related Posts</li></ul>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2008.｜<a href="http://hellobmw.com/archives/divide-and-conque-quick-sort-algorithm.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/divide-and-conque-quick-sort-algorithm.html#comments" >4 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>
]]></description>
			<content:encoded><![CDATA[<p>先引用<a href="http://en.wikipedia.org/wiki/Quicksort"  title="Quick Sort" class="external">维基</a>上的一个图，它很形象地展示了快速排序的整个过程：<br />
<a href="http://en.wikipedia.org/wiki/Quicksort"  class="external"><img src="http://hellobmw.com/wp-content/uploads/sorting_quicksort_anim.gif" alt="快速排序算法图解" title="快速排序算法图解" width="280" height="214" class="size-full wp-image-1107" /></a></p>
<p>快速排序的原理可参考上面的链接或任意一本算法教材，让我来讲也讲不清楚，我也是花好久才彻底看明白（基础太差了，汗），所以就贴贴代码好了，有人会说了，快排的代码网上多得很，还要你来贴干嘛。因为这些代码是我看着《算法基础》这本书上的原理和图自己琢磨出来的（完工后发现跟别人的代码惊人地雷同，汗汗汗），只有递归的终止条件那里参考了一下现成的代码，留下来当作笔记吧。</p>
<div class="syntax_hilite">
<div id="c-16">
<div class="c"><span style="color: #339933;">#include &lt;stdio.h&gt;</span></p>
<p><span style="color: #993333;">int</span> pivot<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> a<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #993333;">int</span> left_, <span style="color: #993333;">int</span> right_<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> i, j, p, k, tmp;<br />
&nbsp; &nbsp; k = i = left_;<br />
&nbsp; &nbsp; j = right_ + <span style="color: #cc66cc;">1</span>;<br />
&nbsp; &nbsp; p = a<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#91;</span>++i<span style="color: #66cc66;">&#93;</span> &lt;= p &amp;&amp; i &lt;right_<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#91;</span>--j<span style="color: #66cc66;">&#93;</span>&gt; p &amp;&amp; j&gt;= left_<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>i &lt;j<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp = a<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> = a<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> = tmp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; tmp = a<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; a<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span> = a<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; a<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> = tmp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> j;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #993333;">void</span> quick_sort<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> a<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #993333;">int</span> left_, <span style="color: #993333;">int</span> right_<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>left_ &lt;right_<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> p = pivot<span style="color: #66cc66;">&#40;</span>a, left_, right_<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; quick_sort<span style="color: #66cc66;">&#40;</span>a, left_, p - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; quick_sort<span style="color: #66cc66;">&#40;</span>a, p + <span style="color: #cc66cc;">1</span>, right_<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #993333;">int</span> main<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">void</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> a<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">9</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">8</span>, <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#125;</span>;<br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> len = <span style="color: #993333;">sizeof</span><span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> / <span style="color: #993333;">sizeof</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; quick_sort<span style="color: #66cc66;">&#40;</span>a, <span style="color: #cc66cc;">0</span>, len - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<div class="entry-related"><h3>Related Posts</h3><ul></li>No Related Posts</li></ul>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2008.｜<a href="http://hellobmw.com/archives/divide-and-conque-quick-sort-algorithm.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/divide-and-conque-quick-sort-algorithm.html#comments" >4 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>]]></content:encoded>
			<wfw:commentRss>http://hellobmw.com/archives/divide-and-conque-quick-sort-algorithm.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>我的答案：浙大ACM#1350（The Drunk Jailer）</title>
		<link>http://hellobmw.com/archives/zoj-1350-the-drunk-jailer.html</link>
		<comments>http://hellobmw.com/archives/zoj-1350-the-drunk-jailer.html#comments</comments>
		<pubDate>Thu, 24 Jul 2008 05:12:05 +0000</pubDate>
		<dc:creator>北极冰仔</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[程序设计]]></category>
		<category><![CDATA[ACM]]></category>
		<category><![CDATA[ZOJ]]></category>

		<guid isPermaLink="false">http://hellobmw.com/?p=1042</guid>
		<description><![CDATA[<p><a href="http://acm.zju.edu.cn/show_problem.php?pid=1350"  class="external">题目地址</a></p>
<blockquote><p>
A certain prison contains a long hall of n cells, each right next to each other. Each cell has a prisoner in it, and each cell is locked.</p>
<p>One night, the jailer gets bored and decides to play a game. For round 1 of the game, he takes a drink of whiskey, and then runs down the hall unlocking each cell. For round 2, he takes a drink of whiskey, and then runs down the hall locking every other cell (cells 2, 4, 6, …). For round 3, he takes a drink of whiskey, and then runs down the hall. He visits every third cell (cells 3, 6, 9, …). If the cell is locked, he unlocks it; if it is unlocked, he locks it. He repeats this for n rounds, takes a final drink, and passes out.</p>
<p>Some number of prisoners, possibly zero, realizes that their cells are unlocked and the jailer is incapacitated. They immediately escape.</p>
<p>Given the number of cells, determine how many prisoners escape jail.</p>
<p>Input</p>
<p>The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines contains a single integer between 5 and 100, inclusive, which is the number of cells n.</p>
<p>Output</p>
<p>For each line, you must print out the number of prisoners that escape when the prison has n cells.</p>
<p>Sample Input</p>
<p>2<br />
5<br />
100</p>
<p>Sample Output</p>
<p>2<br />
10
</p></blockquote>
<p>我的代码是用 C 写的，运行耗时 0 秒，占用内存 392K，比高手们多出了 8K。在内存控制方面我始终把握得不好，不过比刚开始玩时写用了 STL 和类的 C++ 好多了。 <img src='http://hellobmw.com/wp-includes/images/smilies/icon_lol.gif' alt=':lol:' class='wp-smiley' /> </p>
<div class="syntax_hilite">
<div id="c-17">
<div class="c"><span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;malloc.h&gt;</span></p>
<p><span style="color: #993333;">int</span> main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> c, i, *cases = <span style="color: #cc66cc;">0</span>, *esc = <span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; scanf<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"%d"</span>, &amp;c<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span> == c<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> -<span style="color: #cc66cc;">1</span>;<br />
&nbsp; &nbsp; cases = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span>*<span style="color: #66cc66;">&#41;</span>malloc<span style="color: #66cc66;">&#40;</span>c * <span style="color: #993333;">sizeof</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; esc = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span>*<span style="color: #66cc66;">&#41;</span>malloc<span style="color: #66cc66;">&#40;</span>c * <span style="color: #993333;">sizeof</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i &lt;c; i++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; scanf<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"%d"</span>, cases + i<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">char</span>* cells = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">char</span>*<span style="color: #66cc66;">&#41;</span>malloc<span style="color: #66cc66;">&#40;</span>*<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span> + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> j;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &lt;<span style="color: #66cc66;">&#40;</span>*<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; j++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<span style="color: #66cc66;">&#40;</span>cells + j<span style="color: #66cc66;">&#41;</span> = <span style="color: #ff0000;">'0'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; *<span style="color: #66cc66;">&#40;</span>cells + <span style="color: #66cc66;">&#40;</span>*<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> = <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\0</span>'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &lt;<span style="color: #66cc66;">&#40;</span>*<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; j++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> k;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>k = <span style="color: #cc66cc;">0</span>; k &lt;<span style="color: #66cc66;">&#40;</span>*<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; k++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>j * <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> + k<span style="color: #66cc66;">&#41;</span> + k<span style="color: #66cc66;">&#41;</span>&gt;= *<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'0'</span> == *<span style="color: #66cc66;">&#40;</span>cells + j * <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> + k<span style="color: #66cc66;">&#41;</span> + k<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<span style="color: #66cc66;">&#40;</span>cells + j * <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> + k<span style="color: #66cc66;">&#41;</span> + k<span style="color: #66cc66;">&#41;</span> = <span style="color: #ff0000;">'1'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1'</span> == *<span style="color: #66cc66;">&#40;</span>cells + j * <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> + k<span style="color: #66cc66;">&#41;</span> + k<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<span style="color: #66cc66;">&#40;</span>cells + j * <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> + k<span style="color: #66cc66;">&#41;</span> + k<span style="color: #66cc66;">&#41;</span> = <span style="color: #ff0000;">'0'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; *<span style="color: #66cc66;">&#40;</span>esc + i<span style="color: #66cc66;">&#41;</span> = <span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &lt;<span style="color: #66cc66;">&#40;</span>*<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; j++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1'</span> == *<span style="color: #66cc66;">&#40;</span>cells + j<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span>*<span style="color: #66cc66;">&#40;</span>esc + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; free<span style="color: #66cc66;">&#40;</span>cells<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; free<span style="color: #66cc66;">&#40;</span>cases<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>esc<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i &lt;c; i++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"  class="external"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"%d<span style="color: #000099; font-weight: bold;">\n</span>"</span>, *<span style="color: #66cc66;">&#40;</span>esc + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; free<span style="color: #66cc66;">&#40;</span>esc<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>#更新：感谢 <a href="http://jiangli.org/blog/"  class="external">Jiang</a> 的<a href="http://hellobmw.com/archives/zoj-1350-the-drunk-jailer.html#comment-19551" >留言</a>，经过改进，程序占用内存减少了 4K，hooray！下面是改进后的代码。（起初我以为不在最后把计算结果一起输出会 WA，事实上每次计算结束就输出也是 AC。再次感谢 <a href="http://jiangli.org/blog/"  class="external">Jiang</a>！）</p>
<div class="syntax_hilite">
<div id="c-18">
<div class="c"><span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;malloc.h&gt;</span></p>
<p><span style="color: #993333;">int</span> main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> c, i, j, k, n, *cases = <span style="color: #cc66cc;">0</span>, e;<br />
&nbsp; &nbsp; scanf<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"%d"</span>, &amp;c<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span> == c<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> -<span style="color: #cc66cc;">1</span>;<br />
&nbsp; &nbsp; cases = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span>*<span style="color: #66cc66;">&#41;</span>malloc<span style="color: #66cc66;">&#40;</span>c * <span style="color: #993333;">sizeof</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i &lt;c; i++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; e = <span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; scanf<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"%d"</span>, cases + i<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; n = *<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">char</span>* cells = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">char</span>*<span style="color: #66cc66;">&#41;</span>malloc<span style="color: #66cc66;">&#40;</span>n + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &lt;n; j++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cells<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">'0'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; cells<span style="color: #66cc66;">&#91;</span>n<span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\0</span>'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &lt;n; j++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>k = j; k &lt;n; k += j + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cells<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span>++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &lt;n; j++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> == cells<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> % <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"  class="external"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"%d<span style="color: #000099; font-weight: bold;">\n</span>"</span>, e<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; free<span style="color: #66cc66;">&#40;</span>cells<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; free<span style="color: #66cc66;">&#40;</span>cases<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<div class="entry-related"><h3>Related Posts</h3><ul><li><a href="http://hellobmw.com/archives/dna-sorting-pku-acm-1007.html"  title="我的答案：北大ACM#1007（DNA Sorting）">我的答案：北大ACM#1007（DNA Sorting）</a></li><li><a href="http://hellobmw.com/archives/playing-acm-on-zoj.html"  title="这几天在干嘛……">这几天在干嘛……</a></li></ul></div>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2008.｜<a href="http://hellobmw.com/archives/zoj-1350-the-drunk-jailer.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/zoj-1350-the-drunk-jailer.html#comments" >17 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://acm.zju.edu.cn/show_problem.php?pid=1350"  class="external">题目地址</a></p>
<blockquote><p>
A certain prison contains a long hall of n cells, each right next to each other. Each cell has a prisoner in it, and each cell is locked.</p>
<p>One night, the jailer gets bored and decides to play a game. For round 1 of the game, he takes a drink of whiskey, and then runs down the hall unlocking each cell. For round 2, he takes a drink of whiskey, and then runs down the hall locking every other cell (cells 2, 4, 6, …). For round 3, he takes a drink of whiskey, and then runs down the hall. He visits every third cell (cells 3, 6, 9, …). If the cell is locked, he unlocks it; if it is unlocked, he locks it. He repeats this for n rounds, takes a final drink, and passes out.</p>
<p>Some number of prisoners, possibly zero, realizes that their cells are unlocked and the jailer is incapacitated. They immediately escape.</p>
<p>Given the number of cells, determine how many prisoners escape jail.</p>
<p>Input</p>
<p>The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines contains a single integer between 5 and 100, inclusive, which is the number of cells n.</p>
<p>Output</p>
<p>For each line, you must print out the number of prisoners that escape when the prison has n cells.</p>
<p>Sample Input</p>
<p>2<br />
5<br />
100</p>
<p>Sample Output</p>
<p>2<br />
10
</p></blockquote>
<p>我的代码是用 C 写的，运行耗时 0 秒，占用内存 392K，比高手们多出了 8K。在内存控制方面我始终把握得不好，不过比刚开始玩时写用了 STL 和类的 C++ 好多了。 <img src='http://hellobmw.com/wp-includes/images/smilies/icon_lol.gif' alt=':lol:' class='wp-smiley' /> </p>
<div class="syntax_hilite">
<div id="c-19">
<div class="c"><span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;malloc.h&gt;</span></p>
<p><span style="color: #993333;">int</span> main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> c, i, *cases = <span style="color: #cc66cc;">0</span>, *esc = <span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; scanf<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"%d"</span>, &amp;c<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span> == c<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> -<span style="color: #cc66cc;">1</span>;<br />
&nbsp; &nbsp; cases = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span>*<span style="color: #66cc66;">&#41;</span>malloc<span style="color: #66cc66;">&#40;</span>c * <span style="color: #993333;">sizeof</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; esc = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span>*<span style="color: #66cc66;">&#41;</span>malloc<span style="color: #66cc66;">&#40;</span>c * <span style="color: #993333;">sizeof</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i &lt;c; i++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; scanf<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"%d"</span>, cases + i<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">char</span>* cells = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">char</span>*<span style="color: #66cc66;">&#41;</span>malloc<span style="color: #66cc66;">&#40;</span>*<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span> + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> j;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &lt;<span style="color: #66cc66;">&#40;</span>*<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; j++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<span style="color: #66cc66;">&#40;</span>cells + j<span style="color: #66cc66;">&#41;</span> = <span style="color: #ff0000;">'0'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; *<span style="color: #66cc66;">&#40;</span>cells + <span style="color: #66cc66;">&#40;</span>*<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> = <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\0</span>'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &lt;<span style="color: #66cc66;">&#40;</span>*<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; j++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> k;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>k = <span style="color: #cc66cc;">0</span>; k &lt;<span style="color: #66cc66;">&#40;</span>*<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; k++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>j * <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> + k<span style="color: #66cc66;">&#41;</span> + k<span style="color: #66cc66;">&#41;</span>&gt;= *<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'0'</span> == *<span style="color: #66cc66;">&#40;</span>cells + j * <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> + k<span style="color: #66cc66;">&#41;</span> + k<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<span style="color: #66cc66;">&#40;</span>cells + j * <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> + k<span style="color: #66cc66;">&#41;</span> + k<span style="color: #66cc66;">&#41;</span> = <span style="color: #ff0000;">'1'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1'</span> == *<span style="color: #66cc66;">&#40;</span>cells + j * <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> + k<span style="color: #66cc66;">&#41;</span> + k<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<span style="color: #66cc66;">&#40;</span>cells + j * <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> + k<span style="color: #66cc66;">&#41;</span> + k<span style="color: #66cc66;">&#41;</span> = <span style="color: #ff0000;">'0'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; *<span style="color: #66cc66;">&#40;</span>esc + i<span style="color: #66cc66;">&#41;</span> = <span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &lt;<span style="color: #66cc66;">&#40;</span>*<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; j++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1'</span> == *<span style="color: #66cc66;">&#40;</span>cells + j<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span>*<span style="color: #66cc66;">&#40;</span>esc + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; free<span style="color: #66cc66;">&#40;</span>cells<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; free<span style="color: #66cc66;">&#40;</span>cases<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>esc<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i &lt;c; i++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"  class="external"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"%d<span style="color: #000099; font-weight: bold;">\n</span>"</span>, *<span style="color: #66cc66;">&#40;</span>esc + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; free<span style="color: #66cc66;">&#40;</span>esc<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>#更新：感谢 <a href="http://jiangli.org/blog/"  class="external">Jiang</a> 的<a href="http://hellobmw.com/archives/zoj-1350-the-drunk-jailer.html#comment-19551" >留言</a>，经过改进，程序占用内存减少了 4K，hooray！下面是改进后的代码。（起初我以为不在最后把计算结果一起输出会 WA，事实上每次计算结束就输出也是 AC。再次感谢 <a href="http://jiangli.org/blog/"  class="external">Jiang</a>！）</p>
<div class="syntax_hilite">
<div id="c-20">
<div class="c"><span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;malloc.h&gt;</span></p>
<p><span style="color: #993333;">int</span> main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> c, i, j, k, n, *cases = <span style="color: #cc66cc;">0</span>, e;<br />
&nbsp; &nbsp; scanf<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"%d"</span>, &amp;c<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span> == c<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> -<span style="color: #cc66cc;">1</span>;<br />
&nbsp; &nbsp; cases = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span>*<span style="color: #66cc66;">&#41;</span>malloc<span style="color: #66cc66;">&#40;</span>c * <span style="color: #993333;">sizeof</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i &lt;c; i++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; e = <span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; scanf<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"%d"</span>, cases + i<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; n = *<span style="color: #66cc66;">&#40;</span>cases + i<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">char</span>* cells = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">char</span>*<span style="color: #66cc66;">&#41;</span>malloc<span style="color: #66cc66;">&#40;</span>n + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &lt;n; j++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cells<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">'0'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; cells<span style="color: #66cc66;">&#91;</span>n<span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\0</span>'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &lt;n; j++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>k = j; k &lt;n; k += j + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cells<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span>++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &lt;n; j++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> == cells<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> % <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"  class="external"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"%d<span style="color: #000099; font-weight: bold;">\n</span>"</span>, e<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; free<span style="color: #66cc66;">&#40;</span>cells<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; free<span style="color: #66cc66;">&#40;</span>cases<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<div class="entry-related"><h3>Related Posts</h3><ul><li><a href="http://hellobmw.com/archives/dna-sorting-pku-acm-1007.html"  title="我的答案：北大ACM#1007（DNA Sorting）">我的答案：北大ACM#1007（DNA Sorting）</a></li><li><a href="http://hellobmw.com/archives/playing-acm-on-zoj.html"  title="这几天在干嘛……">这几天在干嘛……</a></li></ul></div>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2008.｜<a href="http://hellobmw.com/archives/zoj-1350-the-drunk-jailer.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/zoj-1350-the-drunk-jailer.html#comments" >17 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>]]></content:encoded>
			<wfw:commentRss>http://hellobmw.com/archives/zoj-1350-the-drunk-jailer.html/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Google C++ 编程风格指南（1）：头文件（2）</title>
		<link>http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-2.html</link>
		<comments>http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-2.html#comments</comments>
		<pubDate>Fri, 11 Jul 2008 15:58:29 +0000</pubDate>
		<dc:creator>北极冰仔</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[程序设计]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Guide]]></category>

		<guid isPermaLink="false">http://hellobmw.com/?p=1092</guid>
		<description><![CDATA[<h3>Google C++ 编程风格指南（1）：头文件（2）</h3>
<ul>
<li>-inl.h 文件</li>
<li>函数的参数顺序</li>
<li>头文件名称的顺序</li>
</ul>
<h3>-inl.h 文件</h3>
<p>当需要的时候，你应该使用带有 -inl.h 后缀的名字来定义复杂的<a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-1.html" >内联函数</a>。</p>
<p>内联函数的定义需要放在一个头文件里，使得编译器在发生函数调用的地点展开函数定义。尽管如此，实现代码出现在 .cc 文件中更为合适，我们不想让 .h 文件拥有太多的代码，除非那样做可以提高可读性或是性能。</p>
<p>如果一个内联函数的定义很短小，你应该将其放在 .h 文件中。例如，accessors 和 mutators 应该始终放在类定义之内。更加复杂的内联函数也应该放在一个 .h 文件内以方便实现和调用，尽管这样会造成 .h 文件太笨重。解决方法就是将这些代码放入一个单独的 -inl.h 文件内，虽然会导致它与类的定义相分离，但仍然允许实现部分在必要的时候被包含。</p>
<p>-inl.h 文件的另一个用法是定义模板函数，它用来保持你的模板定义容易被读懂。</p>
<p>不要忘记像任何其它头文件那样，给 -inl.h 写一个 <a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-1.html" >#define 宏</a>。</p>
<h3>函数的参数顺序</h3>
<p>定义一个函数时，参数顺序应该是：先输入、后输出。</p>
<p>C/C++ 函数的参数既可以是输入，也可以是输出，或者二者皆是。输入参数通常是值或 const 引用，输出参数通常是 non-const 指针。当为函数的参数排序时，应把所有只作为输入的参数放在任意输出参数前面。尤其不要把新添加的参数放在函数的最后仅仅因为它们是新添加的；把新添加的输入参数放在输出参数之前。</p>
<p>这不是一条死规矩。那些同时既是输入又是输出的参数（经常是类/结构体）会把水搅浑。所以，遇到这样的函数时，你可以不完全遵守这一条。</p>
<h3>头文件名称的顺序</h3>
<p>按标准顺序使用头文件会增强代码的可读性，并可避免引入隐式对 C 库、C++ 库或其它库的、你的库的头文件的依赖。</p>
<p>一个工程的所有头文件都应该放入工程源代码目录下，而不要使用 UNIX 目录的快捷方式：.（当前目录）或 ..（上级目录）。例如：google-awesome-project/src/base/logging.h 应该被这样包含：</p>
<div class="syntax_hilite">
<div id="cpp-21">
<div class="cpp"><span style="color: #339900;">#include &quot;base/loggin.h&quot; </span></div>
</div>
</div>
<p></p>
<p>在一个主要目的是实现或测试 dir2/foo2.h 的 dir/foo.cc 文件中，将头文件按以下顺序包含：</p>
<ol>
<li>dir2/foo2.h（优先位置）</li>
<li>C 系统文件</li>
<li>C++ 系统文件</li>
<li>其它库的头文件</li>
<li>你的工程的头文件</li>
</ol>
<p>优先位置会减少隐式依赖。我们希望每个头文件被分别编译，达到这个目的的最简单途径就是保证它们是某个 .cc 文件中包含的第一个 .h 文件。</p>
<p>dir/foo.cc 和 dir2/foo2.h 经常在相同的目录下（例：base/basictypes_unittest.cc 和 base/basictypes.h），但是也可以是不同的目录。</p>
<p>最好在每个片段中按字母顺序包含头文件。</p>
<p>例：google-awesome-project/src/foo/internal/fooserver.cc 中包含的头文件看上去差不多是这个样子：</p>
<div class="syntax_hilite">
<div id="cpp-22">
<div class="cpp"><span style="color: #339900;">#include &quot;foo/public/fooserver.h&quot; // 优先位置</span></p>
<p><span style="color: #339900;">#include &lt;sys/types.h&gt;</span><br />
<span style="color: #339900;">#include &lt;unistd.h&gt;</span></p>
<p><span style="color: #339900;">#include &lt;hash_map&gt;</span><br />
<span style="color: #339900;">#include &lt;vector&gt;</span></p>
<p><span style="color: #339900;">#include &quot;base/basictypes.h&quot;</span><br />
<span style="color: #339900;">#include &quot;base/commandlineflags.h&quot;</span><br />
<span style="color: #339900;">#include &quot;foo/public/bar.h&quot; </span></div>
</div>
</div>
<p></p>
<div class="entry-related"><h3>Related Posts</h3><ul><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-1.html"  title="Google C++ 编程风格指南（1）：头文件（1）">Google C++ 编程风格指南（1）：头文件（1）</a></li><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-0-background.html"  title="Google C++ 编程风格指南（0）：背景">Google C++ 编程风格指南（0）：背景</a></li><li><a href="http://hellobmw.com/archives/traps-in-casting-float-to-dword.html"  title="float 转换 DWORD 的陷阱">float 转换 DWORD 的陷阱</a></li><li><a href="http://hellobmw.com/archives/alchemy-cc-to-as3-compiler.html"  title="神器 Alchemy！C/C++ 到 AS3 的编译器">神器 Alchemy！C/C++ 到 AS3 的编译器</a></li><li><a href="http://hellobmw.com/archives/why-put-constant-at-the-left-side-of-equal-to-operator.html"  title="[C/C++] 为什么把常量写在 == 运算符的左边">[C/C++] 为什么把常量写在 == 运算符的左边</a></li><li><a href="http://hellobmw.com/archives/protocol-buffers-performance.html"  title="Protocol Buffers 性能测试">Protocol Buffers 性能测试</a></li></ul></div>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2008.｜<a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-2.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-2.html#comments" >3 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>
]]></description>
			<content:encoded><![CDATA[<h3>Google C++ 编程风格指南（1）：头文件（2）</h3>
<ul>
<li>-inl.h 文件</li>
<li>函数的参数顺序</li>
<li>头文件名称的顺序</li>
</ul>
<h3>-inl.h 文件</h3>
<p>当需要的时候，你应该使用带有 -inl.h 后缀的名字来定义复杂的<a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-1.html" >内联函数</a>。</p>
<p>内联函数的定义需要放在一个头文件里，使得编译器在发生函数调用的地点展开函数定义。尽管如此，实现代码出现在 .cc 文件中更为合适，我们不想让 .h 文件拥有太多的代码，除非那样做可以提高可读性或是性能。</p>
<p>如果一个内联函数的定义很短小，你应该将其放在 .h 文件中。例如，accessors 和 mutators 应该始终放在类定义之内。更加复杂的内联函数也应该放在一个 .h 文件内以方便实现和调用，尽管这样会造成 .h 文件太笨重。解决方法就是将这些代码放入一个单独的 -inl.h 文件内，虽然会导致它与类的定义相分离，但仍然允许实现部分在必要的时候被包含。</p>
<p>-inl.h 文件的另一个用法是定义模板函数，它用来保持你的模板定义容易被读懂。</p>
<p>不要忘记像任何其它头文件那样，给 -inl.h 写一个 <a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-1.html" >#define 宏</a>。</p>
<h3>函数的参数顺序</h3>
<p>定义一个函数时，参数顺序应该是：先输入、后输出。</p>
<p>C/C++ 函数的参数既可以是输入，也可以是输出，或者二者皆是。输入参数通常是值或 const 引用，输出参数通常是 non-const 指针。当为函数的参数排序时，应把所有只作为输入的参数放在任意输出参数前面。尤其不要把新添加的参数放在函数的最后仅仅因为它们是新添加的；把新添加的输入参数放在输出参数之前。</p>
<p>这不是一条死规矩。那些同时既是输入又是输出的参数（经常是类/结构体）会把水搅浑。所以，遇到这样的函数时，你可以不完全遵守这一条。</p>
<h3>头文件名称的顺序</h3>
<p>按标准顺序使用头文件会增强代码的可读性，并可避免引入隐式对 C 库、C++ 库或其它库的、你的库的头文件的依赖。</p>
<p>一个工程的所有头文件都应该放入工程源代码目录下，而不要使用 UNIX 目录的快捷方式：.（当前目录）或 ..（上级目录）。例如：google-awesome-project/src/base/logging.h 应该被这样包含：</p>
<div class="syntax_hilite">
<div id="cpp-23">
<div class="cpp"><span style="color: #339900;">#include &quot;base/loggin.h&quot; </span></div>
</div>
</div>
<p></p>
<p>在一个主要目的是实现或测试 dir2/foo2.h 的 dir/foo.cc 文件中，将头文件按以下顺序包含：</p>
<ol>
<li>dir2/foo2.h（优先位置）</li>
<li>C 系统文件</li>
<li>C++ 系统文件</li>
<li>其它库的头文件</li>
<li>你的工程的头文件</li>
</ol>
<p>优先位置会减少隐式依赖。我们希望每个头文件被分别编译，达到这个目的的最简单途径就是保证它们是某个 .cc 文件中包含的第一个 .h 文件。</p>
<p>dir/foo.cc 和 dir2/foo2.h 经常在相同的目录下（例：base/basictypes_unittest.cc 和 base/basictypes.h），但是也可以是不同的目录。</p>
<p>最好在每个片段中按字母顺序包含头文件。</p>
<p>例：google-awesome-project/src/foo/internal/fooserver.cc 中包含的头文件看上去差不多是这个样子：</p>
<div class="syntax_hilite">
<div id="cpp-24">
<div class="cpp"><span style="color: #339900;">#include &quot;foo/public/fooserver.h&quot; // 优先位置</span></p>
<p><span style="color: #339900;">#include &lt;sys/types.h&gt;</span><br />
<span style="color: #339900;">#include &lt;unistd.h&gt;</span></p>
<p><span style="color: #339900;">#include &lt;hash_map&gt;</span><br />
<span style="color: #339900;">#include &lt;vector&gt;</span></p>
<p><span style="color: #339900;">#include &quot;base/basictypes.h&quot;</span><br />
<span style="color: #339900;">#include &quot;base/commandlineflags.h&quot;</span><br />
<span style="color: #339900;">#include &quot;foo/public/bar.h&quot; </span></div>
</div>
</div>
<p></p>
<div class="entry-related"><h3>Related Posts</h3><ul><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-1.html"  title="Google C++ 编程风格指南（1）：头文件（1）">Google C++ 编程风格指南（1）：头文件（1）</a></li><li><a href="http://hellobmw.com/archives/google-cpp-style-guide-0-background.html"  title="Google C++ 编程风格指南（0）：背景">Google C++ 编程风格指南（0）：背景</a></li><li><a href="http://hellobmw.com/archives/traps-in-casting-float-to-dword.html"  title="float 转换 DWORD 的陷阱">float 转换 DWORD 的陷阱</a></li><li><a href="http://hellobmw.com/archives/alchemy-cc-to-as3-compiler.html"  title="神器 Alchemy！C/C++ 到 AS3 的编译器">神器 Alchemy！C/C++ 到 AS3 的编译器</a></li><li><a href="http://hellobmw.com/archives/why-put-constant-at-the-left-side-of-equal-to-operator.html"  title="[C/C++] 为什么把常量写在 == 运算符的左边">[C/C++] 为什么把常量写在 == 运算符的左边</a></li><li><a href="http://hellobmw.com/archives/protocol-buffers-performance.html"  title="Protocol Buffers 性能测试">Protocol Buffers 性能测试</a></li></ul></div>	<hr noshade style="margin:0;height:1px" />
	<p><small>&copy; <a href="http://hellobmw.com" >HELLOBMW.COM</a>, 2008.｜<a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-2.html" >Permalink</a>｜<a href="http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-2.html#comments" >3 comments</a>｜Digital FingerPrint: 7b7911f00ecf2c9541e8c7ccd3cf7317</small></p>]]></content:encoded>
			<wfw:commentRss>http://hellobmw.com/archives/google-cpp-style-guide-1-header-files-2.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
