牛客网_华为机试_020_牛客网_密码验证合格程序

63 篇文章 1 订阅
28 篇文章 0 订阅

题目描述

密码要求:

 

 

 

1.长度超过8位

 

 

 

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

 

 

 

3.不能有相同长度超2的子串重复

 

 

 

说明:长度超过2的子串


输入描述:

一组或多组长度超过2的子符串。每组占一行

输出描述:

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

示例1

输入

021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000

输出

OK
NG
NG
OK

题目地址:https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841?tpId=37&tqId=21243&tPage=1&rp=&ru=/ta/huawei&qru=/ta/huawei/question-ranking

思路一:按部就班一个一个要求来验证,验证有没有长度超过3的重复子串只要验证有没有长度为3的子串。可以利用sub_str,find函数来实现,也可以两层for循环比较连续3个字符是否相等

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

bool judge(string str){
    if(str.size() <= 8)
        return false;
    
    int flag[4] = {0};
    for(auto it = str.cbegin(); it != str.cend(); ++it)
    {
        if(*it > 'a' && *it < 'z')
            flag[0] = 1;
        else if(*it > 'A' && *it < 'Z')
            flag[1] = 1;
        else if(*it > '0' && *it < '9')
            flag[2] = 1;
        else
            flag[3] = 1;
    }
    if(flag[0]+flag[1]+flag[2]+flag[3]<3)
        return false;
    
    for(int i = 0; i < str.size()-6; i++)
    {
        string sub_str = str.substr(i, 3);
        if(str.find(sub_str, i + 3) != string::npos)
            return false;
    }
    return true;
}

int main()
{
    const string OK = "OK";
    const string NG = "NG";
    string str = "";
    while(cin >> str){
        if(judge(str))
            cout << OK << endl;
        else
            cout << NG << endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值