HJ20 密码验证合格程序【牛客网】
文章目录
- 零、原题链接
 - 一、题目描述
 - 二、测试用例
 - 三、解题思路
 - 四、参考代码
 
零、原题链接
HJ20 密码验证合格程序
一、题目描述


二、测试用例

三、解题思路
- 基本思路:
对每个字符串验证是否满足那三个条件即可。 - 具体思路: 
- 密码长度必须不少于 
8位,则字符串长度要大于等于8; - 必须包含大写字母、小写字母、数字、特殊字符中的至少三种,则遍历字符串,统计类型的数量,然后判断是否不少于 
3种即可; - 不能分割出两个独立的、长度大于 
2的连续子串,使得这两个子串完全相同;- 遍历字符串,每次取连续的 
3个字符构成字符子串,按照<字符子串,首地址>的形式存放到map中;【如果存在长度大于3的独立字符子串相同,则一定存在长度为3的独立子串相同,所以只要判断是否存在长度为3的字符子串即可】 - 如果已经存在于 
map中,则判断首地址是否相差小于2,小于2,则两个字符子串不是独立的,合法;如果大于等于2,则两个子串独立且相同,则非法;【即ababa可以存在子串aba和aba,但是两个子串不是独立的】 
 - 遍历字符串,每次取连续的 
 
 - 密码长度必须不少于 
 
四、参考代码
时间复杂度: O ( ∑ i = 1 n l i ) \Omicron(\sum\limits_{i=1}^nl_i) O(i=1∑nli)【其中  l i l_i li 是第 i 个字符串的长度,n 表示一共有 n 个字符串】
 空间复杂度: O ( max  i = 1 n l i ) \Omicron(\max\limits_{i=1}^n l_i) O(i=1maxnli)
#include <cctype>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
using namespace std;bool meet3(const string& str) {unordered_map<string, int> m_map;string temp;for (int i = 0; i < str.length() - 2; i++) {temp = str.substr(i, 3);if (m_map.count(temp) == 1) {if (m_map[temp] < i - 2)return false;} else {m_map.emplace(temp, i);}}return true;
}int main() {string str;while (getline(cin, str)) {if (str.length() < 8) {cout << "NG" << endl;continue;}int Alpha = 0, alpha = 0, digit = 0, special = 0;for (int i = 0; i < str.length(); i++) {if (isdigit(str[i]))digit = 1;else if ('A' <= str[i] && str[i] <= 'Z')Alpha = 1;else if ('a' <= str[i] && str[i] <= 'z')alpha = 1;elsespecial = 1;}if (Alpha + alpha + digit + special < 3) {cout << "NG" << endl;continue;}if (meet3(str))cout << "OK" << endl;elsecout << "NG" << endl;}
}
// 64 位输出请用 printf("%lld")
