L2-008. 最长对称子串

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>

using namespace std;

int main(void){
  char str[1005];
  gets(str);
  int len = strlen(str);
  int ans = 1;
  for(int i = 0; i < len; i++){
    int L = i - 1,R = i + 1;
    int h = 1;
    while(L >= 0 && R <= len - 1){
      if(str[L] == str[R]){
        L--;
        R++;
        h += 2;
      }
      else{
        break;
      }
      ans = max(ans,h);
    }
  }
  int h;
  for(int i = 1; i < len; i++){
    if(str[i] == str[i - 1]){
      int h = 2;
      int L = i - 2,R = i + 1;
        while(L >= 0 && R <= len - 1){
          if(str[L] == str[R]){
           L--;
             R++;
            h += 2;
          }
          else{
            break;
          }
          ans = max(ans,h);
        }
    }
  }
  cout<<ans<<endl;
  return 0;
}

猜你喜欢

转载自blog.csdn.net/gyh0730/article/details/80259906