CCCC L2-008. 最长对称子串

https://www.patest.cn/contests/gplt/L2-008

题解:想法是扫一遍string,将每一个s[i]作为对称轴,写一个判定函数:不断向两边延伸直到不是回文串为止。

          然后发现没有考虑形如werrew的回文串,所以改了一下,注意两个判定函数的循环方式

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include<stack>
#include<set>
#include<string.h>
#include<list>
#define pb push_back
#define mp make_pair
#define _for(i, a, b) for (int i = (a); i<(b); ++i)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)

using namespace std;
const  int N = 50 + 5;
int len;
string s;
int test(int i) {
    int l =i, r = i;
    int cnt = 0;
    while (l-1 >= 0 && r +1<= len-1) {
        l--; r++;
        if (s[l] == s[r])cnt++;
        else break;
    }
    return cnt;
}
int test2(int i) {
    int l = i, r = i + 1;
    int cnt = 0;
    while (l  >= 0 && r  <= len - 1) {
        if (s[l] == s[r])cnt++;
        else break;
        l--; r++;
        
    }
    return cnt;
}
int main() {
    
    getline(cin, s);
     len = s.length();
     int mx = 0;
    //1000*500
    _for(i, 0, len) {
        mx = max(mx, test(i));
    }
    
    
    int ans=mx*2+1;
    mx = 0;
    _for(i, 0, len) {
        mx = max(mx, test2(i));
    }
    ans = max(ans, mx * 2);
    cout << ans << endl;
    system("pause");
}
/*qwerrewq*/
成功的路并不拥挤,因为大部分人都在颓(笑)
原文地址:https://www.cnblogs.com/SuuT/p/8670305.html