首页 > 其他 > 详细

马拉车求最大回文字串

时间:2016-02-26 18:48:55      阅读:166      评论:0      收藏:0      [点我收藏+]

改进的代码 (输入字符串为s): 

int getLongestPalindrome(string s) {

  string str;

  str.push_back(‘&‘);

  for (char item : s) {

    str.push_back(‘#‘);

    str.push_back(item);

  }

  str.push_back(‘#‘);

  str.push_back(‘$‘);

  int pos = 0;

  vector<int> len(str.size(), 0);

  for (int i = 0; i < str.size(); i++) {

    if (i <= pos+len[pos])

      len[i] = min(len[2*pos-i], pos+len[pos]-i);

    while (str[i-len[i]-1] == str[i+len[i]+1] )

      len[i]++;

  if (i+len[i] > pos+len[pos])

    pos = i;

  }

  return *(max_element(len.begin(), len.end()));

}

Reference:

[1] http://blog.csdn.net/dyx404514/article/details/42061017

马拉车求最大回文字串

原文:http://www.cnblogs.com/wubdut/p/5221440.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!