首页 > 其他 > 详细

最长回文子串

时间:2018-06-06 20:36:55      阅读:211      评论:0      收藏:0      [点我收藏+]
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

int lengthRev(string str) {
  int n = str.length();
  int MAX = 1;

  int b;
  int e;
  int c = 0;
  for (int i = 0; i < n; i++) {
    b = i;
    e = i;
    c = 0;
    while (e + 1 < n && str[e + 1] == str[b]) e++;
    i = e;
    while (b - c >= 0 && e + c < n && str[b - c] == str[e + c]) c++;
    if (2 * (c - 1) + e - b + 1 > MAX) MAX = 2 * (c - 1) + e - b + 1;
  }

  return MAX;
}

int main() {
  int n;
  cin >> n;

  string s;

  while (n--) {
    cin >> s;
    cout << lengthRev(s) << endl;
  }

  return 0;
}

 

最长回文子串

原文:https://www.cnblogs.com/tangjicheng/p/9146777.html

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