首页 > 其他 > 详细

最长对称子串

时间:2020-02-20 09:49:55      阅读:61      评论:0      收藏:0      [点我收藏+]

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

输入格式:

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

输出格式:

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

输入样例:

Is PAT&TAP symmetric?

输出样例:

11

技术分享图片
#include <bits/stdc++.h>
using namespace std;
string str;
int ans;
int len;
int judge(int fore,int back,int slen){
    while(fore >= 0 && back < len){
        if(str[fore] == str[back])
            slen += 2,fore--,back++;
        else break;
    }
    ans = max(ans,slen);
    return ans;
}
int main(){
    //freopen("in","r",stdin);
    ios::sync_with_stdio(0);
    getline(cin,str);
    len = str.size();
    for(int i = 0; i < len; i++){
        //长度为奇数
        judge(i - 1, i + 1,1);
        //长度为偶数
        judge(i,i + 1,0);
    }
    cout << ans;
    return 0;
}
View Code

 

 

最长对称子串

原文:https://www.cnblogs.com/xcfxcf/p/12334352.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!