题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <cctype> #include <iostream> #include <string> #include <set> using namespace std; int main() { string s; cin>>s; bool valid = true; if(s.size()<=8){//若长度不符合要求 valid = false; }else{ bool haveUpperLetter,haveLowerLetter,haveDigit,haveOther; haveUpperLetter = haveLowerLetter = haveDigit = haveOther = false; set<string> subset;//存放长度为3的子串 for(int i=0;i<s.size();i++){ const char& c = s[i]; if(isdigit(c)){ haveDigit = true; }else if(isupper(c)){ haveUpperLetter = true; }else if(islower(c)){ haveLowerLetter = true; }else{ haveOther = true; } if(i<s.size()-2){ string substr = s.substr(i,3); if(subset.find(substr)!=subset.end()){//若该长度为3的子串重复 valid = false; break; }else{ subset.insert(std::move(substr)); } } } if(haveUpperLetter+haveLowerLetter+haveDigit+haveOther<3){ valid = false; } } if(valid){ cout<<"OK"<<endl; }else{ cout<<"NG"<<endl; } } // 64 位输出请用 printf("%lld")