中难题 密码验证合格( 牛客)

密码要求:

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 <string>


using namespace std;

bool IsPassWordOk(string& password){

    if(password.length() <= 8){

        return false;

    }

    int countdaxie = 0 ;
    int countxiaoxie =  0;
    int countfuhao = 0;
    int countshuzi = 0;

    for(auto& chr : password){

        if(isupper(chr)){
            countdaxie++;
        }else if(islower(chr)){
            countxiaoxie++;
        }else if(isdigit(chr)){
            countshuzi++;
        }else if(!isalnum(chr)){
            countfuhao++;
        }
    }

    if( countdaxie + countxiaoxie == 0 || countdaxie + countfuhao == 0 ||
        countdaxie + countshuzi == 0 || countxiaoxie + countfuhao ==0 ||
        countxiaoxie + countshuzi == 0 || countshuzi + countfuhao == 0){

        return false;

    }

    for(int i = 0; i < password.size()-3; ++i){

            string sonstr = password.substr(0,i) + password.substr(i+3);
            if(sonstr.find(password.substr(i, 3)) != string::npos){

                return false;
                break;

            }

    }

    // string mask_str = password;
    // mask_str.erase(mask_str.begin());
    // cout<<"maks is "<<mask_str<<endl;

    // string::iterator istr = password.begin();
    // string::iterator imstr = password.begin()++;
    // int count = 0;
    // while (imstr != mask_str.end())
    // {
    //     while( *imstr == *istr){

    //         ++imstr;
    //         ++istr;
    //         count++;
    //         std::cout<<"count is "<<count<<endl;

    //     }
    //     if(count > 0){

    //         if(count > 2){
    //             return false;
    //         }else{
    //             ++imstr;
    //             ++istr;
    //             continue;
    //         }   
    //     }

    //     ++imstr;
    //     ++istr;
	
    // }

    // for(int i = 0 ; i < password.length() ; i++){

    //     cur_str.push_back(password[i]);

    //     cout<<"cur str push back "<<password[i]<<endl;

    //     for(int j = i+1 ; j < password.length() ; j++){

    //         if(password[j] == password[i]  ){

    //             mask_str.push_back(password[j]);

    //             cout<<"mask str push back "<<password[j]<<endl;

    //             if(password[i + 1] == password[j + 1]){

    //                 cur_str.push_back(password[i + 1]);
    //                 mask_str.push_back(password[j + 1]);
    //                 cout<<"first two str push back "<<password[j + 1]<<endl;

    //                 int m = 1 ;

    //                 while( cur_str == mask_str ){

    //                     m++;

    //                     if( password.find(password[j + m]) != password.npos){

    //                         break;

    //                     }

    //                     cur_str.push_back(password[i + m]);
    //                     mask_str.push_back(password[j + m]);
    //                     cout<<"two str push back "<<password[j + m]<<endl;

    //                 }

    //                 if(cur_str.size() - 1 > 2){

    //                     cout<<"now cur str is "<<cur_str<<endl;

    //                     return false;

    //                 }else{

    //                     mask_str.clear();
    //                     continue;

    //                 }
    //             }

    //         }else{

    //             continue;

    //         }

    //     }
    //     cur_str.clear();
    //     mask_str.clear();

    // }

    return true;

}

int main() {
    
    string str;
    while(cin >> str){

        if(IsPassWordOk(str)){
            cout<<"OK"<<endl;
        }else{
            cout<<"NG"<<endl;
        }

        str.clear();

    }


}



// 64 位输出请用 printf("%lld")

此处调用substr函数:

string str="where does father go";
    string str2 = str.substr (3,5); //两个参数版本(推荐)从位置3开始的5个字符      
 
    string::size_type pos = str.find("does"); 
    string str3 = str.substr (pos);//一个参数版本,从位置pos开始知道最后

可以理解为一个滑动窗口问题

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值