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

<channel>
	<title>Programming Life with Music &#187; 备忘</title>
	<atom:link href="http://jackaldire.com/tag/%e5%a4%87%e5%bf%98/feed/" rel="self" type="application/rss+xml" />
	<link>http://jackaldire.com</link>
	<description>JackalDire &#039;s Blog</description>
	<lastBuildDate>Tue, 13 Jul 2010 08:29:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>[备忘]倒置字符串中的单词</title>
		<link>http://jackaldire.com/200908/reverse-token/</link>
		<comments>http://jackaldire.com/200908/reverse-token/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 14:24:08 +0000</pubDate>
		<dc:creator>JackalDire</dc:creator>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[备忘]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://jackaldire.com/?p=70</guid>
		<description><![CDATA[输入：一个字符串，单词用某个特定符号分割（比如空格） 输出：一个字符串，单词顺序和原串相反 看到倒置，一般的做法是用栈，要么自己建个数组、要么STL，或者递归用程序栈。 优雅的递归 void reverse_token&#40;&#41; &#123; char str&#91;MAX&#93; = &#123;0&#125;; if &#40;scanf&#40;&#34;%[^#]&#34;, str&#41; != EOF&#41; &#123; //利用scanf的正则式特性 getchar&#40;&#41;; reverse_token&#40;&#41;; printf&#40;&#34;%s &#34;, str&#41;; &#125; &#125; STL list void reverse_token&#40;&#41; &#123; char tmp&#91;MAX&#93;; list&#60;string&#62; stack; while &#40;cin.getline&#40;tmp, MAX, '#'&#41;&#41; stack.push_front&#40;string&#40;tmp&#41;&#41;; copy&#40;stack.begin&#40;&#41;, stack.end&#40;&#41;, ostream_iterator&#60;string&#62;&#40;cout,&#34; &#34;&#41;&#41;; &#125; 如果是处理字符串, 而不是stdin, 可以改用sscanf()或者STL的范型算法find, 更标准的做法是strtok()。想到了javascript里的String().split(&#8216;x&#8217;)，直接返回一个分割后的数组，相当的方便。 Related Post 快速排序详细分析 (4) [一道面试题]含有*的字符串匹配问题 (5) Fibonacci数的巧妙求法 [...]]]></description>
			<content:encoded><![CDATA[<p>输入：一个字符串，单词用某个特定符号分割（比如空格）<br />
输出：一个字符串，单词顺序和原串相反</p>
<p>看到倒置，一般的做法是用栈，要么自己建个数组、要么STL，或者递归用程序栈。</p>
<h2>优雅的递归</h2>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> reverse_token<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">char</span> str<span style="color: #009900;">&#91;</span>MAX<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%[^#]&quot;</span><span style="color: #339933;">,</span> str<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> EOF<span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//利用scanf的正则式特性</span>
      getchar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      reverse_token<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s &quot;</span><span style="color: #339933;">,</span> str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span id="more-70"></span></p>
<h2>STL list</h2>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> reverse_token<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">char</span> tmp<span style="color: #009900;">&#91;</span>MAX<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    list<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> stack<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>cin.<span style="color: #202020;">getline</span><span style="color: #009900;">&#40;</span>tmp<span style="color: #339933;">,</span> MAX<span style="color: #339933;">,</span> <span style="color: #ff0000;">'#'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> stack.<span style="color: #202020;">push_front</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">string</span><span style="color: #009900;">&#40;</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    copy<span style="color: #009900;">&#40;</span>stack.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> stack.<span style="color: #202020;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> ostream_iterator<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">cout</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
如果是处理字符串, 而不是stdin, 可以改用sscanf()或者STL的范型算法find, 更标准的做法是strtok()。想到了javascript里的String().split(&#8216;x&#8217;)，直接返回一个分割后的数组，相当的方便。</p>

	<h4>Related Post</h4>
	<ul class="st-related-posts">
	<li><a href="http://jackaldire.com/200908/quick-sort-analysis/" title="快速排序详细分析 (2009年08月27日)">快速排序详细分析</a> (4)</li>
	<li><a href="http://jackaldire.com/200911/string-matching-with-wildcard/" title="[一道面试题]含有*的字符串匹配问题 (2009年11月25日)">[一道面试题]含有*的字符串匹配问题</a> (5)</li>
	<li><a href="http://jackaldire.com/200905/smart-way-to-solve-fibonacci/" title="Fibonacci数的巧妙求法 (2009年05月6日)">Fibonacci数的巧妙求法</a> (2)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://jackaldire.com/200908/reverse-token/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
