华为OJ——密码验证合格程序

密码验证合格程序

题目描述

密码要求:

1.长度超过8

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

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

说明:长度超过2的子串

输入描述:

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

输出描述:

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

输入例子:

021Abc9000

021Abc9Abc1

021ABC9000

021$bc9000

输出例子:

OK

NG

NG

OK

解答代码:

 

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

//判断是否存在长度大于2的子串
int judgeSubStr(string str)
{
    int i,j;
    string subStr1="";
    string subStr2="";
    for(i=0; i<str.length()-3; i++)
    {
        subStr1=str.substr(i,3);
        for(j=i+3; j<str.length()-2; j++)
        {
            subStr2=str.substr(j,3);
            if(subStr1==subStr2)
            {
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    string s1;
    int i,j,k;
    int count=0;
    set<char> letterSmall;//存储小写字母
    set<char> letterBig;//存储大写字母
    set<char> number;//存储数字
    set<char> other;//存储其他字符
    while(cin>>s1)
    {
        //先清空容器
        letterSmall.clear();
        letterBig.clear();
        number.clear();
        other.clear();
        if(s1.length()<=8)
        {
            //cout<<"length"<<endl;
            cout<<"NG"<<endl;
            continue;
        }
        if(judgeSubStr(s1))
        {
            //cout<<"sub"<<endl;
            cout<<"NG"<<endl;
            continue;
        }

        for(i=0; i<s1.length(); i++)
        {
            if(s1[i]>='a'&&s1[i]<='z')
                letterSmall.insert(s1[i]);
            else if(s1[i]>='A'&&s1[i]<='Z')
                letterBig.insert(s1[i]);
            else if(s1[i]>='0'&&s1[i]<='9')
                number.insert(s1[i]);
            else
                other.insert(s1[i]);
        }

        int count=0;
        if(letterSmall.size()>0)//说明存在小写字母
            count++;
        if(letterBig.size()>0)//说明存在大写字母
            count++;
        if(number.size()>0)//说明存在数字
            count++;
        if(other.size()>0)//说明含有其他字符
            count++;
        //cout<<letterSmall.size()<<letterBig.size()<<number.size()<<other.size()<<endl;
        if(count>=3)
        {
            cout<<"OK"<<endl;
        }
        else
        {
            cout<<"NG"<<endl;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值