您的位置:首页 > 编程语言 > C语言/C++

牛客网_华为机试_020_牛客网_密码验证合格程序

2017-07-10 16:18 369 查看


题目描述

密码要求:

1.长度超过8位

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

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

说明:长度超过2的子串

输入描述:

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

输出描述:

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


示例1

输入

021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000


输出

OK
NG
NG
OK


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

思路一:按部就班一个一个要求来验证,验证有没有长度超过3的重复子串只要验证有没有长度为3的子串。可以利用sub_str,find函数来实现,也可以两层for循环比较连续3个字符是否相等

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

bool judge(string str){
if(str.size() <= 8)
return false;

int flag[4] = {0};
for(auto it = str.cbegin(); it != str.cend(); ++it)
{
if(*it > 'a' && *it < 'z')
flag[0] = 1;
else if(*it > 'A' && *it < 'Z')
flag[1] = 1;
else if(*it > '0' && *it < '9')
flag[2] = 1;
else
flag[3] = 1;
}
if(flag[0]+flag[1]+flag[2]+flag[3]<3)
return false;

for(int i = 0; i < str.size()-6; i++)
{
string sub_str = str.substr(i, 3);
if(str.find(sub_str, i + 3) != string::npos)
return false;
}
return true;
}

int main()
{
const string OK = "OK";
const string NG = "NG";
string str = "";
while(cin >> str){
if(judge(str))
cout << OK << endl;
else
cout << NG << endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 算法 华为 牛客网