码迷,mamicode.com
首页 > 其他好文 > 详细

马拉车求最大回文字串

时间:2016-02-26 18:51:19      阅读:177      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!