题解 | #密码截取#

密码截取

https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1

#include <algorithm>
#include <iostream>
using namespace std;
//判断是不是回文子串
bool isTrue(string &s,int left,int right)
{
    for(int i=left,j=right;i<j;i++,j--)
    {
        if(s[i]!=s[j])
            return  false;
    }
    return true;
}
int main() 
{
    string str;
    while(getline(cin,str))
    {
        int maxLen=1;
        for(int i=1;i<str.size();i++)
        {
            for(int j=0;j<i;j++)
            {
                string s=str.substr(j,i-j+1);
			  //如果是回文子串,更新最大回文子串长度
                if(isTrue(s, 0, s.size()-1))
                    maxLen = max(maxLen, i-j+1);
            }
        }
        cout<<maxLen<<endl;
    }
    return 0;

    
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务