最长对称子串

题目详情 - L2-008 最长对称子串 (25 分) (pintia.cn)

STL:21分,另外的4分超时..

复制代码
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    string str;
    getline(cin,str);
    int maxn=-1;
    for(int i=0;i<str.size();i++)
    {
        string ch;
        for(int j=i;j<str.size();j++)
        {
            ch+=str[j];
            string c;
            c=ch;
            reverse(ch.begin(),ch.end());
            if(ch==c) 
            {
                int len=ch.size();
                maxn=max(maxn,len);
            }
            reverse(ch.begin(),ch.end());
        }
    }
    cout<<maxn;
    return 0;
} 
复制代码
复制代码
#include<iostream>
using namespace std;
int main(){
    string str;
    getline(cin,str);
    int maxn=-1;
    for(int i=0;i<str.size();i++)
    {
        int l=i,r=i+1,sum=0;
        while(str[l]==str[r]&&l>=0&&r<str.size())
        {
            l--;
            r++;
            sum+=2;
        }
        maxn=max(maxn,sum);
    }
    for(int i=0;i<str.size();i++)
    {
        int l=i-1,r=i+1;
        int sum=1;
        while(str[l]==str[r]&&l>=0&&r<str.size())
        {
            l--;
            r++;
            sum+=2;
        }
        maxn=max(maxn,sum);    
    }
    cout<<maxn;
    return 0;
}
复制代码

 

posted @   小志61314  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示