题解 | #最长回文子串#

最长回文子串

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

#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool isPalindrome(string str){
    for(int i=0,j=str.size()-1;i<j;i++,j--){
        if(str[i]!=str[j]){
            return false;
        }
    }
    return true;
}
int main() {
    string input;
    while (cin>>input) {
        int max=0;
        for(int i=0;i<=input.size()-1;i++){
            for(int j=input.size()-i;j>=1;j--){
                string temp=input.substr(i,j);
                if(isPalindrome(temp)){
                    if(temp.size()>max){
                        max=temp.size();
                    }
                }
            }
        }
        cout<<max<<endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

04-03 22:39
重庆大学 Java
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客企业服务