求两个字符串最大公共子串

def getCommonStr(str1, str2):
"""求两个字符串最大公共子串"""

  rst_str = ''
  curr_num, begin_num = 0, 0
  i = len(str1)
  while i:
    curr_num += 1
    ex_str = str1[begin_num : curr_num]
    if ex_str in str2:
      if len(ex_str) > len(rst_str):
      rst_str = ex_str
    else:
      begin_num = curr_num - 1
      ex_str = str1[begin_num:curr_num]
      if ex_str in str2:
        if len(ex_str) > rst_str:
        rst_str = ex_str
      else:
        begin_num = curr_num
    i -= 1
  return rst_str

posted @ 2014-05-19 17:18  zizi_come  阅读(255)  评论(0编辑  收藏  举报