题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <iostream> using namespace std; #include <bits/stdc++.h> bool isKindValid(string& s){ //bool upper = false, lower = false, digit = false, others = false; vector<bool>k(4,false); for(char c : s){ if(isupper(c)) k[0] = true; else if(islower(c)) k[1] = true; else if(isdigit(c)) k[2] = true; else k[3] = true; } int ret = 0; for(bool ki : k){ if(ki == true) ret++; } return ret >= 3; } bool isContainStr(string s, string sub, int begin, int end){ for(int i = begin; i + 2 <= end; i++){//在[begin,end]间搜索sub if(s[i] == sub[0] && s[i+1] == sub[1] && s[i+2] == sub[2]) return true; } return false; } bool isSameSubstrLengthLess2(string& s){ int n = s.size(); if(n < 6) return true; for(int i = 0; i < n-3; i++){ string sub = s.substr(i,3); if(isContainStr(s,sub,i+1,n-1)) return false; } return true; } int main() { string s; while(cin>>s){ if(s.size() < 8){ cout<<"NG"<<endl; continue; } if(!isKindValid(s)){ cout<<"NG"<<endl; continue; } if(!isSameSubstrLengthLess2(s)){ cout<<"NG"<<endl; continue; } cout<<"OK"<<endl; } return 0; } // 64 位输出请用 printf("%lld")