暴力枚举
复杂度O(n^2) 100W可以接受
注意点ans初值为1
可以剪枝一下,当长度小于ans可以break
C++ 代码
#include<bits/stdc++.h>
#define pb push_back
#define pp poop_back
#define se second
#define fi first
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N=100010,mod=1e9+7;
string s;
int ans=1;
bool judge(int i,int j)
{
while(i<j&&s[i]==s[j])
{
i++;
j--;
}
if(i>=j) return true;
else return false;
}
int main()
{
getline(cin,s);
for(int i=0;i<s.size();i++)
{
for(int j=s.size()-1;j>i;j--)
{
if(j-i+1<=ans) break;
if(s[i]==s[j])
{
if(judge(i,j))
ans=max(ans,j-i+1);
}
}
}
cout<<ans;
return 0;
}