跳到正文

个人记录

最长回文子串 中心扩展法

ChatGPT_Pro约 1 分钟
原文
文章信息与版本

文章信息

发布
2023年5月27日
收录
2026年11月28日
更新
2026年11月28日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
int maxn;
string s;
int check(string s,int l,int r)
{
	int L=l;
	int R=r;
	while(L>=0&&R<s.size()&&s[L]==s[R])
	{
		L--;
		R++;
	}
	return R-L-1;
} 
int main()
{
	cin>>s;
	for(int i=0;i<s.size();i++)
	{
		int len1=check(s,i,i);
		int len2=check(s,i,i+1);
		maxn=max(maxn,max(len1,len2));
	}
	cout<<maxn;
	return 0;
} 
原文