题解 | #密码验证合格程序#

密码验证合格程序

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

  1. 可以使用位运算记录字符种类
  2. 判断是否含有重复子串的时候,只需要判断长度为3的子串即可。
#include<bits/stdc++.h>

using namespace std;


inline bool SubStringDetermine(string s)
{
    unordered_map<string, int> hash;
    // i : length of sub string
    //for (int len = 3; len < s.size() - 1; len++)
    int len = 3;
    {
        for (int i = 0; i < s.size() - len + 1; i++)
        {
            string sub = s.substr(i, len);
            hash[sub] = hash[sub] + 1;
            if (hash[sub] > 1) 
                return false;
        }
    }
    return true;
}

inline int lowbit(int x)
{
    return x & (-x);
}

inline bool TypeDetermine(string s)
{
    int cnt = 0;
    for (auto c : s)
    {
        if (isupper(c))
            cnt |= 1;
        else if (islower(c))
            cnt |= (1 << 1);
        else if (isdigit(c))
            cnt |= (1 << 2);
        else 
            cnt |= (1 << 3);
    }
    // count 1
    int type = 0;
    for (; cnt; cnt -= (lowbit(cnt)))
        type++;
    return type >= 3;
}

inline bool SizeDetermine(string s)
{
    return s.size() > 8;
}

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    string s;
    for (;getline(cin, s);)
    {
        if (SizeDetermine(s) && TypeDetermine(s) && SubStringDetermine(s)) 
            cout << "OK" << endl;
        else 
            cout << "NG" << endl;
    }
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-03 18:22
投了几百份简历,专业和方向完全对口,都已读不回。尝试改了一下学校,果然有奇效。
steelhead:这不是很正常嘛,BOSS好的是即便是你学院本可能都会和聊几句,牛客上学院本机会很少了
点赞 评论 收藏
分享
看到这个内容真是闹麻了。。。。。。现在有了AI以后很多人面试都会作弊吗?&nbsp;那对老老实实面试的人岂不是不公平....
程序员牛肉:公平那是对小孩子讲的童话故事,成年人的世界只有能不能接受失败的后果。 你要是能接受面试作弊被发现之后多家公司联合永久拉黑的后果,你就搞。
点赞 评论 收藏
分享
06-05 19:46
已编辑
武汉大学 后端
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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