题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <iostream> #include <vector> #include<unordered_set> using namespace std; bool hasCommonSubstring(const std::string& str) { int len = str.size(); // 比较所有长度大于2的子串 for (int i = 0; i < len - 3; i++) { std::string subStr1 = str.substr(i, 3); // 获取第一个子串 for (int j = i + 1; j < len - 3; j++) { std::string subStr2 = str.substr(j, 3); // 获取第二个子串 // 比较两个子串是否存在公共字符 if (subStr2 == subStr1) { return false; // 存在公共元素,返回true } } } return true; // 不存在公共元素,返回false } int main() { string mima; vector<string> all_mima; while(cin >> mima) { all_mima.push_back(mima); } for (int i = 0;i < all_mima.size();i++) { //cout << all_mima[i] << endl; if(all_mima[i].size() <= 8) { cout << "NG" << endl; continue; } unordered_set<int> set; for (int k = 0; k < all_mima[i].size(); k++) { if (all_mima[i][k] >= 'A' && all_mima[i][k] <= 'Z') set.insert(1); else if (all_mima[i][k] >= 'a' && all_mima[i][k] <= 'z') set.insert(2); else if (all_mima[i][k] >= '0' && all_mima[i][k] <= '9') set.insert(3); else set.insert(4); } if (set.size() < 3) { cout << "NG" << endl; continue; } bool right = hasCommonSubstring(all_mima[i]); if (right) { cout << "OK" << endl; } else { cout << "NG" << endl; } } } // 64 位输出请用 printf("%lld")