华为机试题——HJ20 密码验证合格程序

描述

密码要求:

1.长度超过8位

2.包括大小写字母.数字.其它符号,以上四种至少三种

3.不能有长度大于2的不含公共元素的子串重复 (注:其他符号不含空格或换行)

数据范围:输入的字符串长度满足 1 \le n \le 100 \1≤n≤100 

输入描述:

一组字符串。

输出描述:

如果符合要求输出:OK,否则输出NG

示例1

输入:

021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000

复制输出:

OK
NG
NG
OK
#include <iostream>
#include <algorithm>


//判断是否有长度大于2的公共字串
int checkSubstr(std::string subStr, std::string str)
{
    int count = 0;
    for(int i = 0; i < str.length()-3; ++i)
    {
        std::string tempstr = str.substr(i, 3);
        if(subStr == tempstr)
        {
            count++;
        }
    }
    if(count >= 2)
    {
        return 1;
    }
    return 0;
}

/**
 * @brief           其他符号
 *  !"#$%&'()*+,-./     (ASCII码:0x21~0x2F)
    :;<=>?@             (ASCII码:0x3A~0x40)
    [\]^_`              (ASCII码:0x5B~0x60)
    {|}~                (ASCII码:0x7B~0x7E)
 * 
 * @return int 
 */
std::string verifyPassword(std::string str)
{

    bool lenflag  = false;          //第一个条件满足的标志
    bool charFlag = false;          //第二个条件满足的标志
    bool substrFlag = true;        //第三个条件满足的标志
    int chararr[4] = {0,0,0,0};      //统计满足第二个条件的个数

    //1.长度超过8位
    if(str.length() >= 8)
    {
        lenflag = true;
    }

    //2.包括大小写字母.数字.其它符号,以上四种至少三种
    for(int i = 0; i < str.length(); ++i)
    {
        if(str[i]>= '1' && str[i] <= '9')
        {
            chararr[0] = 1;
        }
        if(str[i]>= 'a' && str[i] <= 'z')
        {
            chararr[1] = 1;
        }
        if(str[i]>= 'A' && str[i] <= 'Z')
        {
            chararr[2] = 1;
        }
        if((str[i]>= 0x21 && str[i] <= 0x2F) || (str[i]>= 0x3A && str[i] <= 0x40) || (str[i]>= 0x5B && str[i] <= 0x60) || (str[i]>= 0x7B && str[i] <= 0x7E))
        {
            chararr[3] = 1;
        }
    }

    int charCount = 0;
    for(int i = 0; i < 4; ++i)
    {
        if(chararr[i] == 1)
        {
            charCount++;
        }
    }
    if(charCount >= 3)
    {
        charFlag = true;
    }

    //3.不能有长度大于2的不含公共元素的子串重复
    for(int i = 0; i < str.length()-3; ++i)
    {
        std::string tempstr = str.substr(i, 3);
        int flag = checkSubstr(tempstr, str);
        if(flag)
        {
            substrFlag = false;
            break;
        }
    }
    if(lenflag && charFlag && substrFlag)
    {
        return "OK";
    }

    return "NG";
}

//021Abc9Abc1
int main()
{                                              
    std::string str;
    while(getline(std::cin, str))
    {
        std::cout << verifyPassword(str) << std::endl;
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值