题解 | #密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

#include <iostream>
using namespace std;

int main() {
    // 暴力法
    string str;
    getline(cin, str);

    // 1.长度超过8位
    // 长度
    if (str.length() < 8)
    {
        cout << "NG" << endl;
        return 0;
    }

    // 2.包括大小写字母.数字.其它符号,以上四种至少三种
    int typeCount[4] = {0};
    for (char ch : str)
    {
        if (ch >= 'A' && ch <= 'Z') // 有大写
        {
            typeCount[0] = 1;
        }
        else if (ch >= 'a' && ch <= 'z')  // 有小写
        {
            typeCount[1] = 1;
        }
        else if (ch >= '0' && ch <= '9')  // 有数字
        {
            typeCount[2] = 1;
        }
        else    
        {
            typeCount[3] = 1;
        }
    }

    if (typeCount[0] + typeCount[1] + typeCount[2] + typeCount[3] < 3)
    {
        cout << "NG" << endl;
        return 0;
    }

    // 3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
    bool repeate = false;
    for (size_t i = 0; i < str.length() - 6; i++)
    {
        for (size_t j = i+3; j < str.length(); j++)
        {
            if (str.substr(i, 3).compare(str.substr(j, 3)) == 0)
            {
                repeate = true;
                break;
            }
        } 

        if (repeate)
        {
            break;
        }
    }
    
    if (repeate)
    {
        cout << "NG" << endl;
    }
    else    
    {
        cout << "OK" << endl;
    }
    
    return 0;
    // 


    // 正则法
     
} 

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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