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

密码验证合格程序

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

#include <cctype>
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
bool isValidString(const string& str) {
    unordered_set<string> st;
    int cnt = 0;
  //条件1:字符串长度大于8
    if (str.size() <= 8) {
        return false;
    }
  //条件2:字符串中字符的种类至少有3种
    bool lower = false, big = false, dig = false, symbol = false;
    for (char c : str) 
    {
        if(islower(c))
            lower=true;
        else if(isupper(c))
            big=true;
        else if(isdigit(c))
            dig=true;
        else
            symbol = true;
    }
    if(lower == true)
        cnt++;
    if(big==true)
        cnt++;
    if(dig == true)
        cnt++;
    if(symbol == true)
        cnt++;
    if(cnt < 3)
        return false;
  //条件3:不能有长度大于2的包含公共元素的子串重复 
    for (int i = 0; i < str.length(); ++i) {
        for (int j = i + 1; j < str.length() ; ++j) {
            string sub = str.substr(i,j-i+1);
            if(sub.length() > 2 && st.count(sub))
                return false;
            st.insert(sub);
        }
    }
    return true;
}
int main() {
    string str;
    while (getline(cin, str)) {
        if(isValidString(str)==false)
            cout << "NG" << endl;
        else
            cout<<"OK"<<endl;
    }
    return 0;
}

全部评论

相关推荐

06-28 22:48
已编辑
广东金融学院 Java
小浪_Coding:学院本+这俩项目不是buff叠满了嘛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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