题解 | 密码验证合格程序

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <cctype>
using namespace std;

bool no_same_substr(const string& str) {
    unordered_map<string, int> last_substr_pos; //记录的int是最后一次子串的起始位置
    for(int i = 0; i < str.size() - 2; ++i) {
        string substr = str.substr(i, 3);
        if(last_substr_pos.find(substr) != last_substr_pos.end()) {
            if(i > last_substr_pos[substr] + 2) {
                return false;
            }
        } else {
            last_substr_pos[substr] = i;
        }
    }
    return true;
}

bool has_at_least_three_type(const string& str) {
    int has_digit = 0, has_upper = 0, has_lower = 0, has_special = 0;
    for(const char& c: str) {
        if(isdigit(c)) has_digit = 1;
        else if(isupper(c)) has_upper = 1;
        else if(islower(c)) has_lower = 1;
        else has_special = 1;
        if(has_special + has_digit + has_lower + has_upper >= 3) {
            return true;
        } //在里面可以提前返回 
    } 
    return false;
}

int main() {
    string str;
    vector<string> ans;
    while(cin >> str) {
        if(str.size() < 8) {
            ans.push_back("NG");
            continue;
        }
        if(!has_at_least_three_type(str)) {
            ans.push_back("NG");
            continue;
        }
        if(no_same_substr(str)) {
            ans.push_back("OK");
        } else ans.push_back("NG");
    }
    for(const string& s: ans) {
        cout << s << endl;
    }
    
    return 0;
}

全部评论

相关推荐

爱吃肉的伊登在写日记:好棒,27届简历能做成这个样子,但是第一个项目感觉cover住难度还是不小的,特别是二面的时候肯定要对分布式系统设计这一块儿有高出正常面试者的水平才行
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务