题目链接

​https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841?tpId=37&tqId=21243&tPage=1&rp=&ru=/ta/huawei&qru=/ta/huawei/question-ranking​

题目描述

密码要求:

1.长度超过8位

2.包括大小写字母.数字.其它符号,以上四种至少三种

3.不能有相同长度超2的子串重复

说明:长度超过2的子串

输入描述:

一组或多组长度超过2的子符串。每组占一行

输出描述:

如果符合要求输出:OK,否则输出NG

示例1

输入

复制

021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000

输出

复制

OK
NG
NG
OK

题解:

#include <iostream>
#include <string>
using namespace std;
int sum(int a[4]){
int ans = 0;
for(int j = 0; j < 4; j++){
ans += a[j];
}
return ans;
}
int main(){
string s;
while(cin >> s){
int buf[100];
bool is_ok = true;
int buf_num = 0;
int is_full[4] = {0,0,0,0};
int s_l = s.length();
for(int i = 0; i < s_l; i++){
buf[buf_num++] = s[i];
if(s[i] >= '0' && s[i] <= '9'){
is_full[0] = 1;
}
else if(s[i] >= 'a' && s[i] <= 'z'){
is_full[1] = 1;
}
else if(s[i] >= 'A' && s[i] <= 'Z'){
is_full[2] = 1;
}
else{
is_full[3] = 1;
}
}
if(sum(is_full) < 3 || s_l <= 8){
is_ok = false;
}
else if(sum(is_full) >= 3 && s_l > 8){
for(int i = 0; i < buf_num - 3; i++){
for(int j = i + 1; j < buf_num; j++){
if(buf[i] == buf[j] && buf[i + 1] == buf[j + 1] && buf[i + 2] == buf[j + 2]){
is_ok = false;
}
}
}
}
if(is_ok == false){
cout << "NG" << endl;
}
else{
cout << "OK" << endl;
}
}
return 0;
}