L2-008 最长对称子串

暴力枚举就可以,但是注意最小对称串为 1个字母长度为1

#include <bits/stdc++.h>
using namespace std;
#define int long long 
using pii=pair<int,int>;

bool check(int x,int y,string s)//判断是否为对称子串
{
    int l=x,r=y;
    while(l<r)
    {
        if(s[r]!=s[l])
        {
            return 0;
        }
        l++,r--;
    }
    return 1;
    
}


void solve()
{

    string s;
    int res=1;//注意 如果只有一个字母也是对称子串 长度为1
    getline(cin,s);
    int n=s.size();
    for(int i=0;i<n;i++)//i为每个起点
    {
        for(int j=i+1;j<n;j++)//枚举位置i-位置j的子串
        {
            if(check(i,j,s)){
                res=max(res,j-i+1);
            }
            
        }
        
        
    }
    cout<<res;
    
}



signed main()
{
    int t=1;
  //  cin>>t;
    while(t--) solve();
    
    
}

posted on 2025-03-30 13:37  swj2529411658  阅读(18)  评论(0)    收藏  举报

导航