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

密码验证合格程序

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;
}
全部评论

相关推荐

阿武同学:基本信息保留前面三行,其他的可以全部删掉,邮箱最重要的你没写,主修课程精简到8个以内,实习里面2/3/4都是水内容的,非要写的话建议两到三句话,项目经历排版优化下,自我评价缩到三行
点赞 评论 收藏
分享
09-25 00:00
已编辑
电子科技大学 Java
球球与墩墩:这不是前端常考的对象扁平化吗,面试官像是前端出来的 const flattern = (obj) => { const res = {}; const dfs = (curr, path) => { if(typeof curr === 'object' && curr !== null) { const isArray = Array.isArray(curr); for(let key in curr) { const newPath = path ? isArray ? `${path}[${key}]` : `${path}.${key}` : key; dfs(curr[key], newPath); } } else { res[path] = curr } } dfs(obj); return res; }
查看3道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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