题解 | #密码验证合格程序#
密码验证合格程序
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; }