﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-首页原创精华区</title><link>http://www.cppblog.com/</link><description>专注于C++技术</description><language>zh-cn</language><lastBuildDate>Fri, 09 May 2008 23:41:41 GMT</lastBuildDate><pubDate>Fri, 09 May 2008 23:41:41 GMT</pubDate><ttl>60</ttl><item><title>玩了一下CEGUI，就想废掉现有的ui frame</title><link>http://www.cppblog.com/socketref/archive/2008/05/10/49389.html</link><dc:creator>放屁阿狗</dc:creator><author>放屁阿狗</author><pubDate>Fri, 09 May 2008 19:55:00 GMT</pubDate><guid>http://www.cppblog.com/socketref/archive/2008/05/10/49389.html</guid><wfw:comment>http://www.cppblog.com/socketref/comments/49389.html</wfw:comment><comments>http://www.cppblog.com/socketref/archive/2008/05/10/49389.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/socketref/comments/commentRss/49389.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/socketref/services/trackbacks/49389.html</trackback:ping><description><![CDATA[了解了一些CEGUI 基础的东西，觉得确实是个好东西，咋以前没早用她呢<br>cegui的widget一般都是texture绘制吧，也就是将 raster-image render到widget面。<br>之前正好把librsvg移植到wince上，看看是否能将svg扩展为cegui::drawable 对象, backend就采用 opengl<br><br>准备cegui到wince的移植工作<br><br><img src ="http://www.cppblog.com/socketref/aggbug/49389.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/socketref/" target="_blank">放屁阿狗</a> 2008-05-10 03:55 <a href="http://www.cppblog.com/socketref/archive/2008/05/10/49389.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何实现程序在应用程序菜单中隐藏（hide application from user's menu）</title><link>http://www.cppblog.com/franksunny/archive/2008/05/09/49363.html</link><dc:creator>frank.sunny</dc:creator><author>frank.sunny</author><pubDate>Fri, 09 May 2008 14:48:00 GMT</pubDate><guid>http://www.cppblog.com/franksunny/archive/2008/05/09/49363.html</guid><wfw:comment>http://www.cppblog.com/franksunny/comments/49363.html</wfw:comment><comments>http://www.cppblog.com/franksunny/archive/2008/05/09/49363.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/franksunny/comments/commentRss/49363.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/franksunny/services/trackbacks/49363.html</trackback:ping><description><![CDATA[<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">实现应用程序的图标隐藏，</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">2<sup>nd</sup></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">和</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">S60</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">的</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">3rd</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">差别很大，相对来说</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">3rd</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">因为有一个</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">[appname]_reg.rss</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">文件，所以显得很简单，默认的在</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">APP_REGISTRATION_INFO</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">中有一个属性值：</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">BYTE hidden = KAppNotHidden;<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">我们要实现图标隐藏，只需将其值赋为</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">KAppIsHidden</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">即可。具体示例代码如下：</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">RESOURCE APP_REGISTRATION_INFO<o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">{<o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><span style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>app_file="Hello_Hide_app_0xEC<st1:chmetcnv w:st="on" UnitName="F" SourceValue="12" HasSpace="False" Negative="False" NumberType="1" TCSC="0">12F</st1:chmetcnv>4E3";<o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><span style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>localisable_resource_file =<span style="mso-spacerun: yes">&nbsp; </span>qtn_loc_resource_file_1;<o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><span style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>localisable_resource_id = R_LOCALISABLE_APP_INFO;<o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><span style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="COLOR: red">hidden = KAppIsHidden;<o:p></o:p></span></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><span style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>embeddability=KAppNotEmbeddable;<o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><span style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>newfile=KAppDoesNotSupportNewFile;<o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">}<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p>&nbsp;</o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">在</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">2nd</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">版本中显得略微复杂些，具体实现如下（本人尚未测试过）：</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">I installed the application without name (.app only) or in a folder out of \system\apps\&lt;myapp&gt;\, for example, c:\system\data. In that way the app was not in the list.<o:p></o:p></span></p>
<img src ="http://www.cppblog.com/franksunny/aggbug/49363.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/franksunny/" target="_blank">frank.sunny</a> 2008-05-09 22:48 <a href="http://www.cppblog.com/franksunny/archive/2008/05/09/49363.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何在第三版上实现开机自启动</title><link>http://www.cppblog.com/franksunny/archive/2008/05/09/49365.html</link><dc:creator>frank.sunny</dc:creator><author>frank.sunny</author><pubDate>Fri, 09 May 2008 14:48:00 GMT</pubDate><guid>http://www.cppblog.com/franksunny/archive/2008/05/09/49365.html</guid><wfw:comment>http://www.cppblog.com/franksunny/comments/49365.html</wfw:comment><comments>http://www.cppblog.com/franksunny/archive/2008/05/09/49365.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/franksunny/comments/commentRss/49365.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/franksunny/services/trackbacks/49365.html</trackback:ping><description><![CDATA[<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">第二版的开机自启动比较麻烦，需要涉及到创建</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">mdl</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">文件并且需要在</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">mdl</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">中将另一程序开启，所以略过。至于第三版的开机自启动相对来说更加简单些：</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">假设你的应用</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">ID</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">为：</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">ef37946b<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">1)</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">在</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">data</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">下新建一个文件，</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt"> [ef37946b].rss</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">（注意加上</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">[]</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">）文件具体代码如下</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">#include &lt;startupitem.rh&gt;<o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">RESOURCE STARTUP_ITEM_INFO dispatcher<o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">{<o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </span>executable_name = "!:\\sys\\bin\\AutoStart.exe"; <o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </span>recovery = EStartupItemExPolicyNone;<o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">}<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">此处的</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">AutoStart.exe</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">是你的应用程序文件名。</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">注：笔者试图通过修改此处为其他应用程序名从而启动指定其他程序，但是没有成功。</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">2)</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">在</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">mmp</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">文件中增加以下代码</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">START RESOURCE [ef37946b].rss <o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">TARGETPATH<span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>\private\<st1:chmetcnv w:st="on" UnitName="F" SourceValue="101" HasSpace="False" Negative="False" NumberType="1" TCSC="0">101f</st1:chmetcnv><st1:chmetcnv w:st="on" UnitName="a" SourceValue="875" HasSpace="False" Negative="False" NumberType="1" TCSC="0">875a</st1:chmetcnv>\import <o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">HEADER<span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">END<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">确保：</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">LANG<span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>SC<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">CAPABILITY<span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>ReadUserData<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">注意&#8220;</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">\private\<st1:chmetcnv w:st="on" UnitName="F" SourceValue="101" HasSpace="False" Negative="False" NumberType="1" TCSC="0">101f</st1:chmetcnv>875a\import</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">&#8221;不能够变。</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">3)</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">在</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">pkg</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">文件中增加以下代码</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">"$(EPOCROOT)epoc32\data\z\private\<st1:chmetcnv w:st="on" UnitName="F" SourceValue="101" HasSpace="False" Negative="False" NumberType="1" TCSC="0">101f</st1:chmetcnv><st1:chmetcnv w:st="on" UnitName="a" SourceValue="875" HasSpace="False" Negative="False" NumberType="1" TCSC="0">875a</st1:chmetcnv>\import[ef37946b].rSC"-"!:\private\<st1:chmetcnv w:st="on" UnitName="F" SourceValue="101" HasSpace="False" Negative="False" NumberType="1" TCSC="0">101f</st1:chmetcnv><st1:chmetcnv w:st="on" UnitName="a" SourceValue="875" HasSpace="False" Negative="False" NumberType="1" TCSC="0">875a</st1:chmetcnv>\import\[ef<st1:chmetcnv w:st="on" UnitName="a" SourceValue="37946" HasSpace="False" Negative="False" NumberType="1" TCSC="0">37946a</st1:chmetcnv>].rSC"<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p>&nbsp;</o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">如果是采用</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">carbide c++</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">编译，那么使用上述代码就可以了。</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">如果是使用</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">makesis</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">命令行打包或者使用</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt">.Net</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">编译，那么你需要修改成绝对路径，路径名视你的安装目录而定。</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">例如：</span><span lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="mso-bidi-font-size: 10.5pt">"C:\Symbian\9.1\S60_3rd_MR\Epoc32\Data\z\private\<st1:chmetcnv w:st="on" UnitName="F" SourceValue="101" HasSpace="False" Negative="False" NumberType="1" TCSC="0">101f</st1:chmetcnv><st1:chmetcnv w:st="on" UnitName="a" SourceValue="875" HasSpace="False" Negative="False" NumberType="1" TCSC="0">875a</st1:chmetcnv>\import[ef37946b].rSC"-"!:\private\<st1:chmetcnv w:st="on" UnitName="F" SourceValue="101" HasSpace="False" Negative="False" NumberType="1" TCSC="0">101f</st1:chmetcnv><st1:chmetcnv w:st="on" UnitName="a" SourceValue="875" HasSpace="False" Negative="False" NumberType="1" TCSC="0">875a</st1:chmetcnv>\import\[ef<st1:chmetcnv w:st="on" UnitName="a" SourceValue="37946" HasSpace="False" Negative="False" NumberType="1" TCSC="0">37946a</st1:chmetcnv>].rSC"<o:p></o:p></span></p>
<img src ="http://www.cppblog.com/franksunny/aggbug/49365.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/franksunny/" target="_blank">frank.sunny</a> 2008-05-09 22:48 <a href="http://www.cppblog.com/franksunny/archive/2008/05/09/49365.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何实现程序的前后台运行切换</title><link>http://www.cppblog.com/franksunny/archive/2008/05/09/49362.html</link><dc:creator>frank.sunny</dc:creator><author>frank.sunny</author><pubDate>Fri, 09 May 2008 14:45:00 GMT</pubDate><guid>http://www.cppblog.com/franksunny/archive/2008/05/09/49362.html</guid><wfw:comment>http://www.cppblog.com/franksunny/comments/49362.html</wfw:comment><comments>http://www.cppblog.com/franksunny/archive/2008/05/09/49362.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/franksunny/comments/commentRss/49362.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/franksunny/services/trackbacks/49362.html</trackback:ping><description><![CDATA[<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">首先，需要使程序有获知焦点变化的能力。具体通过在</span><span lang=EN-US>AppUI</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类中重载</span><span lang=EN-US>CAknAppUi:: HandleForegroundEventL(TBool aForeground )</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数来实现。</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">其次，在获知焦点变化的同时，改变应用程序的焦点，通过</span><span lang=EN-US>TApaTask::SendToBackground()</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span><span lang=EN-US>TApaTask::BringToForeground()</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">两个函数来实现。由于这里用到的</span><span lang=EN-US>TApaTask</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类，需要包含</span><span lang=EN-US>APGTASK.H</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span><span lang=EN-US>apgrfx.lib</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">再次，因为需要在调用其上函数时，必须用我们的应用程序的窗口组</span><span lang=EN-US>id(window<span style="mso-spacerun: yes">&nbsp; </span>group<span style="mso-spacerun: yes">&nbsp; </span>id)</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">初始化</span><span lang=EN-US>(Initialise) TApaTask</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这个对象，这个实现需要用到，获取当前应用程序窗口组</span><span lang=EN-US>id</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的函数</span><span lang=EN-US>CEikonEnv::Static()-&gt;RootWin().Identifier()</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。刚好以上函数又要包含</span><span lang=EN-US>w32std.h</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span><span lang=EN-US>w32.lib</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: -21pt; mso-list: l0 level1 lfo1; tab-stops: list 42.0pt"><span lang=EN-US style="FONT-FAMILY: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings"><span style="mso-list: Ignore">l<span style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">具体实现代码如下：</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US>void CHelloUIAppUi::HandleForegroundEventL(TBool<span style="mso-spacerun: yes">&nbsp; </span>aForeground)</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US>{</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><span style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>if(aForeground)</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><span style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><span style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>TApaTask task ( CEikonEnv::Static()-&gt;WsSession() );</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><span style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>task.SetWgId( CEikonEnv::Static()-&gt;RootWin().Identifier() );</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><span style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>//Foreground run </span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><span style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>task.BringToForeground();</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><span style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>ActivateLocalViewL(iHelloUIContainerView-&gt;Id());</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><span style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><span style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>else</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><span style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><span style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>TApaTask task ( CEikonEnv::Static()-&gt;WsSession() );</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><span style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>task.SetWgId( CEikonEnv::Static()-&gt;RootWin().Identifier() );</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><span style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>//background run </span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><span style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>task.SendToBackground();</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><span style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class=MsoNormal style="BACKGROUND: #e0e0e0; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US>}</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><o:p>&nbsp;</o:p></span></p>
<img src ="http://www.cppblog.com/franksunny/aggbug/49362.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/franksunny/" target="_blank">frank.sunny</a> 2008-05-09 22:45 <a href="http://www.cppblog.com/franksunny/archive/2008/05/09/49362.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>tcp要点学习-基础概念</title><link>http://www.cppblog.com/kevinlynx/archive/2008/05/09/49322.html</link><dc:creator>Kevin Lynx</dc:creator><author>Kevin Lynx</author><pubDate>Fri, 09 May 2008 08:30:00 GMT</pubDate><guid>http://www.cppblog.com/kevinlynx/archive/2008/05/09/49322.html</guid><wfw:comment>http://www.cppblog.com/kevinlynx/comments/49322.html</wfw:comment><comments>http://www.cppblog.com/kevinlynx/archive/2008/05/09/49322.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/kevinlynx/comments/commentRss/49322.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/kevinlynx/services/trackbacks/49322.html</trackback:ping><description><![CDATA[<p style="FONT-SIZE: 10pt">&nbsp;
<p style="FONT-SIZE: 10pt">TCP是TCP/IP协议簇中传输层上的一种网络协议，它是一种面向连接的、可靠的协议。为了提供这种可靠性，<br>TCP实现了各种有效的机制、算法。为了从一种宏观的角度去了解这个协议，这里先大致地提一下与之相关<br>的概念。
<p style="FONT-SIZE: 10pt"><strong>1. 什么是&#8216;面向连接的&#8217;？</strong><br>&nbsp;&nbsp; 引用&lt;TCP/IP协议详解&gt;中的概念：<br>&nbsp;&nbsp; 面向连接意味着两个使用TCP的应用（通常是一个客户和一个服务器）在彼此交换数据之前必须先建立<br>&nbsp;&nbsp; 一个TCP连接。
<p style="FONT-SIZE: 10pt"><strong>2. 什么是&#8216;三次握手&#8217;？<br></strong>&nbsp;&nbsp; 在建立TCP连接之前，两个使用TCP的应用需要交换三次网络数据。这三个数据包的来往也就是所谓的&#8216;<br>&nbsp;&nbsp; 三次握手&#8217;。
<p style="FONT-SIZE: 10pt"><strong>3. 报文段segment</strong><br>&nbsp;&nbsp; 我们说TCP是流式的网络协议，那是因为，应用程序可以一直往TCP写数据，无论你是逐byte，还是write<br>&nbsp;&nbsp; a chunk，TCP对应用传给它的数据进行缓冲，直到缓冲数据达到一定尺寸才发送。可以看出，对于应用<br>&nbsp;&nbsp; 而言，TCP就像是stream的。但事实上，在TCP层，数据还是以块为单位的。这个块也就是所谓的报文段<br>&nbsp;&nbsp; segment。
<p style="FONT-SIZE: 10pt"><strong>4. 什么是MTU？<br></strong>&nbsp;&nbsp; MTU即最大传输单元（Maximum Transmission Unit，MTU）是指一种通信协议的某一层上面所能通过的<br>&nbsp;&nbsp; 大数据报大小（以字节为单位）。我个人目前的理解认为，MTU是一个网络在硬件层次上所允许的最大<br>&nbsp;&nbsp; 数据包大小，例如以太网大概是1500字节。
<p style="FONT-SIZE: 10pt"><strong>5. 什么是MSS？</strong><br>&nbsp;&nbsp; MSS即最大报文段大小（Maximum Segment Size），它是指TCP中一个报文段上附加的用户数据的最大大小。<br>&nbsp;&nbsp; 这里稍微说下应用层发送某个数据包时整个TCP/IP协议栈的操作过程：应用层将自己的用户数据传给TCP<br>&nbsp;&nbsp; 层（传输层），TCP在这些数据前添加自己的协议头（简单地理解为附加一些数据），然后将数据交给<br>&nbsp;&nbsp; IP层（网络层），IP层附加自己的协议头，以此类推。<br>&nbsp;&nbsp; 虽然MSS意思是最大报文段大小，但事实上它是排除了协议头的用户数据。
<p style="FONT-SIZE: 10pt"><strong>6. MTU and MSS ?</strong><br>&nbsp;&nbsp; 可以简单地给你一个这样的公示：mss = mtu - tcp_header_size - ip_header_size。<br>&nbsp;&nbsp; 而通常，IP协议附加的协议头大小和TCP的协议头大小都是20字节，所以通常的MSS为1460字节。<br>&nbsp;&nbsp; 注意，这里说的数字并不见得正确，因为MSS是可以被协商的。各种协议头也可能被添加附加数据，但是<br>&nbsp;&nbsp; 他们的关系是这样的。
<p style="FONT-SIZE: 10pt"><strong>7. 什么是窗口大小？</strong><br>&nbsp;&nbsp; 找本TCP的书看下TCP数据包的包头（本文多次使用数据包、报文的概念，我这里说的都是一样的），你会<br>&nbsp;&nbsp; 发现那个16位的窗口大小。<br>&nbsp;&nbsp; 窗口这个域对于整个TCP协议都很重要。简单地说，窗口大小是指接收端的接收缓存的大小。上面说了，应用<br>&nbsp;&nbsp; 在发数据的时候，TCP会缓存这些数据，稍后发送。接收数据时也一样，TCP接收数据并缓存起来，直到应用<br>&nbsp;&nbsp; 调用recv之类的函数取数据时，TCP才将这些缓存数据清除。
<p style="FONT-SIZE: 10pt">&nbsp;&nbsp; TCP发送端会根据TCP接收端那个接收缓存大小决定发送多少数据（如何知道这个缓存大小？稍后给概念）。<br>&nbsp;&nbsp; 这样，TCP接收端的接收缓存才不至于缓冲溢出。
<p style="FONT-SIZE: 10pt"><strong>8. 提供可靠性的方法之一：ACK确认？<br></strong>&nbsp;&nbsp; 这里还不敢提序号、确认号、延时ACK等乱七八糟的东西。我只能告诉你，当TCP发送某些数据给TCP接收方<br>&nbsp;&nbsp; 时，TCP接收方会发回一个确认报文。TCP发送方收到这个确认报文后，就可以确认刚才发送的数据包成功到达。
<p style="FONT-SIZE: 10pt">&nbsp;&nbsp; 为什么这个确认报文叫ACK确认（貌似是我临时给的概念:D）？再翻到TCP包头结构那张图，ACK是TCP包头中<br>&nbsp;&nbsp; 的1bit标志位，如同SYN、PSH、RST之类的标志一样，这些标志都有一个专有的用途。当ACK标志位被设置为1<br>&nbsp;&nbsp; 时，我就称其为ACK确认标志，因为ACK就是用于确认报文段的。
<p style="FONT-SIZE: 10pt">&nbsp;&nbsp; 在上面所说的窗口大小中，我提到，发送方如何知道接收方的接收缓存大小呢？这也是通过确认报文段实现：<br>&nbsp;&nbsp; 当接收方接收到数据后，发送ACK确认数据包给发送方，就设置包头中的窗口域。
<p style="FONT-SIZE: 10pt"><strong>9. 提供可靠性的方法之二：各种定时器</strong><br>&nbsp;&nbsp; TCP中会设置很多计时器，这些定时器大多用于超时重传（老半天得不到回应，所以重传数据）。
<p style="FONT-SIZE: 10pt"><strong>10.什么是全双工？</strong><br>&nbsp;&nbsp; 全双工就是你可以同时在一个TCP连接上进行数据的发送和接收。这种双工特性也促使了关闭TCP连接时的四次<br>&nbsp;&nbsp; 握手。
<p style="FONT-SIZE: 10pt"><strong>11.TODO : more concepts...</strong></p>
<p style="FONT-SIZE: 10pt"><strong></strong><br>这里我尽量简单地介绍一些TCP中的概念，希望可以让你有概括性的了解。预计下一节我会讲讲建立TCP连接的相关细节。<br>除了Stevens的&lt;TCP/IP详解&gt;，我推荐&lt;<a href="http://www.tcpipguide.com/index.htm">The TCP/IP Guide</a>&gt;，据说是另一部TCP的权威之作。 </p>
<img src ="http://www.cppblog.com/kevinlynx/aggbug/49322.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/kevinlynx/" target="_blank">Kevin Lynx</a> 2008-05-09 16:30 <a href="http://www.cppblog.com/kevinlynx/archive/2008/05/09/49322.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用fstream对二进制文件的读写</title><link>http://www.cppblog.com/gohan/archive/2008/05/09/49303.html</link><dc:creator>Gohan</dc:creator><author>Gohan</author><pubDate>Fri, 09 May 2008 06:41:00 GMT</pubDate><guid>http://www.cppblog.com/gohan/archive/2008/05/09/49303.html</guid><wfw:comment>http://www.cppblog.com/gohan/comments/49303.html</wfw:comment><comments>http://www.cppblog.com/gohan/archive/2008/05/09/49303.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/gohan/comments/commentRss/49303.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/gohan/services/trackbacks/49303.html</trackback:ping><description><![CDATA[<p>这里介绍使用fstream这个类完成这个任务，fstream在输入输出方面比较全能。 </p>
<p>操作系统通过二进制文件格式存储大量文件。一般不指定二进制文件操作的I/O操作是面向文本的，用来读写特定编码的文本。本文介绍用C++的fstream类读写二进制文件。 </p>
<p>读写数据以这个WebSite结构体为例  </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Struct for C++ File I/O binary file sample</p>
<div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; font-size: 8pt; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060;">   1:</span> <span style="color: #0000ff;">struct</span> WebSites</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060;">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060;">   3:</span>      <span style="color: #0000ff;">char</span> SiteName[100];</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060;">   4:</span>      <span style="color: #0000ff;">int</span> Rank;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060;">   5:</span> };</pre>
</div>
</div>
<h4>写操作</h4>
<p>&nbsp;&nbsp;&nbsp; 注意事项
</p>
<ul>
    <li>要写的文件需要用到输出模式标志 ios::out和二进制操作模式标志ios::binary。
    </li>
    <li>write()函数需要两个参数.第一个参数是char*类型用来指定需要写入的数据， 第二个参数是int类型指定写入多少个字节.
    </li>
    <li>最后记得要用close()结束. </li>
</ul>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Sample for C++ File I/O binary file write </p>
<div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; font-size: 8pt; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060;">   1:</span> <span style="color: #0000ff;">void</span> write_to_binary_file(WebSites p_Data)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060;">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060;">   3:</span>     fstream binary_file(<span style="color: #006080;">"c:\\test.dat"</span>,ios::<span style="color: #0000ff;">out</span>|ios::binary|ios::app); </pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060;">   4:</span>     binary_file.write(reinterpret_cast&lt;<span style="color: #0000ff;">char</span> *&gt;(&amp;p_Data),<span style="color: #0000ff;">sizeof</span>(WebSites));</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060;">   5:</span>     binary_file.close();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060;">   6:</span> } </pre>
</div>
</div>
<p>上面的例子把一个WebSites的对象追加进了c:\test.dat之中，ios::app是追加方式操作文件的标志。
</p>
<p>上面的write函数，需要第一个参数为char*类型，所以用 reinterpret_cast转换将这个对象地址转换成char*类型.
</p>
<p>读操作
</p>
<p>&nbsp;&nbsp; 跟上面的操作流程类似. 唯一不同在于使用输入模式标志ios::in, 使用read()方法.
</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Sample for C++ File I/O binary file read </p>
<div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; font-size: 8pt; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060;">   1:</span> <span style="color: #0000ff;">void</span> read_from_binary_file()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060;">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060;">   3:</span>     WebSites p_Data;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060;">   4:</span>     fstream binary_file(<span style="color: #006080;">"c:\\test.dat"</span>,ios::binary|ios::<span style="color: #0000ff;">in</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060;">   5:</span>     binary_file.read(reinterpret_cast&lt;<span style="color: #0000ff;">char</span> *&gt;(&amp;p_Data),<span style="color: #0000ff;">sizeof</span>(WebSites));</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060;">   6:</span>     binary_file.close();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060;">   7:</span>     cout&lt;&lt;p_Data.SiteName&lt;&lt;endl;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060;">   8:</span>     cout&lt;&lt;<span style="color: #006080;">"Rank :"</span>&lt;&lt; p_Data.Rank&lt;&lt;endl;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060;">   9:</span> } </pre>
</div>
</div>
<p>本文只是关于文件io流的一些基础介绍，一些高级操作比如seek，检查文件指针的有效性等等，也是需要学习的，这里就不多说了。
</p>
<p><a href="http://www.codersource.net/cpp_file_io_binary.html" target="_blank">文章来源</a>&nbsp; 我只是做一个简短的翻译</p><img src ="http://www.cppblog.com/gohan/aggbug/49303.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/gohan/" target="_blank">Gohan</a> 2008-05-09 14:41 <a href="http://www.cppblog.com/gohan/archive/2008/05/09/49303.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>08年05月08日</title><link>http://www.cppblog.com/richardhe/archive/2008/05/08/49222.html</link><dc:creator>RichardHe</dc:creator><author>RichardHe</author><pubDate>Thu, 08 May 2008 08:30:00 GMT</pubDate><guid>http://www.cppblog.com/richardhe/archive/2008/05/08/49222.html</guid><wfw:comment>http://www.cppblog.com/richardhe/comments/49222.html</wfw:comment><comments>http://www.cppblog.com/richardhe/archive/2008/05/08/49222.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/richardhe/comments/commentRss/49222.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/richardhe/services/trackbacks/49222.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: CEGUI有背景图片的BUTTON:<br>昨天听到刀哥问我对Looknfeel写东西有没有什么心德体会.说实话,我还真正的写过一个wideget.然后突然就有一个冲动想法.很多东西不写是不会熟悉的.我就参考刀哥的方法写了一个BUTTON<br>有两种方法可以实现,一为在Looknfeel文件中直接修改&nbsp;&nbsp;<a href='http://www.cppblog.com/richardhe/archive/2008/05/08/49222.html'>阅读全文</a><img src ="http://www.cppblog.com/richardhe/aggbug/49222.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/richardhe/" target="_blank">RichardHe</a> 2008-05-08 16:30 <a href="http://www.cppblog.com/richardhe/archive/2008/05/08/49222.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>正则表达式——一点小插曲</title><link>http://www.cppblog.com/vczh/archive/2008/05/07/49158.html</link><dc:creator>陈梓瀚(vczh)</dc:creator><author>陈梓瀚(vczh)</author><pubDate>Wed, 07 May 2008 13:21:00 GMT</pubDate><guid>http://www.cppblog.com/vczh/archive/2008/05/07/49158.html</guid><wfw:comment>http://www.cppblog.com/vczh/comments/49158.html</wfw:comment><comments>http://www.cppblog.com/vczh/archive/2008/05/07/49158.html#Feedback</comments><slash:comments>9</slash:comments><wfw:commentRss>http://www.cppblog.com/vczh/comments/commentRss/49158.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/vczh/services/trackbacks/49158.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要:     有个同学近来一直在做一个魔兽世界战况分析（名字好像叫DeusCraft），说是很火。只是用C#觉得不是很爽，想移植到C++上面来。但是那个东西在分析的时候用了好多正则表达式，于是只好找了些正则表达式引擎来测。<br><br>    测试的文件一共有27万多行，首先通过一个检查时间的正则表达式。如果成功，则在接下来的20几条正则表达式中验证字符串命中哪一条，然后开始做剩余的工作。原先在C#上花了12秒分析，后来换了boost的正则表达式花费40秒，然后从MSR上找了一个号称比boost快4倍的正则表达式引擎，结果还是40秒（都是微软的，咋差距这么大……）。于是同学用他自己做的正则表达式引擎花了23秒（此数据不太记得），我用我以前那个东西花费108秒（-_-|||）。<br><br>    于是我们这几天就在优化正则表达式引擎，到了今天同学那个花费13秒，我那个12秒。Visual Studio 2008 Team System上有一个Performance Wizard，用于在程序执行的过程中统计各个函数所占用的时间，可以方便定位，看出效率瓶颈，非常好用。<br><b&nbsp;&nbsp;<a href='http://www.cppblog.com/vczh/archive/2008/05/07/49158.html'>阅读全文</a><img src ="http://www.cppblog.com/vczh/aggbug/49158.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/vczh/" target="_blank">陈梓瀚(vczh)</a> 2008-05-07 21:21 <a href="http://www.cppblog.com/vczh/archive/2008/05/07/49158.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>动态规划算法</title><link>http://www.cppblog.com/Fox/archive/2008/05/07/dynamic_programming.html</link><dc:creator>Fox</dc:creator><author>Fox</author><pubDate>Wed, 07 May 2008 12:43:00 GMT</pubDate><guid>http://www.cppblog.com/Fox/archive/2008/05/07/dynamic_programming.html</guid><wfw:comment>http://www.cppblog.com/Fox/comments/49153.html</wfw:comment><comments>http://www.cppblog.com/Fox/archive/2008/05/07/dynamic_programming.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/Fox/comments/commentRss/49153.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Fox/services/trackbacks/49153.html</trackback:ping><description><![CDATA[<p>以前在学习非数值算法的时候，曾经了解过<a href="http://en.wikipedia.org/wiki/Dynamic_programming" target="_blank">动态规划算法（Dynamic programming）</a>，以下是对<a href="http://en.wikipedia.org/wiki/Main_Page" target="_blank">Wikipedia</a>上<a href="http://en.wikipedia.org/wiki/Dynamic_programming" target="_blank">动态规划</a>的翻译，图也是<a href="http://en.wikipedia.org/wiki/Main_Page" target="_blank">Wikipedia</a>上的，仓促行文，不到之处，请方家指正。</p> <p>这篇文章的术语实在是太多了，所以我在文中加入了少量注释，一律以<em><strong>粗斜体</strong></em>注明。</p> <p>本文的不足之处将<strong>随时修正</strong>，MIT的《<a href="http://mitpress.mit.edu/algorithms/" target="_blank">Introduction to Algorithms</a>》第15章是专门讲动态规划的。</p> <p>_____________________________________________________________</p> <p align="center"><strong><a href="http://en.wikipedia.org/wiki/Dynamic_programming" target="_blank">动态规划</a></strong></p> <p>在数学与计算机科学领域，<strong>动态规划</strong>用于解决那些可分解为<a href="http://en.wikipedia.org/wiki/Overlapping_subproblem" target="_blank">重复子问题</a>（overlapping subproblems，<em><strong>想想递归求阶乘吧</strong></em>）并具有<a href="http://en.wikipedia.org/wiki/Optimal_substructure" target="_blank">最优子结构</a>（optimal substructure，<strong><em>想想最短路径算法</em></strong>）（如下所述）的问题，动态规划比通常算法花费更少时间。  <p>上世纪40年代，<a href="http://en.wikipedia.org/wiki/Richard_Bellman" target="_blank">Richard Bellman</a>最早使用动态规划这一概念表述通过遍历寻找最优决策解问题的求解过程。1953年，Richard Bellman将动态规划<a href="http://www.wu-wien.ac.at/usr/h99c/h9951826/bellman_dynprog.pdf" target="_blank">赋予现代意义</a>，该领域被IEEE纳入系统分析和工程中。为纪念Bellman的贡献，动态规划的核心方程被命名为<a href="http://en.wikipedia.org/wiki/Bellman_equation" target="_blank">贝尔曼方程</a>，该方程以<a href="http://en.wikipedia.org/wiki/Recursion" target="_blank">递归</a>形式重申了一个优化问题。  <p>在“动态规划”（dynamic programming）一词中，programming与“计算机编程”（computer programming）中的programming并无关联，而是来自“<a href="http://en.wikipedia.org/wiki/Mathematical_programming" target="_blank">数学规划</a>”（mathematical programming），也称优化。因此，规划是指对生成活动的优化策略。举个例子，编制一场展览的日程可称为规划。 在此意义上，规划意味着找到一个可行的活动计划。  <ul> <li> <div align="left"><strong>概述</strong></div></li></ul> <p><img style="margin: 10px" src="http://upload.wikimedia.org/wikipedia/en/4/42/Shortest_path_optimal_substructure.png" align="left">  <p><strong></strong>&nbsp; <p><strong>图1</strong> 使用最优子结构寻找最短路径：直线表示边，波状线表示两顶点间的最短路径（路径中其他节点未显示）；粗线表示从起点到终点的最短路径。  <p><strong><em>不难看出，start到goal的最短路径由start的相邻节点到goal的最短路径及start到其相邻节点的成本决定。</em></strong>  <p><strong><em></em></strong>&nbsp; <p>&nbsp; <p>最优子结构即可用来寻找整个问题最优解的子问题的最优解。举例来说，寻找<a href="http://en.wikipedia.org/wiki/Graph_%28mathematics%29" target="_blank">图</a>上某顶点到终点的<a href="http://en.wikipedia.org/wiki/Shortest_path_problem" target="_blank">最短路径</a>，可先计算该顶点所有相邻顶点至终点的最短路径，然后以此来选择最佳整体路径，如<strong>图1</strong>所示。  <p>一般而言，最优子结构通过如下三个步骤解决问题：  <p>a) 将问题分解成较小的子问题；  <p>b) 通过递归使用这三个步骤求出子问题的最优解；  <p>c) 使用这些最优解构造初始问题的最优解。  <p>子问题的求解是通过不断划分为更小的子问题实现的，直至我们可以在常数时间内求解。  <p><img style="margin: 10px" src="http://upload.wikimedia.org/wikipedia/commons/thumb/0/06/Fibonacci_dynamic_programming.svg/108px-Fibonacci_dynamic_programming.svg.png" align="left">  <p><strong></strong>&nbsp; <p><strong></strong>&nbsp; <p><strong>图2</strong> Fibonacci序列的子问题示意图：使用<a href="http://en.wikipedia.org/wiki/Directed_acyclic_graph" target="_blank">有向无环图</a>（DAG, directed acyclic graph）而非<a href="http://en.wikipedia.org/wiki/Tree_structure" target="_blank">树</a>表示重复子问题的分解。  <p><strong><em>为什么是DAG而不是树呢？答案就是，如果是树的话，会有很多重复计算，下面有相关的解释。</em></strong>  <p>&nbsp; <p>&nbsp; <p>一个问题可划分为重复子问题是指通过相同的子问题可以解决不同的较大问题。例如，在Fibonacci序列中，F3 = F1 + F2和F4 = F2 + F3都包含计算F2。由于计算F5需要计算F3和F4，一个比较笨的计算F5的方法可能会重复计算F2两次甚至两次以上。这一点对所有重复子问题都适用：愚蠢的做法可能会为重复计算已经解决的最优子问题的解而浪费时间。  <p>为避免重复计算，可将已经得到的子问题的解保存起来，当我们要解决相同的子问题时，重用即可。该方法即所谓的<a href="http://en.wikipedia.org/wiki/Memoization" target="_blank">缓存</a>（memoization，而不是存储memorization，虽然这个词亦适合，<strong><em>姑且这么叫吧，这个单词太难翻译了，简直就是可意会不可言传，其意义是没计算过则计算，计算过则保存</em></strong>）。当我们确信将不会再需要某一解时，可以将其抛弃，以节省空间。在某些情况下，我们甚至可以提前计算出那些将来会用到的子问题的解。  <p>总括而言，动态规划利用：  <p>1) <a href="http://en.wikipedia.org/wiki/Overlapping_subproblem" target="_blank">重复子问题</a>  <p>2) <a href="http://en.wikipedia.org/wiki/Optimal_substructure" target="_blank">最优子结构</a>  <p>3) <a href="http://en.wikipedia.org/wiki/Memoization" target="_blank">缓存</a>  <p>动态规划通常采用以下两种方式中的一种两个办法：  <p><a href="http://en.wikipedia.org/wiki/Top-down" target="_blank">自顶向下</a>：将问题划分为若干子问题，求解这些子问题并保存结果以免重复计算。该方法将递归和缓存结合在一起。  <p><a href="http://en.wikipedia.org/wiki/Bottom-up" target="_blank">自下而上</a>：先行求解所有可能用到的子问题，然后用其构造更大问题的解。该方法在节省堆栈空间和减少函数调用数量上略有优势，但有时想找出给定问题的所有子问题并不那么直观。  <p>为了提高<a href="http://en.wikipedia.org/wiki/Call-by-name" target="_blank">按名传递</a>（call-by-name，这一机制与<a href="http://en.wikipedia.org/wiki/Call-by-need" target="_blank">按需传递</a>call-by-need相关，<strong><em>复习一下参数传递的各种规则吧，简单说一下，按名传递允许改变实参值</em></strong>）的效率，一些编程语言将函数的返回值“自动”缓存在函数的特定参数集合中。一些语言将这一特性尽可能简化（如<a href="http://en.wikipedia.org/wiki/Scheme_%28programming_language%29" target="_blank">Scheme</a>、<a href="http://en.wikipedia.org/wiki/Common_Lisp" target="_blank">Common Lisp</a>和<a href="http://en.wikipedia.org/wiki/Perl" target="_blank">Perl</a>），也有一些语言需要进行特殊扩展（如C++，<strong><em>C++中使用的是按值传递和按引用传递，因此C++中本无自动缓存机制，需自行实现，具体实现的一个例子是<a href="http://www.apl.jhu.edu/~paulmac/c++-memoization.html" target="_blank">Automated Memoization in C++</a></em></strong>）。无论如何，只有<a href="http://en.wikipedia.org/wiki/Referential_transparency_%28computer_science%29" target="_blank">指称透明</a>（referentially transparent，<strong><em>指称透明是指在程序中使用表达式、函数本身或以其值替换对程序结果没有任何影响</em></strong>）函数才具有这一特性。  <ul> <li><strong>例子</strong> </li></ul> <p><strong>1. Fibonacci序列</strong>  <p>寻找<a href="http://en.wikipedia.org/wiki/Fibonacci_sequence" target="_blank">Fibonacci序列</a>中第n个数，基于其数学定义的直接实现：  <p>&nbsp;&nbsp; function fib(n)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if n = 0 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if n = 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return fib(n-1) + fib(n-2)  <p>如果我们调用fib(5)，将产生一棵对于同一值重复计算多次的调用树：  <ol> <li>fib(5)  <li>fib(4) + fib(3)  <li>(fib(3) + fib(2)) + (fib(2) + fib(1))  <li>((fib(2) + fib(1)) + (fib(1) + fib(0))) + ((fib(1) + fib(0)) + fib(1))  <li>(((fib(1) + fib(0)) + fib(1)) + (fib(1) + fib(0))) + ((fib(1) + fib(0)) + fib(1)) </li></ol> <p>特别是，fib(2)计算了3次。在更大规模的例子中，还有更多fib的值被重复计算，将消耗指数级时间。  <p>现在，假设我们有一个简单的<a href="http://en.wikipedia.org/wiki/Associative_array" target="_blank">映射</a>（map）对象<em>m</em>，为每一个计算过的fib及其返回值建立映射，修改上面的函数fib，使用并不断更新<em>m</em>。新的函数将只需<em>O</em>(<em>n</em>)的时间，而非指数时间：  <p>&nbsp;&nbsp; var m := map(0 → 1, 1 → 1)<br>&nbsp;&nbsp; function fib(n)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if map m does not contain key n<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m[n] := fib(n-1) + fib(n-2)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return m[n]  <p>这一保存已计算出的数值的技术即被称为<a href="http://en.wikipedia.org/wiki/Memoization" target="_blank">缓存</a>，这儿使用的是<strong>自顶向下</strong>的方法：先将问题划分为若干子问题，然后计算和存储值。  <p>在<strong>自下而上</strong>的方法中，我们先计算较小的fib，然后基于其计算更大的fib。这种方法也只花费线性（<em>O</em>(<em>n</em>)）时间，因为它包含一个<em>n</em>-1次的循环。然而，这一方法只需要常数（<em>O</em>(1)）的空间，相反，<strong>自顶向下</strong>的方法则需要O(<em>n</em>)的空间来储存映射关系。  <p>&nbsp;&nbsp; function fib(n)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var previousFib := 0, currentFib := 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if n = 0 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if n = 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; repeat n-1 times<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var newFib := previousFib + currentFib<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; previousFib := currentFib<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; currentFib&nbsp; := newFib<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return currentFib  <p>在这两个例子，我们都只计算fib(2)一次，然后用它来计算fib(3)和fib(4)，而不是每次都重新计算。  <p><strong>2. 一种平衡的0-1矩阵</strong>  <p>考虑<em>n</em>*<em>n</em>矩阵的赋值问题：只能赋0和1，<em>n</em>为偶数，使每一行和列均含<em>n</em>/2个0及<em>n</em>/2个1。例如，当<em>n</em>=4时，两种可能的方案是：  <p>+ - - - - +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + - - - - +<br>| 0 1 0 1 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 0 0 1 1 |<br>| 1 0 1 0 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 0 0 1 1 |<br>| 0 1 0 1 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 1 1 0 0 |<br>| 1 0 1 0 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 1 1 0 0 |<br>+ - - - - +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + - - - - +  <p>问：对于给定<em>n</em>，共有多少种不同的赋值方案。  <p>至少有三种可能的算法来解决这一问题：<a href="http://en.wikipedia.org/wiki/Brute_force" target="_blank">穷举法</a>（brute force）、<a href="http://en.wikipedia.org/wiki/Backtracking" target="_blank">回溯法</a>（backtracking）及动态规划（dynamic programming）。穷举法列举所有赋值方案，并逐一找出满足平衡条件的方案。由于共有C(<em>n</em>, <em>n</em>/2)^<em>n</em>种方案（<strong><em>在一行中，含n/2个0及n/2个1的组合数为C(n,n/2)，相当于从n个位置中选取n/2个位置置0，剩下的自然是1</em></strong>），当<em>n</em>=6时，穷举法就已经几乎不可行了。回溯法先将矩阵中部分元素置为0或1，然后检查每一行和列中未被赋值的元素并赋值，使其满足每一行和列中0和1的数量均为<em>n</em>/2。回溯法比穷举法更加巧妙一些，但仍需遍历所有解才能确定解的数目，可以看到，当<em>n</em>=8时，该题解的数目已经高达116963796250。动态规划则无需遍历所有解便可确定解的数目（<strong><em>意思是划分子问题后，可有效避免若干子问题的重复计算</em></strong>）。  <p>通过动态规划求解该问题出乎意料的简单。考虑每一行恰含<em>n</em>/2个0和<em>n</em>/2个1的<em>k</em>*<em>n</em>(1&lt;=<em>k</em>&lt;=<em>n</em>)的子矩阵，函数f根据每一行的可能的赋值映射为一个向量，每个向量由<em>n</em>个整数对构成。向量每一列对应的一个整数对中的两个整数分别表示该列上该行以下已经放置的0和1的数量。该问题即转化为寻找f((<em>n</em>/2,<em>n</em>/2),(<em>n</em>/2,<em>n</em>/2),...,(<em>n</em>/2,<em>n</em>/2))（具有<em>n</em>个参数或者说是一个含<em>n</em>个元素的向量）的值。其子问题的构造过程如下：  <p>1) 最上面一行（<strong><em>第k行</em></strong>）具有C(<em>n</em>, <em>n</em>/2)种赋值；  <p>2) 根据最上面一行中每一列的赋值情况（为0或1），将其对应整数对中相应的元素值减1；  <p>3) 如果任一整数对中的任一元素为负，则该赋值非法，不能成为正确解；  <p>4) 否则，完成对<em>k</em>*<em>n</em>的子矩阵中最上面一行的赋值，取<em>k</em>=<em>k</em>-1，计算剩余的(<em>k</em>-1)*<em>n</em>的子矩阵的赋值；  <p>5) 基本情况是一个1*<em>n</em>的细小的子问题，此时，该子问题的解的数量为0或1，取决于其向量是否是<em>n</em>/2个(0, 1)和<em>n</em>/2个(1, 0)的排列。  <p>例如，在上面给出的两种方案中，向量序列为：  <p>((2, 2) (2, 2) (2, 2) (2, 2))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((2, 2) (2, 2) (2, 2) (2, 2))&nbsp;&nbsp;&nbsp;&nbsp; k = 4<br>&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1  <p><font color="#800000">((1, 2) (2, 1) (1, 2) (2, 1))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((1, 2) (1, 2) (2, 1) (2, 1))&nbsp;&nbsp;&nbsp;&nbsp; k = 3</font><br>&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1  <p>((1, 1) (1, 1) (1, 1) (1, 1))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((0, 2) (0, 2) (2, 0) (2, 0))&nbsp;&nbsp;&nbsp;&nbsp; k = 2<br>&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0  <p>((0, 1) (1, 0) (0, 1) (1, 0))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((0, 1) (0, 1) (1, 0) (1, 0))&nbsp;&nbsp;&nbsp;&nbsp; k = 1<br>&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0  <p>((0, 0) (0, 0) (0, 0) (0, 0))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((0, 0) (0, 0), (0, 0) (0, 0))  <p><strong><font color="#800000"><em>动态规划在此的意义在于避免了相同f的重复计算，更进一步的，上面着色的两个f，虽然对应向量不同，但f的值是相同的，想想为什么吧</em>:D<em>。</em></font></strong>  <p>该问题解的数量（序列a058527在OEIS）是1, 2, 90, 297200, 116963796250, 6736218287430460752, ...  <p>下面的外部链接中包含回溯法的Perl源代码实现，以及动态规划法的MAPLE和C语言的实现。  <p><strong>3. 棋盘</strong>  <p>考虑<em>n</em>*<em>n</em>的棋盘及成本函数C(<em>i</em>,<em>j</em>)，该函数返回方格(<em>i</em>,<em>j</em>)相关的成本。以5*5的棋盘为例：  <p>5 | 6 7 4 7 8<br>4 | 7 6 1 1 4<br>3 | 3 5 7 8 2<br>2 | 2 6 7 0 2<br>1 | 7 3 5 6 1<br>- + - - - - -<br>&nbsp; | 1 2 3 4 5  <p>可以看到：C(1,3)=5  <p>从棋盘的任一方格的第一阶（即行）开始，寻找到达最后一阶的最短路径（使所有经过的方格的成本之和最小），假定只允许向左对角、右对角或垂直移动一格。  <p>5 |<br>4 |<br>3 |<br>2 |&nbsp;&nbsp; x x x<br>1 |&nbsp;&nbsp;&nbsp;&nbsp; o<br>- + - - - - -<br>&nbsp; | 1 2 3 4 5  <p>该问题展示了最优子结构。即整个问题的全局解依赖于子问题的解。定义函数q(<em>i</em>,<em>j</em>)，令：q(<em>i</em>,<em>j</em>)表示到达方格(<em>i</em>,<em>j</em>)的最低成本。  <p>如果我们可以求出第<em>n</em>阶所有方格的q(<em>i</em>,<em>j</em>)值，取其最小值并逆向该路径即可得到最短路径。  <p>记q(<em>i</em>,<em>j</em>)为方格(<em>i</em>,<em>j</em>)至其下三个方格（<strong><em>(i-1,j-1)、(i-1,j)、(i-1,j+1)</em></strong>）最低成本与c(<em>i</em>,<em>j</em>)之和，例如：  <p>5 |<br>4 |&nbsp;&nbsp;&nbsp;&nbsp; <em>A</em><br>3 |&nbsp;&nbsp; <em>B C D<br></em>2 |<br>1 |<br>- + - - - - -<br>&nbsp; | 1 2 3 4 5  <p>q(<em>A</em>) = min(q(<em>B</em>),q(<em>C</em>),q(<em>D</em>)) + c(<em>A</em>)  <p>定义q(<em>i</em>,<em>j</em>)的一般形式：  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-&nbsp; inf.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>j</em>&lt;1 or <em>j</em>&gt;n<br>q(<em>i</em>,<em>j</em>) = -+-&nbsp; c(<em>i</em>,<em>j</em>)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i=1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-&nbsp; min(q(<em>i</em>-1,<em>j</em>-1),q(<em>i</em>-1,<em>j</em>),q(<em>i</em>-1,<em>j</em>+1))+c(<em>i</em>,<em>j</em>)&nbsp;&nbsp; otherwise.  <p>方程的第一行是为了保证递归可以退出（处理边界时只需调用一次递归函数）。第二行是第一阶的取值，作为计算的起点。第三行的递归是算法的重要组成部分，与例子<em>A</em>、<em>B</em>、<em>C</em>、<em>D</em>类似。从该定义我们可以直接给出计算q(<em>i</em>,<em>j</em>)的简单的递归代码。在下面的伪代码中，<em>n</em>表示棋盘的维数，C(<em>i</em>,<em>j</em>)是成本函数，min()返回一组数的最小值：  <p>function minCost(i, j)<br>&nbsp;&nbsp;&nbsp; if j &lt; 1 or j &gt; n<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return infinity<br>&nbsp;&nbsp;&nbsp; else if i = 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return c(i,j)<br>&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return min(minCost(i-1,j-1),minCost(i-1,j),minCost(i-1,j+1))+c(i,j)  <p>需要指出的是，minCost只计算路径成本，并不是最终的实际路径，二者相去不远。与Fibonacci数相似，由于花费大量时间重复计算相同的最短路径，这一方式慢的恐怖。不过，如果采用自下而上法，使用二维数组q[i,j]代替函数minCost，将使计算过程快得多。我们为什么要这样做呢？选择保存值显然比使用函数重复计算相同路径要简单的多。  <p>我们还需要知道实际路径。路径问题，我们可以通过另一个前任数组p[i,j]解决。这个数组用于描述路径，代码如下：  <p>function computeShortestPathArrays()<br>&nbsp;&nbsp;&nbsp;&nbsp; for x from 1 to n<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; q[1, x] := c(1, x)<br>&nbsp;&nbsp;&nbsp;&nbsp; for y from 1 to n<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; q[y, 0]&nbsp;&nbsp;&nbsp;&nbsp; := infinity<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; q[y, n + 1] := infinity<br>&nbsp;&nbsp;&nbsp;&nbsp; for y from 2 to n<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for x from 1 to n<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m := min(q[y-1, x-1], q[y-1, x], q[y-1, x+1])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; q[y, x] := m + c(y, x)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if m = q[y-1, x-1]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p[y, x] := -1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if m = q[y-1, x]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p[y, x] :=&nbsp; 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p[y, x] :=&nbsp; 1  <p>剩下的求最小值和输出就比较简单了：  <p>function computeShortestPath()<br>&nbsp;&nbsp;&nbsp;&nbsp; computeShortestPathArrays()<br>&nbsp;&nbsp;&nbsp;&nbsp; minIndex := 1<br>&nbsp;&nbsp;&nbsp;&nbsp; min := q[n, 1] <br>&nbsp;&nbsp;&nbsp;&nbsp; for i from 2 to n <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if q[n, i] &lt; min<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; minIndex := i<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; min := q[n, i]<br>&nbsp;&nbsp;&nbsp;&nbsp; printPath(n, minIndex)  <p>function printPath(y, x)<br>&nbsp;&nbsp;&nbsp;&nbsp; print(x)<br>&nbsp;&nbsp;&nbsp;&nbsp; print("&lt;-")<br>&nbsp;&nbsp;&nbsp;&nbsp; if y = 2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(x + p[y, x])<br>&nbsp;&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printPath(y-1, x + p[y, x])  <p><strong>4. 序列比对 </strong> <p><a href="http://en.wikipedia.org/wiki/Sequence_alignment" target="_blank">序列比对</a>是动态规划的一个重要应用。序列比对问题通常是使用编辑操作（替换、插入、删除一个要素等）进行序列转换。每次操作对应不同成本，目标是找到编辑序列的最低成本。  <p>可以很自然地想到使用递归解决这个问题，序列<em>A</em>到<em>B</em>的最优编辑通过以下措施之一实现：  <p>插入<em>B</em>的第一个字符，对<em>A</em>和<em>B</em>的剩余序列进行最优比对；  <p>删去<em>A</em>的第一个字符，对<em>A</em>和<em>B</em>进行最优比对；  <p>用<em>B</em>的第一个字符替换<em>A</em>的第一个字符，对<em>A</em>的剩余序列和<em>B</em>进行最优比对。  <p>局部比对可在矩阵中列表表示，单元(<em>i</em>,<em>j</em>)表示A[1..<em>i</em>]到b[1..<em>j</em>]最优比对的成本。单元(<em>i</em>,<em>j</em>)的成本计算可通过累加相邻单元的操作成本并选择最优解实现。至于序列比对的不同实现算法，参见<a href="http://en.wikipedia.org/wiki/Smith-Waterman" target="_blank">Smith-Waterman</a>和<a href="http://en.wikipedia.org/wiki/Needleman-Wunsch" target="_blank">Needleman-Wunsch</a>。  <p><strong><em>对序列比对的话题并不熟悉，更多的话也无从谈起，有熟悉的朋友倒是可以介绍一下。</em></strong>  <ul> <li><strong>应用动态规划的算法</strong> </li></ul> <p>1) 许多<a href="http://en.wikipedia.org/wiki/String_%28computer_science%29" target="_blank">字符串</a>操作算法如<a href="http://en.wikipedia.org/wiki/Longest_common_subsequence_problem" target="_blank">最长公共子列</a>、<a href="http://en.wikipedia.org/wiki/Longest_increasing_subsequence_problem" target="_blank">最长递增子列</a>、<a href="http://en.wikipedia.org/wiki/Longest_common_substring_problem" target="_blank">最长公共字串</a>； </p> <p>2) 将动态规划用于<a href="http://en.wikipedia.org/wiki/Undirected_graph" target="_blank">图</a>的树分解，可以有效解决有界<a href="http://en.wikipedia.org/wiki/Treewidth" target="_blank">树宽图</a>的<a href="http://en.wikipedia.org/wiki/Tree_decomposition" target="_blank">生成树</a>等许多与图相关的算法问题；  <p>3) 决定是否及如何可以通过某一特定<a href="http://en.wikipedia.org/wiki/Context-free_grammar" target="_blank">上下文无关文法</a>产生给定字符串的<a href="http://en.wikipedia.org/wiki/CYK_algorithm" target="_blank">Cocke-Younger-Kasami</a> (CYK)算法；  <p>4) <a href="http://en.wikipedia.org/wiki/Computer_chess" target="_blank">计算机国际象棋</a>中<a href="http://en.wikipedia.org/wiki/Transposition_table" target="_blank">转换表</a>和<a href="http://en.wikipedia.org/wiki/Refutation_table" target="_blank">驳斥表</a>的使用；  <p>5) <a href="http://en.wikipedia.org/wiki/Viterbi_algorithm" target="_blank">Viterbi算法</a>（用于<a href="http://en.wikipedia.org/wiki/Hidden_Markov_model" target="_blank">隐式马尔可夫模型</a>）；  <p>6) <a href="http://en.wikipedia.org/wiki/Earley_algorithm" target="_blank">Earley算法</a>（一类<a href="http://en.wikipedia.org/wiki/Chart_parser" target="_blank">图表分析器</a>）；  <p>7) <a href="http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm" target="_blank">Needleman-Wunsch</a>及其他<a href="http://en.wikipedia.org/wiki/Bioinformatics" target="_blank">生物信息学</a>中使用的算法，包括<a href="http://en.wikipedia.org/wiki/Sequence_alignment" target="_blank">序列比对</a>、<a href="http://en.wikipedia.org/wiki/Structural_alignment" target="_blank">结构比对</a>、<a href="http://en.wikipedia.org/wiki/RNA_structure" target="_blank">RNA结构</a>预测；  <p>8) <a href="http://en.wikipedia.org/wiki/Levenshtein_distance" target="_blank">Levenshtein距离</a>（编辑距离）；  <p>9) <a href="http://en.wikipedia.org/wiki/Floyd-Warshall_algorithm" target="_blank">弗洛伊德最短路径</a>算法；  <p>10) <a href="http://en.wikipedia.org/wiki/Chain_matrix_multiplication" target="_blank">连锁矩阵乘法</a>次序优化；  <p>11) <a href="http://en.wikipedia.org/wiki/Subset_sum_problem" target="_blank">子集求和</a>、<a href="http://en.wikipedia.org/wiki/Knapsack_problem" target="_blank">背包问题</a>和<a href="http://en.wikipedia.org/wiki/Partition_problem" target="_blank">分治问题</a>的伪多项式时间算法；  <p>12) 计算两个时间序列全局距离的<a href="http://en.wikipedia.org/wiki/Dynamic_time_warping" target="_blank">动态时间规整</a>算法；  <p>13) 关系型数据库的查询优化的<a href="http://en.wikipedia.org/wiki/Selinger" target="_blank">Selinger</a>（又名<a href="http://en.wikipedia.org/wiki/System_R" target="_blank">System R</a>）算法；  <p>14) 评价B样条曲线的<a href="http://en.wikipedia.org/wiki/De_Boor_algorithm" target="_blank">De Boor算法</a>；  <p>15) 用于解决板球运动中断问题的<a href="http://en.wikipedia.org/wiki/Duckworth-Lewis_method" target="_blank">Duckworth-Lewis</a>方法；  <p>16) 价值迭代法求解<a href="http://en.wikipedia.org/wiki/Markov_decision_process" target="_blank">马尔可夫决策过程</a>；  <p>17) 一些图形图像边缘以下的选择方法，如“磁铁”选择工具在<a href="http://en.wikipedia.org/wiki/Photoshop" target="_blank">Photoshop</a>；  <p>18) <a href="http://en.wikipedia.org/wiki/Interval_scheduling" target="_blank">间隔调度</a>；  <p>19) <a href="http://en.wikipedia.org/wiki/Word_wrap" target="_blank">自动换行</a>；  <p>20) <a href="http://en.wikipedia.org/wiki/Travelling_salesman_problem" target="_blank">巡回旅行商问题</a>（<strong><em>又称邮差问题或货担郎问题</em></strong>）；  <p>21) <a href="http://en.wikipedia.org/wiki/Segmented_least_squares" target="_blank">分段最小二乘法</a>；  <p>22) <a href="http://en.wikipedia.org/wiki/Music_Information_Retrieval" target="_blank">音乐信息检索</a>跟踪。  <p><strong><em>对于这些算法应用，大多未曾接触，甚至术语翻译的都有问题，鉴于本文主要在于介绍动态规划，所以仓促之中，未及查证。</em></strong>  <ul> <li><strong>相关</strong> </li></ul> <p>1) <a href="http://en.wikipedia.org/wiki/Bellman_equation" target="_blank">贝尔曼方程</a>  <p>2) <a href="http://en.wikipedia.org/wiki/Markov_decision_process" target="_blank">马尔可夫决策过程</a>  <p>3) <a href="http://en.wikipedia.org/wiki/Greedy_algorithm" target="_blank">贪心算法</a> </p> <ul> <li><strong>参考</strong> </li></ul> <li>Adda, Jerome, and Cooper, Russell, 2003. <em><a href="http://www.eco.utexas.edu/~cooper/dynprog/dynprog1.html">Dynamic Economics.</a></em> MIT Press. An accessible introduction to dynamic programming in economics. The link contains sample programs.  <li>Richard Bellman, 1957, <em>Dynamic Programming</em>, Princeton University Press. Dover paperback edition (2003), <a href="http://en.wikipedia.org/wiki/Special:BookSources/0486428095">ISBN 0486428095</a>.  <li>Bertsekas, D. P., 2000. <em>Dynamic Programming and Optimal Control, Vols. 1 &amp; 2</em>, 2nd ed. Athena Scientific. <a href="http://en.wikipedia.org/wiki/Special:BookSources/1886529094">ISBN 1-886529-09-4</a>.  <li><a href="http://en.wikipedia.org/wiki/Thomas_H._Cormen">Thomas H. Cormen</a>, <a href="http://en.wikipedia.org/wiki/Charles_E._Leiserson">Charles E. Leiserson</a>, <a href="http://en.wikipedia.org/wiki/Ronald_L._Rivest">Ronald L. Rivest</a>, and <a href="http://en.wikipedia.org/wiki/Clifford_Stein">Clifford Stein</a>, 2001. <em><a href="http://en.wikipedia.org/wiki/Introduction_to_Algorithms">Introduction to Algorithms</a></em>, 2nd ed. MIT Press &amp; McGraw-Hill. <a href="http://en.wikipedia.org/wiki/Special:BookSources/0262032937">ISBN 0-262-03293-7</a>. Especially pp. 323–69.  <li>Giegerich, R., Meyer, C., and Steffen, P., 2004, "<a href="http://bibiserv.techfak.uni-bielefeld.de/adp/ps/GIE-MEY-STE-2004.pdf">A Discipline of Dynamic Programming over Sequence Data,</a>" <em>Science of Computer Programming 51</em>: 215-263.  <li><a href="http://en.wikipedia.org/wiki/Nancy_Stokey">Nancy Stokey</a>, and <a href="http://en.wikipedia.org/wiki/Robert_E._Lucas">Robert E. Lucas</a>, with <a href="http://en.wikipedia.org/wiki/Edward_Prescott">Edward Prescott</a>, 1989. <em>Recursive Methods in Economic Dynamics</em>. Harvard Univ. Press.  <li>S. P. Meyn, 2007. <a href="http://decision.csl.uiuc.edu/~meyn/pages/CTCN/CTCN.html">Control Techniques for Complex Networks</a>, Cambridge University Press, 2007.  <ul> <li><strong>外部链接</strong> </li></ul> <ul> <li><a href="http://www.dyna.org/">Dyna</a>, a declarative programming language for dynamic programming algorithms  <li>Wagner, David B., 1995, "<a href="http://citeseer.ist.psu.edu/268391.html">Dynamic Programming.</a>" An introductory article on dynamic programming in <a href="http://en.wikipedia.org/wiki/Mathematica">Mathematica</a>.  <li><a href="http://www.cse.ohio-state.edu/~gurari/course/cis680/cis680Ch21.html">Ohio State University: CIS 680: class notes on dynamic programming</a>, by Eitan M. Gurari  <li><a href="http://mat.gsia.cmu.edu/classes/dynamic/dynamic.html">A Tutorial on Dynamic programming</a>  <li><a href="http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-046JFall-2005/LectureNotes/index.htm">MIT course on algorithms</a> - Includes a video lecture on DP along with lecture notes -- See lecture 15.  <li><a href="http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Dynamic">More DP Notes</a>  <li>King, Ian, 2002 (1987), "<a href="http://www.business.auckland.ac.nz/Departments/econ/workingpapers/full/Text230.pdf">A Simple Introduction to Dynamic Programming in Macroeconomic Models.</a>" An introduction to dynamic programming as an important tool in economic theory.  <li><a href="http://www.topcoder.com/tc?module=Static&amp;d1=tutorials&amp;d2=dynProg">Dynamic Programming: from novice to advanced</a> A TopCoder.com article by Dumitru on Dynamic Programming  <li><a href="http://bibiserv.techfak.uni-bielefeld.de/adp/">Algebraic Dynamic Programming</a> - a formalized framework for dynamic programming, including an <a href="http://bibiserv.techfak.uni-bielefeld.de/dpcourse">entry-level course</a> to DP, University of Bielefeld  <li>Dreyfus, Stuart, "<a href="http://www.eng.tau.ac.il/~ami/cd/or50/1526-5463-2002-50-01-0048.pdf">Richard Bellman on the birth of Dynamic Programming.</a>"  <li><a href="http://www.avatar.se/lectures/molbioinfo2001/dynprog/dynamic.html">Dynamic programming tutorial</a>  <li><a href="http://20bits.com/2007/05/08/introduction-to-dynamic-programming/">An Introduction to Dynamic Programming</a> </li></ul> <p>_____________________________________________________________</p> <p>关于动态规划，这只是一篇译文，后面将根据实际问题具体写点动态规划的应用。</p></li><img src ="http://www.cppblog.com/Fox/aggbug/49153.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Fox/" target="_blank">Fox</a> 2008-05-07 20:43 <a href="http://www.cppblog.com/Fox/archive/2008/05/07/dynamic_programming.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>获取汉字的首拼字母(java)</title><link>http://www.cppblog.com/socketref/archive/2008/05/07/49074.html</link><dc:creator>放屁阿狗</dc:creator><author>放屁阿狗</author><pubDate>Tue, 06 May 2008 17:28:00 GMT</pubDate><guid>http://www.cppblog.com/socketref/archive/2008/05/07/49074.html</guid><wfw:comment>http://www.cppblog.com/socketref/comments/49074.html</wfw:comment><comments>http://www.cppblog.com/socketref/archive/2008/05/07/49074.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/socketref/comments/commentRss/49074.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/socketref/services/trackbacks/49074.html</trackback:ping><description><![CDATA[代码很容易阅读，以前做蓝牙项目时用户电话本搜索只用<br><br><br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;string&nbsp;GetChineseSpell(string&nbsp;strText)<br>{<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;len&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;strText.Length;<br>string&nbsp;myStr&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">""</span><span style="color: #000000;">;<br></span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;i</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">len;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br>{<br>myStr&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;getSpell(strText.Substring(i,</span><span style="color: #000000;">1</span><span style="color: #000000;">));<br>}<br></span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;myStr;<br>}<br><br></span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;string&nbsp;getSpell(string&nbsp;myChar)<br>{<br></span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;arrCN&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;System.Text.Encoding.Default.GetBytes(myChar);<br></span><span style="color: #0000ff;">if</span><span style="color: #000000;">(arrCN.Length&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br>{<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;area&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">short</span><span style="color: #000000;">)arrCN[</span><span style="color: #000000;">0</span><span style="color: #000000;">];<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;pos&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">short</span><span style="color: #000000;">)arrCN[</span><span style="color: #000000;">1</span><span style="color: #000000;">];<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;code&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(area</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">8</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;pos;<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">[]&nbsp;areacode&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;{</span><span style="color: #000000;">45217</span><span style="color: #000000;">,</span><span style="color: #000000;">45253</span><span style="color: #000000;">,</span><span style="color: #000000;">45761</span><span style="color: #000000;">,</span><span style="color: #000000;">46318</span><span style="color: #000000;">,</span><span style="color: #000000;">46826</span><span style="color: #000000;">,</span><span style="color: #000000;">47010</span><span style="color: #000000;">,</span><span style="color: #000000;">47297</span><span style="color: #000000;">,</span><span style="color: #000000;">47614</span><span style="color: #000000;">,</span><span style="color: #000000;">48119</span><span style="color: #000000;">,</span><span style="color: #000000;">48119</span><span style="color: #000000;">,</span><span style="color: #000000;">49062</span><span style="color: #000000;">,</span><span style="color: #000000;">49324</span><span style="color: #000000;">,</span><span style="color: #000000;">49896</span><span style="color: #000000;">,</span><span style="color: #000000;">50371</span><span style="color: #000000;">,</span><span style="color: #000000;">50614</span><span style="color: #000000;">,</span><span style="color: #000000;">50622</span><span style="color: #000000;">,</span><span style="color: #000000;">50906</span><span style="color: #000000;">,</span><span style="color: #000000;">51387</span><span style="color: #000000;">,</span><span style="color: #000000;">51446</span><span style="color: #000000;">,</span><span style="color: #000000;">52218</span><span style="color: #000000;">,</span><span style="color: #000000;">52698</span><span style="color: #000000;">,</span><span style="color: #000000;">52698</span><span style="color: #000000;">,</span><span style="color: #000000;">52698</span><span style="color: #000000;">,</span><span style="color: #000000;">52980</span><span style="color: #000000;">,</span><span style="color: #000000;">53689</span><span style="color: #000000;">,</span><span style="color: #000000;">54481</span><span style="color: #000000;">};<br></span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;i</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">26</span><span style="color: #000000;">;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br>{<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;max&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">55290</span><span style="color: #000000;">;<br></span><span style="color: #0000ff;">if</span><span style="color: #000000;">(i&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">25</span><span style="color: #000000;">)&nbsp;max&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;areacode[i</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">];<br></span><span style="color: #0000ff;">if</span><span style="color: #000000;">(areacode[i]</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">code&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;code</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">max)<br>{<br></span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;System.Text.Encoding.Default.GetString(</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]{(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)(</span><span style="color: #000000;">65</span><span style="color: #000000;">+</span><span style="color: #000000;">i)});<br>}<br>}<br></span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">*</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br>}<br></span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;myChar;<br>}</span></div>
<br><br><img src ="http://www.cppblog.com/socketref/aggbug/49074.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/socketref/" target="_blank">放屁阿狗</a> 2008-05-07 01:28 <a href="http://www.cppblog.com/socketref/archive/2008/05/07/49074.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>今天去the9.com面试，一些考题，一些想法</title><link>http://www.cppblog.com/socketref/archive/2008/05/06/49040.html</link><dc:creator>放屁阿狗</dc:creator><author>放屁阿狗</author><pubDate>Tue, 06 May 2008 12:16:00 GMT</pubDate><guid>http://www.cppblog.com/socketref/archive/2008/05/06/49040.html</guid><wfw:comment>http://www.cppblog.com/socketref/comments/49040.html</wfw:comment><comments>http://www.cppblog.com/socketref/archive/2008/05/06/49040.html#Feedback</comments><slash:comments>37</slash:comments><wfw:commentRss>http://www.cppblog.com/socketref/comments/commentRss/49040.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/socketref/services/trackbacks/49040.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp; 昨天接到the9的人事mm的电话通知今天去公司面试，职位大概是开发岗位<br>这些年来也一直没有面试的经历，闲在家里没事所以今天就去看看。<br>the9.com =&gt;张江高科技园区碧波路690号3号楼,google了一下具体位置，带了个导航仪开着桑哥走了。<br>外环比较拥挤，25公里开了45分钟便到了碧波路，一个大拐就进了690号，接着一个小拐又一个大拐，直接将车停就进了车位，"技术不错,可以打9.9分"。<br>&nbsp;&nbsp;&nbsp; the9也算是有点财力和规模，整个一片都是the9公司。<br>刚想推门下车，一个保安马上上来，我想这个服务到是周到。"先生，这里不能停车，这是我们老板的车位". 奶奶的，确实边上不是BMW就是A6之类的车子，仔细一看，确实车位上都有具体的车牌。一不小心把车停到the9老板 家了。接着就倒车，7拐八拐 找了个日光浴的位置。<br>来到the9的前台，说是要做题，领了份考卷就去2号会议室。<br>&nbsp;&nbsp;&nbsp; 里面有2人，各一男女，没多时便走了，过了半小时又进来一位做题，看上去比我是年轻多了。<br>开始做题，好久没被面试了，有点兴奋。某些题目回答的太细且考虑过多，磨磨蹭蹭也搞了一个小时，看了下钟点15:30了。<br>接着等人来捞我去谈，等了30分钟也没人来，所以就踱到前台交予前台mm(长得不错哦)。然后我继续等，约莫20来分钟mm叫我，我便跟一个叫陈国*的Man去面试，陈**带我绕了几条走廊，那个走路的速度真是超级的慢，居然是我走在他前面，有点受不了。<br>&nbsp;&nbsp;&nbsp; 进了一会议室，陈**不知为何一下子没开口，瞬即拿出笔在白板上写了起来。<br>&nbsp;&nbsp;&nbsp; "你现在做个题目哦，题目是这样的:1000~10000里面的4位平方数你给我找出来，数字的规则是 abcd, a=b c=d,我现在有个其他面试，过5分钟我再来"，奶奶的，居然还让我做题，而且是这种小学生做的题目。说完陈Man就走了，真是来气，起来我也转身离开了the.com。<br>做了这么些年的开发，本来以为面试会跟我聊一下系统的架构，opensource，通信技巧，看了我的简历也不应该当成应届毕业生来对待啊，一些考官就是喜欢在面试过程中夹杂一些自己的小聪明搞一些旁门做到的东西，想想过去我做考官也不是这个样子的，还是比较对人尊重的，这么大的一个公司让面试的人左等右等，感觉这是不这么的好。<br>&nbsp;&nbsp;&nbsp; 记得一个mm说的好，说是老板与员工不存在地位的差别，雇佣和被雇工是建立在平等的基础上的合作关系。<br>&nbsp;&nbsp;&nbsp; 想到了 盖茨关于他的车位总是被员工占用，及员工总是跟盖茨借钱的故事；想到了以前一位博士领导整天给老总安装office的事情<br>&nbsp;&nbsp;&nbsp; 中国人骨子里还是比较官僚的，阶级感比较强烈，老板永远是老板，是上帝，打工的就是一条狗。<br>&nbsp;&nbsp;&nbsp; 不过我对狗这个字眼不感冒，我就是一条狗，但是是条有尊严的狗。<br>&nbsp;&nbsp;&nbsp; the9对其现在不这么感兴趣了，林子大了啥鸟都有，还是老实在家呆着。<br>&nbsp;&nbsp;&nbsp; the9的考题对于开发者的还是有点用的，凭着有点记忆的脑子回想一下考题，大致如下: <br><br>1.是非题: 10题&nbsp; 具体记不清楚了<br>2.解释: <br>&nbsp;&nbsp;&nbsp; const 的作用（2种以上)<br>&nbsp;&nbsp;&nbsp; 数据与链表的差异和作用 <br>&nbsp;&nbsp;&nbsp; 纯虚函数，重载的区别和作用<br><br>3.改错并解释: <br>&nbsp; 1. void getmemory( char * p){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p = new char[20];<br>&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp; main(){<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  char *str;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  getmemory(str);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; strcpy(str,"hello");<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp; 2. char * getmemory(){<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  char buf[]="ssssssssssssssss";<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  return buf;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; main(){<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  sprintf(buf,"%d",100);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  printf( buf);<br>&nbsp;&nbsp;&nbsp; }<br><br>4.编写函数: <br>&nbsp;&nbsp;&nbsp; 1. strcmp<br>&nbsp;&nbsp;&nbsp; int strcmp( char * s1,char * s2 ){<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; 2. strstr<br>&nbsp;&nbsp;&nbsp; // return pointer if s2 found in s1,else return NULL<br>&nbsp;&nbsp;&nbsp; char * strstr(char* s1,char* s2){<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp; &nbsp; 3. void compress(char * in,char * out)<br>&nbsp;&nbsp;&nbsp; 要求:&nbsp; &nbsp;&nbsp;  <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; in&nbsp;&nbsp; &nbsp; &nbsp;  &nbsp;&nbsp;  out<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  &nbsp;&nbsp;  &nbsp; abc&nbsp;&nbsp;  &nbsp;&nbsp;  &nbsp;&nbsp;  abc<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  &nbsp;&nbsp;  aaabbbccc&nbsp;&nbsp;  &nbsp; a2b2c2<br><br>&nbsp;&nbsp;&nbsp; 5. 实现以下类成员函数并解释<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; class String(){<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  &nbsp;&nbsp;  String(char* s=NULL);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  &nbsp;&nbsp;  String( const String &amp; other);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  &nbsp;&nbsp;  String &amp; operator+=(const String &amp;other);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  &nbsp;&nbsp;  bool operator==(const String &amp; other );<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  &nbsp;&nbsp;  operator double();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; };<br>&nbsp;&nbsp;&nbsp; 6. 链表倒置<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  struct listNode{<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  &nbsp;&nbsp; struct listNode * next;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  &nbsp;&nbsp;  int data;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;  <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; 返回列表头节点<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listNode * reverse(listNode * head){<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <br>这些题基本上是能考核一个c/cpp开发人员的基本技术能力的<br><br>  <img src ="http://www.cppblog.com/socketref/aggbug/49040.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/socketref/" target="_blank">放屁阿狗</a> 2008-05-06 20:16 <a href="http://www.cppblog.com/socketref/archive/2008/05/06/49040.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>lua和python谁更适用于嵌入MMORPG？</title><link>http://www.cppblog.com/kevinlynx/archive/2008/05/06/49023.html</link><dc:creator>Kevin Lynx</dc:creator><author>Kevin Lynx</author><pubDate>Tue, 06 May 2008 09:37:00 GMT</pubDate><guid>http://www.cppblog.com/kevinlynx/archive/2008/05/06/49023.html</guid><wfw:comment>http://www.cppblog.com/kevinlynx/comments/49023.html</wfw:comment><comments>http://www.cppblog.com/kevinlynx/archive/2008/05/06/49023.html#Feedback</comments><slash:comments>9</slash:comments><wfw:commentRss>http://www.cppblog.com/kevinlynx/comments/commentRss/49023.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/kevinlynx/services/trackbacks/49023.html</trackback:ping><description><![CDATA[<p>&nbsp;</p> <p>预计新项目会选择lua或python之一作为游戏的脚本语言。以前草草地接触过这两门语言，对于语法，以及嵌入进C/C++程序都有点感性上的认识。可能是受《UNIX编程艺术》中KISS原则的影响，现在总喜欢简洁的东西。所以我个人比较偏向于使用lua。</p> <p>&nbsp;</p> <p>这两天翻了下网络上的资料，在lua的wiki上看到一篇比较lua和python的<a href="http://lua-users.org/wiki/LuaVersusPython">文章</a>，草草地翻译出要点：</p> <p>Python:<br>1. 扩展库很多，资料很多<br>2. 数值计算比较强大，支持多维数组，而lua没有数组类型<br>3. 本身带的c类型(?)支持处理动态链接库，不需要进行C封装(C扩展)<br>4. 远程调试器，似乎lua扩展工具支持<br>5. 自然语言似的语法<br>6. 对于string和list的支持，lua可以通过扩展库实现<br>7. 对unicode的支持<br>8. 空格敏感(代码不忽略空格)，这其实可以使python的代码风格看起来更好一点<br>9. 内建位操作，lua可以通过扩展库支持<br>10.语言本身对错误的处理要好些，可以有效减少程序错误<br>11.初级文档比lua多<br>12.对面向对象支持更好  <p>Lua:<br>1. 比python小巧很多(包括编译出来的运行时库)<br>2. 占用更小的内存<br>3. 解释器速度更快<br>4. 比python更容易集成到C语言中<br>5. 对于对象不使用引用计数(引用计数会导致更多的问题？)<br>6. lua早期定位于一种配置语言(作为配置文件)，因此比起python来更容易配置数据<br>7. 语言更漂亮(nice)、简单(simple)、强大(powerful)。<br>8. lua支持多线程，每个线程可以配置独立的解释器，因此lua更适合于集成进多线程程序<br>9. 对空格不敏感，不用担心编辑器会将tab替换成空格  <p>Useful Comments:<br>1. Everything is an object allocated on the heap in Python, including numbers. (So 123+456 creates a new heap object).<br>2. lua对于coroutine的支持更适用于嵌入进游戏，虽然python也有，但是并没有包含进核心模块  <p>3.Python was a language better suited to Game AI</p> <p>&nbsp;</p> <p>本来想去找点对于python的正面资料(嵌入进游戏这方面），但是居然没找到。客观地说如果单独用python做应用，python还是很有优势。现在心意已决，应该向leader推荐lua。</p> <p>&nbsp;</p> <p>ps，希望能补充以上两种语言的特点。</p><img src ="http://www.cppblog.com/kevinlynx/aggbug/49023.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/kevinlynx/" target="_blank">Kevin Lynx</a> 2008-05-06 17:37 <a href="http://www.cppblog.com/kevinlynx/archive/2008/05/06/49023.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>以后要注意stdafx.h和targetver.h里的系统版本定义啊</title><link>http://www.cppblog.com/greatws/archive/2008/05/05/48933.html</link><dc:creator>greatws</dc:creator><author>greatws</author><pubDate>Mon, 05 May 2008 13:50:00 GMT</pubDate><guid>http://www.cppblog.com/greatws/archive/2008/05/05/48933.html</guid><wfw:comment>http://www.cppblog.com/greatws/comments/48933.html</wfw:comment><comments>http://www.cppblog.com/greatws/archive/2008/05/05/48933.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/greatws/comments/commentRss/48933.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/greatws/services/trackbacks/48933.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 做了个DNS查询有关的程序，用到了DnsQuery和DnsRecordListFree这2个函数，拿到导师那里一用，竟然出现一个对话框，“无法将函数DnsFree定位于动态连接库Dnsapi.dll上”...&nbsp;&nbsp;<a href='http://www.cppblog.com/greatws/archive/2008/05/05/48933.html'>阅读全文</a><img src ="http://www.cppblog.com/greatws/aggbug/48933.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/greatws/" target="_blank">greatws</a> 2008-05-05 21:50 <a href="http://www.cppblog.com/greatws/archive/2008/05/05/48933.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>中南赛A题 Accumulation Degree</title><link>http://www.cppblog.com/sicheng/archive/2008/05/05/48928.html</link><dc:creator>oyjpart</dc:creator><author>oyjpart</author><pubDate>Mon, 05 May 2008 12:59:00 GMT</pubDate><guid>http://www.cppblog.com/sicheng/archive/2008/05/05/48928.html</guid><wfw:comment>http://www.cppblog.com/sicheng/comments/48928.html</wfw:comment><comments>http://www.cppblog.com/sicheng/archive/2008/05/05/48928.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/sicheng/comments/commentRss/48928.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sicheng/services/trackbacks/48928.html</trackback:ping><description><![CDATA[<div class="ptt" lang="en-US">Accumulation Degree</div>
<div class="plm">
<table align="center">
    <tbody>
        <tr>
            <td><strong>Time Limit:</strong> 5000MS</td>
            <td width="10"><br></td>
            <td><strong>Memory Limit:</strong> 65536K</td>
        </tr>
        <tr>
            <td><strong>Total Submissions:</strong> 248</td>
            <td width="10"><br></td>
            <td><strong>Accepted:</strong> 30</td>
        </tr>
    </tbody>
</table>
</div>
<p>Description</p>
<div class="ptx" lang="en-US">
<div>
<p>Trees
are an important component of the natural landscape because of their
prevention of erosion and the provision of a specific ather-sheltered
ecosystem in and under their foliage. Trees have also been found to
play an important role in producing oxygen and reducing carbon dioxide
in the atmosphere, as well as moderating ground temperatures. They are
also significant elements in landscaping and agriculture, both for
their aesthetic appeal and their orchard crops (such as apples). Wood
from trees is a common building material. </p>
<p>Trees also play an
intimate role in many of the world's mythologies. Many scholars are
interested in finding peculiar properties about trees, such as the
center of a tree, tree counting, tree coloring. A(<em>x</em>) is one of such properties.</p>
<p>A(<em>x</em>) (accumulation degree of node <em>x</em>) is defined as follows:</p>
<ol>
    <li>Each edge of the tree has an positive capacity.</li>
    <li>The nodes with degree of one in the tree are named terminals.</li>
    <li>The flow of each edge can't exceed its capacity.</li>
    <li><em>A</em>(<em>x</em>) is the maximal flow that node <em>x</em> can flow to other terminal nodes.</li>
</ol>
<p>Since it may be hard to understand the definition, an example is showed below:</p>
<p><img src="http://acm.pku.edu.cn/JudgeOnline/images/3585_1.PNG"></p>
<br>
<table id="table1" border="0" width="69%">
    <tbody>
        <tr>
            <td colspan="3">A(1)=11+5+8=24</td>
        </tr>
        <tr>
            <td width="15%">Details:</td>
            <td width="19%">1<strong>-&gt;</strong>2</td>
            <td width="63%">11</td>
        </tr>
        <tr>
            <td width="15%">　</td>
            <td width="19%">1<strong>-&gt;</strong>4<strong>-&gt;</strong>3</td>
            <td width="63%">5</td>
        </tr>
        <tr>
            <td width="15%">　</td>
            <td width="19%">1<strong>-&gt;</strong>4<strong>-&gt;</strong>5</td>
            <td width="63%">8(since 1<strong>-&gt;</strong>4 has capacity of 13)</td>
        </tr>
        <tr>
            <td colspan="3" width="97%">A(2)=5+6=11</td>
        </tr>
        <tr>
            <td width="15%">Details:</td>
            <td width="19%">2<strong>-&gt;</strong>1<strong>-&gt;</strong>4<strong>-&gt;</strong>3</td>
            <td width="63%">5</td>
        </tr>
        <tr>
            <td width="15%">　</td>
            <td width="19%">2<strong>-&gt;</strong>1<strong>-&gt;</strong>4<strong>-&gt;</strong>5</td>
            <td width="63%">6</td>
        </tr>
        <tr>
            <td colspan="3" width="97%">A(3)=5</td>
        </tr>
        <tr>
            <td width="15%">Details: </td>
            <td width="19%">3<strong>-&gt;</strong>4<strong>-&gt;</strong>5</td>
            <td width="63%">5</td>
        </tr>
        <tr>
            <td colspan="3" width="97%">A(4)=11+5+10=26</td>
        </tr>
        <tr>
            <td width="15%">Details:</td>
            <td width="19%">4<strong>-&gt;</strong>1<strong>-&gt;</strong>2</td>
            <td width="63%">11</td>
        </tr>
        <tr>
            <td width="15%">　</td>
            <td width="19%">4<strong>-&gt;</strong>3</td>
            <td width="63%">5</td>
        </tr>
        <tr>
            <td width="15%">　</td>
            <td width="19%">4<strong>-&gt;</strong>5</td>
            <td width="63%">10</td>
        </tr>
        <tr>
            <td colspan="3" width="97%">A(5)=10</td>
        </tr>
        <tr>
            <td width="15%">Details:</td>
            <td width="19%">5<strong>-&gt;</strong>4<strong>-&gt;</strong>1<strong>-&gt;</strong>2</td>
            <td width="63%">10</td>
        </tr>
    </tbody>
</table>
<p>The
accumulation degree of a tree is the maximal accumulation degree among
its nodes. Here your task is to find the accumulation degree of the
given trees.</p>
</div>
</div>
<p>Input</p>
<div class="ptx" lang="en-US">
<p>The first line of the input is an integer <em>T</em> which indicates the number of test cases. The first line of each test case is a positive integer <em>n</em>. Each of the following <em>n</em> - 1 lines contains three integers <em>x</em>, <em>y</em>, <em>z</em> separated by spaces, representing there is an edge between node <em>x</em> and node <em>y</em>, and the capacity of the edge is <em>z</em>. Nodes are numbered from 1 to <em>n</em>.<br>All the elements are nonnegative integers no more than 200000. You may assume that the test data are all tree metrics.</p>
</div>
<p>Output</p>
<div class="ptx" lang="en-US">
<p>For each test case, output the result on a single line. <br>　</p>
</div>
<p>Sample Input</p>
<pre class="sio">1<br>5<br>1 2 11<br>1 4 13<br>3 4 5<br>4 5 10<br></pre>
<p>Sample Output</p>
<pre class="sio">26</pre>
<p>Source</p>
<div class="ptx" lang="en-US"><a href="http://acm.pku.edu.cn/JudgeOnline/searchproblem?field=source&amp;key=South+Central+China+2008+hosted+by+NUDT">South Central China 2008 hosted by NUDT</a></div>
<br>这道题的基本思想是树形DP，如果不能理解的话请试图把双向边看成两个单向边，再比划比划就出来了。<br>当然不一定非要以边做为DP的单元，也可以归到边上（如果你有那份心的话）。<br>比赛的时候因为数据量大而Stack Overflow，一直想写人工模拟栈，但因为没写过，在比赛中写不出来。<br><br>五一节虚心的跟alpc62学习了怎么写人工模拟栈，核心思想就是将同一个DFS内的不同DFS做个标记，这样在出栈的时候就可以判断自己所处的位置，也就知道自己该采取什么行动了。<br>比如<br>void DFS(int x) {<br>&nbsp;&nbsp;&nbsp; for(int i = 0; i &lt; head[x].size(); ++i) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  DFS(head[x][i]);<br>&nbsp;&nbsp;&nbsp; }<br>}<br>如果把(x, i)这个2元组压入栈也就知道自己现在所处的地方了。<br>如果有更多的内部DFS，同样是加对应的标记。<br><br>当然，BFS也是一种很好的选择（应该说大多数队伍会选择BFS而不是人工模拟栈）<br><br>//Accumulation Degree in BFS<br><br>#include &lt;vector&gt;<br>#include &lt;algorithm&gt;<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>#define Min(a, b) (a&lt;b?a:b)<br>#define Max(a, b) (a&gt;b?a:b)<br><br>struct Node <br>{<br>&nbsp;&nbsp; &nbsp;int x, i, pre;<br>&nbsp;&nbsp; &nbsp;Node() {}<br>&nbsp;&nbsp; &nbsp;Node(int xx, int ii, int pp) {x=xx, i = ii, pre=pp;}<br>};<br><br>struct Edge<br>{<br>&nbsp;&nbsp; &nbsp;int x, w, dp;<br>&nbsp;&nbsp; &nbsp;Edge() {}<br>&nbsp;&nbsp; &nbsp;Edge(int xx, int ww, int dd=0) { x=xx,w=ww,dp=dd;}<br>};<br><br>const int N = 200010;<br>vector&lt;Edge&gt; e[N];<br>bool chk[N];<br>int n, flow[N];<br><br>void solve() {<br>&nbsp;&nbsp; &nbsp;int i, j, k;<br>&nbsp;&nbsp; &nbsp;vector&lt;Node&gt; Q;<br><br>&nbsp;&nbsp; &nbsp;fill(chk, chk + n, 0);<br>&nbsp;&nbsp; &nbsp;fill(flow, flow + n, 0);<br><br>&nbsp;&nbsp; &nbsp;for(i = 0; i &lt; n &amp;&amp; e[i].size()!=1; ++i);<br>&nbsp;&nbsp; &nbsp;int st = 0, end = 0;<br>&nbsp;&nbsp; &nbsp;chk[i] = 1;<br>&nbsp;&nbsp; &nbsp;for(j = 0; j &lt; e[i].size(); ++j) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Q.push_back(Node(i, j, -1)); <br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end++;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;chk[e[i][j].x] = 1;<br>&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;while(st &lt; end) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;int x = e[Q[st].x][Q[st].i].x, pre = Q[st].pre;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(i = 0; i &lt; e[x].size(); ++i) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(!chk[e[x][i].x]) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Q.push_back(Node(x, i, st));<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end++;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;chk[e[x][i].x] = 1;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;++st;<br>&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;for(i = end-1; i &gt;= 0; --i) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;int x = Q[i].x, pre = Q[i].pre, idx = Q[i].i;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(e[e[x][idx].x].size() == 1) e[x][idx].dp = e[x][idx].w;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else e[x][idx].dp = Min(e[x][idx].dp, e[x][idx].w);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(pre == -1) continue;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;int prex = Q[pre].x, preidx = Q[pre].i;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;e[prex][preidx].dp += e[x][idx].dp;<br>&nbsp;&nbsp; &nbsp;}<br><br><br>&nbsp;&nbsp; &nbsp;for(i = 0; i &lt; e[Q[0].x].size(); ++i) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;flow[Q[0].x] += e[Q[0].x][i].dp;<br>&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;for(i = 0; i &lt; end; ++i) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;int x = Q[i].x, pre = Q[i].pre, idx = Q[i].i;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;int y = e[x][idx].x, xx;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(xx = 0; xx &lt; e[y].size() &amp;&amp; e[y][xx].x != x; ++xx);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(pre == -1) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;e[y][xx].dp = e[y][xx].w;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;e[y][xx].dp = Min(e[y][xx].dp, e[y][xx].w);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(j = 0; j &lt; e[y].size(); ++j) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;flow[y] += e[y][j].dp;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(j = 0; j &lt; e[y].size(); ++j) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;int yy = e[y][j].x;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(yy == x) continue;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(k = 0; k &lt; e[yy].size() &amp;&amp; e[yy][k].x != y; ++k);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;e[yy][k].dp = flow[y] - e[y][j].dp;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;}<br><br>&nbsp;&nbsp; &nbsp;int max = 0;<br>&nbsp;&nbsp; &nbsp;for(i = 0; i &lt; n; ++i) <br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;max = Max(max, flow[i]);<br>&nbsp;&nbsp; &nbsp;printf("%d\n", max);<br>}<br><br>int main() {<br>&nbsp;&nbsp; &nbsp;int ntc;<br>&nbsp;&nbsp; &nbsp;int i;<br>&nbsp;&nbsp; &nbsp;int x, y, w;<br>&nbsp;&nbsp; &nbsp;scanf("%d", &amp;ntc);<br>&nbsp;&nbsp; &nbsp;while(ntc--) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;scanf("%d", &amp;n);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(i = 0; i &lt; n; ++i) e[i].clear();<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(i = 0; i &lt; n-1; ++i) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;scanf("%d %d %d", &amp;x, &amp;y, &amp;w);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;--x; --y;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;e[x].push_back(Edge(y, w));<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;e[y].push_back(Edge(x, w));<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;solve();<br>&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;return 0;<br>}<br><br>
<br> <img src ="http://www.cppblog.com/sicheng/aggbug/48928.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sicheng/" target="_blank">oyjpart</a> 2008-05-05 20:59 <a href="http://www.cppblog.com/sicheng/archive/2008/05/05/48928.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>08年05月05日</title><link>http://www.cppblog.com/richardhe/archive/2008/05/05/48900.html</link><dc:creator>RichardHe</dc:creator><author>RichardHe</author><pubDate>Mon, 05 May 2008 07:31:00 GMT</pubDate><guid>http://www.cppblog.com/richardhe/archive/2008/05/05/48900.html</guid><wfw:comment>http://www.cppblog.com/richardhe/comments/48900.html</wfw:comment><comments>http://www.cppblog.com/richardhe/archive/2008/05/05/48900.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/richardhe/comments/commentRss/48900.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/richardhe/services/trackbacks/48900.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: CEGUI的中文输入:<br>目前为止为我解决的也是参考别人的方法改的代码,不知道哪位兄台有更好的解决方案?<br>源码如下:&nbsp;&nbsp;<a href='http://www.cppblog.com/richardhe/archive/2008/05/05/48900.html'>阅读全文</a><img src ="http://www.cppblog.com/richardhe/aggbug/48900.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/richardhe/" target="_blank">RichardHe</a> 2008-05-05 15:31 <a href="http://www.cppblog.com/richardhe/archive/2008/05/05/48900.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>随便写个strcmp()函数，看看大家能否有更简洁的实现</title><link>http://www.cppblog.com/socketref/archive/2008/05/05/48844.html</link><dc:creator>放屁阿狗</dc:creator><author>放屁阿狗</author><pubDate>Sun, 04 May 2008 18:57:00 GMT</pubDate><guid>http://www.cppblog.com/socketref/archive/2008/05/05/48844.html</guid><wfw:comment>http://www.cppblog.com/socketref/comments/48844.html</wfw:comment><comments>http://www.cppblog.com/socketref/archive/2008/05/05/48844.html#Feedback</comments><slash:comments>15</slash:comments><wfw:commentRss>http://www.cppblog.com/socketref/comments/commentRss/48844.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/socketref/services/trackbacks/48844.html</trackback:ping><description><![CDATA[<br>return true if equal<br><br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">bool&nbsp;</span><span style="color: #008080;">strcmp</span><span style="color: #000000;">(&nbsp;char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;d</span><span style="color: #000000;">,</span><span style="color: #000000;">char&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;s){<br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(&nbsp;d</span><span style="color: #000000;">==</span><span style="color: #0