解题思路
这是一个密码强度评估问题,需要根据不同规则对密码进行打分。
实现思路
- 分别统计各类字符的数量
- 根据规则计算各部分得分
- 计算总分并映射到安全等级
代码
#include <iostream>
#include <string>
using namespace std;
class PasswordChecker {
private:
// 检查字符类型
bool isDigit(char c) { return c >= '0' && c <= '9'; }
bool isUpper(char c) { return c >= 'A' && c <= 'Z'; }
bool isLower(char c) { return c >= 'a' && c <= 'z'; }
bool isSymbol(char c) {
return (c >= 0x21 && c <= 0x2F) ||
(c >= 0x3A && c <= 0x40) ||
(c >= 0x5B && c <= 0x60) ||
(c >= 0x7B && c <= 0x7E);
}
public:
string checkPassword(string password) {
int score = 0;
int len = password.length();
int upperCount = 0, lowerCount = 0, digitCount = 0, symbolCount = 0;
// 统计各类字符
for(char c : password) {
if(isUpper(c)) upperCount++;
else if(isLower(c)) lowerCount++;
else if(isDigit(c)) digitCount++;
else if(isSymbol(c)) symbolCount++;
}
// 1. 长度得分
if(len <= 4) score += 5;
else if(len <= 7) score += 10;
else score += 25;
// 2. 字母得分
if(upperCount > 0 && lowerCount > 0) score += 20;
else if(upperCount > 0 || lowerCount > 0) score += 10;
// 3. 数字得分
if(digitCount > 1) score += 20;
else if(digitCount == 1) score += 10;
// 4. 符号得分
if(symbolCount > 1) score += 25;
else if(symbolCount == 1) score += 10;
// 5. 奖励分
if(upperCount > 0 && lowerCount > 0 && digitCount > 0 && symbolCount > 0)
score += 5;
else if((upperCount > 0 || lowerCount > 0) && digitCount > 0 && symbolCount > 0)
score += 3;
else if((upperCount > 0 || lowerCount > 0) && digitCount > 0)
score += 2;
// 返回安全等级
if(score >= 90) return "VERY_SECURE";
if(score >= 80) return "SECURE";
if(score >= 70) return "VERY_STRONG";
if(score >= 60) return "STRONG";
if(score >= 50) return "AVERAGE";
if(score >= 25) return "WEAK";
return "VERY_WEAK";
}
};
int main() {
string password;
while(getline(cin, password)) {
PasswordChecker checker;
cout << checker.checkPassword(password) << endl;
}
return 0;
}
import java.util.*;
public class Main {
static class Solution {
private boolean isDigit(char c) { return c >= '0' && c <= '9'; }
private boolean isUpper(char c) { return c >= 'A' && c <= 'Z'; }
private boolean isLower(char c) { return c >= 'a' && c <= 'z'; }
private boolean isSymbol(char c) {
return (c >= 0x21 && c <= 0x2F) ||
(c >= 0x3A && c <= 0x40) ||
(c >= 0x5B && c <= 0x60) ||
(c >= 0x7B && c <= 0x7E);
}
public String getPasswordLevel(String password) {
int score = 0;
int len = password.length();
int upper = 0, lower = 0, digit = 0, symbol = 0;
// 统计字符
for(char c : password.toCharArray()) {
if(isUpper(c)) upper++;
else if(isLower(c)) lower++;
else if(isDigit(c)) digit++;
else if(isSymbol(c)) symbol++;
}
// 长度分数
if(len <= 4) score += 5;
else if(len <= 7) score += 10;
else score += 25;
// 字母分数
if(upper > 0 && lower > 0) score += 20;
else if(upper > 0 || lower > 0) score += 10;
// 数字分数
if(digit > 1) score += 20;
else if(digit == 1) score += 10;
// 符号分数
if(symbol > 1) score += 25;
else if(symbol == 1) score += 10;
// 奖励分数
if(upper > 0 && lower > 0 && digit > 0 && symbol > 0) score += 5;
else if((upper > 0 || lower > 0) && digit > 0 && symbol > 0) score += 3;
else if((upper > 0 || lower > 0) && digit > 0) score += 2;
// 返回等级
if(score >= 90) return "VERY_SECURE";
if(score >= 80) return "SECURE";
if(score >= 70) return "VERY_STRONG";
if(score >= 60) return "STRONG";
if(score >= 50) return "AVERAGE";
if(score >= 25) return "WEAK";
return "VERY_WEAK";
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()) {
String password = sc.nextLine();
Solution sol = new Solution();
System.out.println(sol.getPasswordLevel(password));
}
}
}
def is_digit(c: str) -> bool:
return '0' <= c <= '9'
def is_upper(c: str) -> bool:
return 'A' <= c <= 'Z'
def is_lower(c: str) -> bool:
return 'a' <= c <= 'z'
def is_symbol(c: str) -> bool:
return (0x21 <= ord(c) <= 0x2F) or \
(0x3A <= ord(c) <= 0x40) or \
(0x5B <= ord(c) <= 0x60) or \
(0x7B <= ord(c) <= 0x7E)
def get_password_level(password: str) -> str:
score = 0
length = len(password)
upper = lower = digit = symbol = 0
# 统计字符
for c in password:
if is_upper(c): upper += 1
elif is_lower(c): lower += 1
elif is_digit(c): digit += 1
elif is_symbol(c): symbol += 1
# 长度分数
if length <= 4: score += 5
elif length <= 7: score += 10
else: score += 25
# 字母分数
if upper > 0 and lower > 0: score += 20
elif upper > 0 or lower > 0: score += 10
# 数字分数
if digit > 1: score += 20
elif digit == 1: score += 10
# 符号分数
if symbol > 1: score += 25
elif symbol == 1: score += 10
# 奖励分数
if upper > 0 and lower > 0 and digit > 0 and symbol > 0: score += 5
elif (upper > 0 or lower > 0) and digit > 0 and symbol > 0: score += 3
elif (upper > 0 or lower > 0) and digit > 0: score += 2
# 返回等级
if score >= 90: return "VERY_SECURE"
if score >= 80: return "SECURE"
if score >= 70: return "VERY_STRONG"
if score >= 60: return "STRONG"
if score >= 50: return "AVERAGE"
if score >= 25: return "WEAK"
return "VERY_WEAK"
# 处理输入
while True:
try:
password = input()
print(get_password_level(password))
except EOFError:
break
算法及复杂度
算法分析
-
字符统计:
- 遍历字符串统计各类字符数量
- 使用独立函数判断字符类型
-
分数计算:
- 按规则累加各部分分数
- 最后根据总分确定安全等级
复杂度分析
- 时间复杂度:
- 其中
为密码长度,需要遍历一次字符串
- 空间复杂度:
- 只需要常量额外空间存储计数器