题解 | #最长回文子串#

最长回文子串

https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

//判断是否为回文,只要确定反转后是否相同即可确定
bool word(string s)
{
    string temp = s;
    reverse(s.begin(), s.end());
    if(temp != s) 
        return false;
    return true;
}

int main() {
    string s;
    int len=0;
    cin >> s;
    
    for(int i=0; i<s.length(); i++)
    {
        for(int j=s.length(); j>0; j--)
        {
            string sub = s.substr(i,j);
            if(word(sub))
            {
                if(sub.size() > len)
                    len = sub.size();
            }
        }
    }
    cout << len << endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

头像
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务