题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
对于一个密码验证格式,有3点要求:
- 密码长度大于8 --> 判断code的size
- 必须有大小写,数字,其他字符4种中的3种 --> 遍历,只要多出现一种类型就记录+1
- 判断是否有重复的长度大于2的公共子串
难点在于如何判断公共子串,使用双指针来判断:
主指针:遍历每个字符
副指针:在主指针后3个字符开始,查找有没有相同字符
若有,则开始判断3个长度的主、副子串是否相同。相同则存在,不相同,则移动主指针
若无,接着移动副指针
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
bool find_substr(string code){
    int len = size(code);
    int ind_start = 0;
    while(ind_start < len-5){
        char indCheck = code[ind_start];
        int ind_sub_start = ind_start+3;
        while(ind_sub_start<len-2){
            if(code[ind_sub_start] != indCheck)
                ind_sub_start++;
            else{
                int lenCheck = 3;
                string strCheck = code.substr(ind_start, lenCheck);
                string strSub = code.substr(ind_sub_start, lenCheck);
                if(strCheck == strSub)
                    return true;
                else
                    break;
            }
        }
        ind_start ++;
    }
    return false;
}
bool judge(string code){
    int len = size(code);
    if(len < 8)
        return false;
    else{
        int count = 0;
        bool flag[4] = {false};
        for(int i = 0; i < len; i ++){
            if(code[i] >= '0' && code[i] <= '9')
                flag[0] = true;
            else if(code[i] >= 'A' && code[i] <= 'Z')
                flag[1] = true;
            else if(code[i] >= 'a' && code[i] <= 'z')
                flag[2] = true;
            else
                flag[3] = true;
        }
        for(int i = 0; i < 4; i ++){
            if(flag[i])
                count ++;
        }
        if(count < 3)
            return false;
        
        else{
            if(find_substr(code))
                return false;
            else
                return true;
        }
    }
}
int main() {
    string s;
    while(getline(cin, s)){
        if(judge(s))
            cout << "OK" << endl;
        else
            cout << "NG" << endl;
    }
}
// 64 位输出请用 printf("%lld")

 投递影石Insta360等公司10个岗位
投递影石Insta360等公司10个岗位