题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <iostream> #include <vector> using namespace std; vector<string> splitStringToMultipleUnits(string strSigned, string strInput) { //分隔生成的列表 vector<string> vectorSplit; //结尾部分重构,添加一个要检索的符合,为了之后递归的方便 string strTotal = strInput + strSigned; //被寻找到被包含字段的位置坐标 size_t nPos = strTotal.find(strSigned); size_t nSize = strTotal.size(); while (nPos != string::npos) { //分隔出的当前段的字符 string strSplit = strTotal.substr(0, nPos); vectorSplit.push_back(strSplit); strTotal = strTotal.substr(nPos + strSigned.size() - 1 + 1, nSize); nPos = strTotal.find(strSigned); } return vectorSplit; } int main() { string strInput = ""; getline(cin, strInput); bool bCheckedFlag = true;//标记是否合格,true 合格 if (strInput.size() <= 8) { bCheckedFlag = false; } else { vector<bool> vecTmpBool(4, false);//前四个 分别代表是否包含大写字母;小写字母;数字;其它符号 for (size_t i = 0; i < strInput.size(); i++) { if ((!vecTmpBool[0]) && 'A' <= strInput[i] && strInput[i] <= 'Z') { vecTmpBool[0] = true; } else if ((!vecTmpBool[1]) && 'a' <= strInput[i] && strInput[i] <= 'z') { vecTmpBool[1] = true; } else if ((!vecTmpBool[2]) && '0' <= strInput[i] && strInput[i] <= '9') { vecTmpBool[2] = true; } else if ((!vecTmpBool[3]) && ((33 <= int(strInput[i]) && int(strInput[i]) <= 47) || (58 <= int(strInput[i]) && int(strInput[i]) <= 64) || (91 <= int(strInput[i]) && int(strInput[i]) <= 96) || (123 <= int(strInput[i]) && int(strInput[i]) <= 126))) { vecTmpBool[3] = true; } } int nNumfortrue = 0; for (bool bVal : vecTmpBool) { if (bVal) { nNumfortrue++; } } nNumfortrue >= 3 ? bCheckedFlag = true : bCheckedFlag = false; if (bCheckedFlag) { //每三个长度的字符串做匹配,如果遇到能匹配上的,则跳出,直接致bCheckedFlag false for (size_t j = 0; j < strInput.size() - 2; j++) { string strSigned = strInput.substr(j, 3); vector<string> vecRes = splitStringToMultipleUnits(strSigned, strInput); if (vecRes.size() >= 3) { bCheckedFlag = false; break; } } } } bCheckedFlag == true ? cout << "OK" << endl : cout << "NG" << endl; } // 64 位输出请用 printf("%lld")