思考人生中...

博客记录了使用C++进行暴力破解时遇到的问题。代码是直接抄写的,自己编写存在问题,具体问题出在重复尾缀上,如bbcccbbb等情况,且尚未探明原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接抄的…自己写的有点问题…放在下面

class Solution {
public:
    string longestPalindrome(string s) 
    {
        int i,j,p=0,l=0,n=s.size();
        for(i=0;i<n;i++)
        {
            for(j=1;j<=min(i,n-i);j++)
                if(s[i-j]!=s[i+j])  
                    break;
            if(l<j)
            {
                l=j;
                p=i;
            }
        }
        l--;
        int P=0,L=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<=min(i,n-i);j++)
                if(s[i-j]!=s[i+j+1])  
                    break;
            if(L<j)
            {
                L=j;
                P=i;
            }
        }
        L--;
        if(2*l+1>2*L+2) return s.substr(p-l,2*l+1);
        else            return s.substr(P-L,2*L+2);
    }
};

尝试暴力破解

class Solution {
public:
    bool testFx1(string x, int len) {
        //cout << "work" << x << len << endl;
        for (int r = 0, j = len; r < j; ++r, --j) {
            //cout << x[r] << "," << x[j] << endl;
            if (x[r] != x[j]) {
                //cout << x[r] << "," << x[j] << endl;
                return false;
            }
        }
        return true;
    }
/*
    string testFx2(string _In,  int _Begin_Point, int _End_Point) {
        string _Out;
        while (_Begin_Point <= _End_Point) {
            _Out.push_back(_In[_Begin_Point]);
            _Begin_Point++;
            //cout << _Out << endl;
        }
        return _Out;
    }
*/  
    string longestPalindrome(string s) {
        int size = s.size();
        if (size < 2) return s;
        int maxL = 1;
        while (s[maxL - 1] == s[maxL] && maxL < size) maxL++;
        string out = s.substr(0, maxL);
        for (int r = 0; r < size + 1; ++r) {
            for (int j = r + maxL; j < size; ++j) {
                //cout << "Ready for test" << r << ":" << s[r] << s[j] << endl;
                if (s[r] == s[j])
                    if (testFx1(s.substr(r,j+1), (j - r))) {
                        maxL = j - r + 1;
                        out = s.substr(r,j+1);
                        //cout << out << endl;
                    }
            }
        }
        //cout << out.size() << endl;
        if (out.front() != out.back()) out.pop_back();
        return out;
    }
};

问题出在重复尾缀…没有探明原因…
出错例如bbcccbbb等

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值