题解 | 密码验证合格程序

#include <iostream>
#include <string>
#include <unordered_set>
#include <cctype>

bool isPasswordValid(const std::string& password) {
    // 检查长度
    if (password.length() < 8) {
        return false;
    }

    // 检查字符类型
    bool hasUpper = false, hasLower = false, hasDigit = false, hasSpecial = false;
    for (char ch : password) {
        if (isupper(ch)) {
            hasUpper = true;
        } else if (islower(ch)) {
            hasLower = true;
        } else if (isdigit(ch)) {
            hasDigit = true;
        } else if (ch >= 33 && ch <= 126) {
            hasSpecial = true;
        }
    }
    int typeCount = (hasUpper ? 1 : 0) + (hasLower ? 1 : 0) + (hasDigit ? 1 : 0) + (hasSpecial ? 1 : 0);
    if (typeCount < 3) {
        return false;
    }

    // 检查子串重复
    for (size_t len = 3; len <= password.length(); ++len) {
        for (size_t i = 0; i <= password.length() - len; ++i) {
            std::string substr = password.substr(i, len);
            if (password.find(substr, i + substr.size()) != -1) {
                return false;
            }
        }
    }

    return true;
}

int main() {
    std::string password;
    while (std::getline(std::cin, password)) {
        if (isPasswordValid(password)) {
            std::cout << "OK" << std::endl;
        } else {
            std::cout << "NG" << std::endl;
        }
    }
    return 0;
}

全部评论

相关推荐

小浪_Coding:1. 个人技能排版太乱, 写的技术栈太浅了, 跟测试,自动化相关的太少; 2. 项目开发类的太简单没有亮点, 算法类的项目建议只放一个,最好有自动化,CI/CD, pipline的项目, 需要更换; 3.整体排版需要优化, SOOB打招呼都需要注意等.
我的简历长这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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