HJ20.密码验证合格程序

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841?tpId=37&tags=&title=&difficulty=&judgeStatus=&rp=1&gioEnter=menu

HJ20.密码验证合格程序

#include <iostream>
#include <string>
#include <unordered_set>

const std::string OK = "OK";
const std::string NG = "NG";

bool check1(const std::string& s) {
    return s.length() > 8;
}

bool check2(const std::string& s) {
    int type[4] = {0, 0, 0, 0};
    for (auto& i : s) {
        if (i == ' ' || i == '\n') {
            return false;
        }
        if (i >= 'A' && i <= 'Z') {
            type[0] = 1;
        }else if (i >= 'a' && i <= 'z') {
            type[1] = 1;
        }else if (i >= '0' && i <= '9') {
            type[2] = 1;
        }else {
            type[3] = 1;
        }
    }
    if (type[0] + type[1] + type[2] + type[3] < 3) {
        return false;
    }
    return true;
}

bool check3(const std::string& s) {
    std::unordered_set<std::string> sets;
    std::string tmp;
    for (int i = 0; i < s.length() - 2; ++i) {
        tmp = s.substr(i, 3);
        if (sets.find(tmp) == sets.end()) {
            sets.insert(tmp);
        }else {
            return false;
        }
    }
    return true;
}

int main() {
    std::string in;
    while (getline(std::cin, in)) {
        if (check1(in) && check2(in) && check3(in)) {
            std::cout << OK << '\n';
        }else {
            std::cout << NG << '\n';
        }
    }
    return 0;
}

解题思路:

难点1:题目本身,实在没搞清楚“3.不能有长度大于2的不含公共元素的子串重复 (注:其他符号不含空格或换行)”这句话的意思,什么叫“不含公共元素”是什么意思?最后是按照不含长度大于2的重复子串来做的;

难点2:重复子串的存在性判断。需要注意的是,这里只是判断有没有,并不是找出来,另外重复子串的长度是大于2,意味着我们只需要看长度为3的子串即可(如果有长度为4或者以上的子字符串,必然满足长度为3的重复子串存在!”;

难点3:如何快速找到子串吧,一种方式就是两层循环嵌套查找,时间复杂度比较高,题目没有对空间做限制,可以想到使用set快速查找,时间复杂度为O(n),相当于用空间换时间了;

难点4:密码中总共有几种字符类型,还是字典的思想,这里简单用数组来实现;

全部评论
哈希确实巧妙地省了时间,另外想问一个重复子串的判断的问题,如果是0000这种,按照这种hash存储会存两个000,从而算重复。那么按照题意来讲,0000应该被算成重复子串吗?我做的时候是将子串前后分割开分别与子串进行判断,也就是不会将这种情况算作重复最后也通过了全部用例...
点赞
送花
回复
分享
发布于 2022-06-30 23:17
我愿称之为标准答案
点赞
送花
回复
分享
发布于 2023-06-30 16:15 浙江
秋招专场
校招火热招聘中
官网直投

相关推荐

18 3 评论
分享
牛客网
牛客企业服务