题解 | 判断长度大于2的包含公共元素的#密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
/**
* @file HJ20(medi-password).cpp
* @author your name (you@domain.com)
* @brief 密码要求:
* 1.长度超过8位
* 2.包括大小写字母.数字.其它符号,以上四种至少三种
* 3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
* @version 0.1
* @date 2023-04-14
*
* @copyright Copyright (c) 2023
*
*/
#include<iostream>
#include<vector>
#include<cctype>
using namespace std;
// 判断不能有长度大于2的包含公共元素的子串重复
// 就是判断字符串是否包含长度超 2 的两个以上相同子串,
// 方法一:暴力匹配:故考虑长度为 3 的子字符串是否有重复即可,
// 从而子字符串有(len - 2)种可能,但作为基准子字符串的只需要(len - 3)个即可
bool isSub1(string s){
for (int i = 0; i < s.size()-3; i ++){
for (int j = i + 3; j < s.size()-3; j++){
// 如果字符串相同
if (0 == s.compare(i, 3, s, j, 3)){
//compare比较,相同为0
return true;
}
}
}
return false;
}
// 方法二:正则:
#include<regex>
// 说实话正则我也没看懂
bool isSub2(string s){
regex pattern(".*(...)(.*\\1).*"); //匹配串前后连续3个字符一样的
if(regex_search(s, pattern))
return true; //有
else
return false; //没有
}
bool check(string s){
if(s.size()<8) return false;
int A=0,b=0,n=0, o=0, count=1;
for(int i =0; i<s.size();++i){
if(s[i]>='A' && s[i]<='Z') A++;
else if(s[i]>='a' && s[i]<='z') b++;
else if(isdigit(s[i])) n++;
else o++;
}
int c=0;
if(A >0) c++;
if(b >0) c++;
if(o >0) c++;
if(n >0) c++;
if(c<3) return false;
if(isSub1(s)) return false;
return true;
}
int main(){
string s;
while(cin>>s){
if(check(s)) cout << "OK" <<endl;
else cout << "NG" <<endl;
}
}
