题解 | #密码截取#

密码截取

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

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

class Password {
  private:
    string s;

  public:
    //构造函数
    Password(const string s);

    //析构函数
    ~Password();

    //判断是否是对称子串
    bool isSymSubStr(const int begin, const int end)const;

    //返回有效密码串的最大长度
    int maxLenOfValidCipherStr(void)const;
};

Password::Password(const string s) {
    this->s = s;
    return;
}

Password::~Password() {
}

bool Password::isSymSubStr(const int begin, const int end)const {
    //同步缩短首尾,如果第一个字符始终与最后一个字符相同,则子串对称
    for (int i = begin, j = end; i < j; i++, j--)
        if (this->s[i] != this->s[j])
            return false;
    return true;
}

int Password::maxLenOfValidCipherStr(void)const {
    //遍历所有可能性,将最大值返回
    int maxlength = 0;
    for (int i = 0; i < this->s.size(); i++)
        for (int j = this->s.size(); j > i; j--)
            if (this->isSymSubStr(i, j))
                if (maxlength < j - i + 1)
                    maxlength = j - i + 1;
    return maxlength;
}

int main() {
    string s;
    while (cin >> s) { // 注意 while 处理多个 case
        Password p(s);
        cout << p.maxLenOfValidCipherStr() << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

king122:专业技能不要写这么多,熟悉和熟练你经不住问,排版有些难看,中间的空隙搞小一点,项目描述的话感觉是从课程中抄下来的,改一改吧,不然烂大街了,每个项目都写一两点,用什么技术实现了什么难点,然后再写一些数字上去像时间又花了90%这样,这样面试会多一些,如果觉得自己的项目还是不够用的话,我有几个大厂最近做过的实习项目,感兴趣的话可以看我简介中的项目地址
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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