7-1 最长对称子串 (25 分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_42623428/article/details/83116952

对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11。

输入格式:

输入在一行中给出长度不超过1000的非空字符串。

输出格式:

在一行中输出最长对称子串的长度。

输入样例:

Is PAT&TAP symmetric?

输出样例:

11
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    getline(cin,s);
    int num=0;
    for(int i=0;i<=s.length()-1;i++){
        int k=i,f=1;
        int t=s.length()-1;
        while(s[k]!=s[t])t--;
        int tt=t;
        for(int j=k;j<=tt;j++){
            if(s[j]!=s[tt]){
                f=0;break;
            }
            tt--;
        }
        if(f==1&&t-k+1>num)num=t-k+1;
    }
    cout<<num<<endl;
}

猜你喜欢

转载自blog.csdn.net/qq_42623428/article/details/83116952