<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
  <channel>
    <title>Tomb Raider</title>
    <description></description>
    <link>http://TombRaider.javaeye.com</link>
    <language>UTF-8</language>
    <copyright>Copyright 2003-2008, JavaEye.com</copyright>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <generator>JavaEye - 做最棒的软件开发交流社区</generator>
          <item>
        <title>转贴：语言暴力、煽动性、讲道理和和谐盛世</title>
        <author>Teal'c</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://TombRaider.javaeye.com">Teal'c</a>&nbsp;
                    链接：<a href="http://TombRaider.javaeye.com/blog/195012" style="color:red;">http://TombRaider.javaeye.com/blog/195012</a>&nbsp;
          发表时间: 2008年05月20日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <a href="http://bbs.hoopchina.com/htm_data/5/0804/332174.html" target="_blank">http://bbs.hoopchina.com/htm_data/5/0804/332174.html</a><br />看了公子那么多文章，又被头像误导（杨立新？），今天才知道公子比我还小两岁。<img src="/images/smiles/icon_cry.gif"/>
          <br/><br/>
          <span style="color:red;">
            <a href="http://TombRaider.javaeye.com/blog/195012#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Tue, 20 May 2008 20:43:01 +0800</pubDate>
        <link>http://TombRaider.javaeye.com/blog/195012</link>
        <guid>http://TombRaider.javaeye.com/blog/195012</guid>
      </item>
          <item>
        <title>Ruby每周一测 - 容易记的电话号码</title>
        <author>Teal'c</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://TombRaider.javaeye.com">Teal'c</a>&nbsp;
                    链接：<a href="http://TombRaider.javaeye.com/blog/184506" style="color:red;">http://TombRaider.javaeye.com/blog/184506</a>&nbsp;
          发表时间: 2008年04月20日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <div class="quote_title">Eastsun 写道</div><div class="quote_div">另外,按LZ"通过查找字典把输入的数字变成可能的字母输出"的意思<br />我的上面代码得到的结果字符串中不会包含数字,也就是某些号码会无解<br />但我看了下原题,貌似是可以包含数字的,譬如号码"1-800-PICK-UPS "应该可以以"1800-PICK-UPS "形式作为一个解.<br />我写了个容许数字出现的代码,不过稍微复杂些.</div><br />贴一个python版本的，惭愧，弄了一晚上<img src="/images/smiles/icon_redface.gif"/><br /><pre name="code" class="python">
#-*- coding: utf-8 -*-
#!/usr/bin/env python
from __future__ import with_statement

button_mapping = {
    'A' : 2, 'B' : 2, 'C' : 2,
    'D' : 3, 'E' : 3, 'F' : 3,
    'G' : 4, 'H' : 4, 'I' : 4,
    'J' : 5, 'K' : 5, 'L' : 5,
    'M' : 6, 'N' : 6, 'O' : 6,
    'P' : 7, 'Q' : 7, 'R' : 7, 'S' : 7,
    'T' : 8, 'U' : 8, 'V' : 8,
    'W' : 9, 'X' : 9, 'Y' : 9, 'Z' : 9,
}


"""
{ word_signature:word }
"""
signatures = {}

def suffeient(word):
    """
    remove empty and un-mapped strings 
    """
    if not word:
        return False
    if len(word) >= 2 and word[-2] == "\'":
        return False
    for c in word:
        if not (c.upper() in button_mapping.keys()):
            return False
    return True

def make_signature(word, sig_dict):
    #def action_maker(seq):
    #    return lambda c: seq.append(c)
    sig_tmp = []
    #f = action_maker(sig_tmp)
    [ sig_tmp.append(str(button_mapping[c])) for c in [c.upper() for c in word] ]
    sig_dict[''.join(sig_tmp)] = word


def make_signatures(words):
    global signatures
    [ make_signature(word, signatures) for word in words if suffeient(word) ]
    
   
def build_word_list(number, words_dict, indent=0):
    """
    Brief introduction of the algorithm:
    
    Find all of the signatures that are contained in the number in the signature dict
    Sort the matched keys in decend order
    For each of the matched element:
        divide the element into 3 parts: header, matched, reminder
        call the build_word_list recursively to find the possible matched word
        for the header and reminder parts, respectively
        examine the result and return
        
    There might be more than one word having the same signature, it depends the
    signature-building algorithm and the signature storage's data structure. In
    this solution the words having the duplicated signatures are omitted to
    reduce the complexity of the implementation; this simplification should not
    (um..., maybe not:-)) affect the correctness of the algorithm.
    """
    
    def compose_result(header, matched, reminder):
        ret = "%s-%s-%s" % (header, matched, reminder)
        if ret[0] == '-':
            return ret[1:]
        elif ret[-1] == '-':
            return ret[0:-1]
        else:
            return ret
    
    #print '%sinput:%s' % (indent * '.', number )
    if not number:
        return True, number
    if number in words_dict.keys():
        #print '%smatches:%s' % ( indent * '.', words_dict[number] )
        return True, number
    else:
        possible_matches = [num for num in words_dict.keys() if num in number]
        possible_matches.sort(lambda a,b:len(a) &lt; len(b) and 1 or -1)
        #print '%spossible_matches: %s' % (indent * '.', [ (word, words_dict[word]) for word in possible_matches ])
        
        for matched in possible_matches:
            header_num, matched_num, reminder_num = number.partition(matched)
            #print "header_num:%s, matched_num:%s, reminder_num:%s" % (header_num, matched_num, reminder_num)
            header_matched, header_word = build_word_list(header_num,
                                                          words_dict, indent + 1)
            reminder_matched, reminder_word = build_word_list(reminder_num,
                                                              words_dict, indent + 1)
            if header_matched and reminder_matched:
                return True, compose_result(header_word, matched, reminder_word)
            elif header_matched and not reminder_matched:
                return False, compose_result(header_word, matched, reminder_num)
            elif not header_matched and reminder_matched:
                return False, compose_result(header_num, matched, reminder_word)
            elif not header_matched and not reminder_matched:
                return False, compose_result(header_num, matched, reminder_num)
            else:
                pass
        return False, number
    
def output(build_result):
    global signatures
    ret = ""
    grouped_result = build_result[1].split('-')
    #print "build result %s, %s:" % build_result
    for group in grouped_result:
        if signatures.has_key(group):
            ret = ret + "-" + signatures[group]
        else:
            ret = ret + "-" + group
    return ret[1:]

def input(number):
    return ''.join(str(number).split('-'))


if __name__ == '__main__':
    with file('words') as f:
        trimed = [ line[0:-1] for line in f ]
        make_signatures(trimed)
        
        word_list = build_word_list('118737829', signatures)
        print word_list and output(word_list) or "None"
        
        word_list = build_word_list('7425877', signatures)
        print word_list and output(word_list) or "None"
        
        word_list = build_word_list('74258777425877', signatures)
        print word_list and output(word_list) or "None"
        
        word_list = build_word_list(input('1-800-7425-877'), signatures)
        print word_list and output(word_list) or "None"
</pre>
          <br/><br/>
          <span style="color:red;">
            <a href="http://TombRaider.javaeye.com/blog/184506#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sun, 20 Apr 2008 02:05:57 +0800</pubDate>
        <link>http://TombRaider.javaeye.com/blog/184506</link>
        <guid>http://TombRaider.javaeye.com/blog/184506</guid>
      </item>
      </channel>
</rss>