解题思路
-
密码长度验证:
- 检查密码长度是否大于8位
- 如果不满足,直接返回"NG"
-
字符类型验证:
- 检查是否包含大写字母
- 检查是否包含小写字母
- 检查是否包含数字
- 检查是否包含特殊字符
- 统计出现的字符类型数量,必须至少有3种
-
重复子串验证:
- 检查是否存在长度大于2的重复子串
- 使用双重循环遍历所有可能的子串
- 如果找到重复子串,返回"NG"
-
如果所有验证都通过,返回"OK"
算法及复杂度
- 算法:字符串处理 + 暴力匹配
- 时间复杂度:
,其中n是密码长度,主要在查找重复子串时
- 空间复杂度:
,只需要常数级别的额外空间
注意事项
-
字符类型要求:
- 大写字母
- 小写字母
- 数字
- 特殊字符
- 至少包含其中三种
-
重复子串判断:
- 长度必须大于2
- 不能有重复出现的子串
- 如 "abcabc" 不合法,因为 "abc" 重复出现
-
输入输出:
- 每行一个密码字符串
- 对每个密码输出 "OK" 或 "NG"
代码
#include <iostream>
#include <string>
using namespace std;
string checkPassword(const string& password) {
// 1. 检查长度是否大于8
if (password.length() <= 8) {
return "NG";
}
// 2. 检查是否包含至少三种字符类型
bool hasUpper = false;
bool hasLower = false;
bool hasDigit = false;
bool hasSpecial = false;
for (char c : password) {
if (isupper(c)) hasUpper = true;
else if (islower(c)) hasLower = true;
else if (isdigit(c)) hasDigit = true;
else hasSpecial = true;
}
int typeCount = hasUpper + hasLower + hasDigit + hasSpecial;
if (typeCount < 3) {
return "NG";
}
// 3. 检查是否有长度大于2的重复子串
for (size_t i = 0; i < password.length() - 3; i++) {
for (size_t j = i + 3; j < password.length() - 2; j++) {
if (password.substr(i, 3) == password.substr(j, 3)) {
return "NG";
}
}
}
return "OK";
}
int main() {
string password;
while (getline(cin, password)) {
cout << checkPassword(password) << endl;
}
return 0;
}
import java.util.Scanner;
public class Main {
public static String checkPassword(String password) {
// 1. 检查长度是否大于8
if (password.length() <= 8) {
return "NG";
}
// 2. 检查是否包含至少三种字符类型
boolean hasUpper = false;
boolean hasLower = false;
boolean hasDigit = false;
boolean hasSpecial = false;
for (char c : password.toCharArray()) {
if (Character.isUpperCase(c)) hasUpper = true;
else if (Character.isLowerCase(c)) hasLower = true;
else if (Character.isDigit(c)) hasDigit = true;
else hasSpecial = true;
}
int typeCount = (hasUpper ? 1 : 0) +
(hasLower ? 1 : 0) +
(hasDigit ? 1 : 0) +
(hasSpecial ? 1 : 0);
if (typeCount < 3) {
return "NG";
}
// 3. 检查是否有长度大于2的重复子串
for (int i = 0; i < password.length() - 3; i++) {
for (int j = i + 3; j < password.length() - 2; j++) {
if (password.substring(i, i + 3).equals(
password.substring(j, j + 3))) {
return "NG";
}
}
}
return "OK";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String password = sc.nextLine();
System.out.println(checkPassword(password));
}
sc.close();
}
}
def check_password(password):
# 1. 检查长度是否大于8
if len(password) <= 8:
return "NG"
# 2. 检查是否包含至少三种字符类型
has_upper = False
has_lower = False
has_digit = False
has_special = False
for char in password:
if char.isupper():
has_upper = True
elif char.islower():
has_lower = True
elif char.isdigit():
has_digit = True
else:
has_special = True
type_count = sum([has_upper, has_lower, has_digit, has_special])
if type_count < 3:
return "NG"
# 3. 检查是否有长度大于2的重复子串
for i in range(len(password)-3):
for j in range(i+3, len(password)-2):
if password[i:i+3] == password[j:j+3]:
return "NG"
return "OK"
def main():
while True:
try:
password = input().strip()
print(check_password(password))
except EOFError:
break
if __name__ == "__main__":
main()
算法及复杂度
- 算法:字符串处理 + 暴力匹配
- 时间复杂度:
,其中n是密码长度,主要在查找重复子串时
- 空间复杂度:
,只需要常数级别的额外空间