密码验证合格程序--牛客华为

1 篇文章 0 订阅

题目描述
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有长度超过2的子串重复

输入描述:
一组或多组长度超过2的子符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NG

c++

#include<iostream>
#include<map>
#include<string>
using namespace std;
//统计长度超过2的子串重复
int sub(string str1)
{
	int i, j, k=1;
	int len;
	string tmp;
	map<string, int>substr1;
	len = str1.size();
	for (i = 0;i+3<len+1; i++)//此处讨巧直接统计长度为3的子串,速度为对应j属于(3,len)的1/5
		{
			tmp = str1.substr(i,3);
			if (substr1.find(tmp) == substr1.end())
				substr1[tmp] = 1;
			else
				substr1[tmp] += 1;
		}
	int nsize = substr1.size();
	map<string ,int>::iterator iter;
	for (iter = substr1.begin(); iter != substr1.end(); iter++)
	{
		if (iter->second > 1)
			k=-1;
	}
	return k;
}
//统计字符串的长度、种类问题
int kinds(string str2)
{
	int i, m,sum=0;
	int a[4] = { 0 };
	int tmp;
	m = str2.length();
	if ( m > 8)
	{
		for (i = 0; i < m; i++)
		{
		    tmp = int(str2[i]);
			if (47<tmp&&tmp<58)
				a[0] +=1;
			else if (64 < tmp&&tmp < 91)
				a[1] += 1;
			else if (96 < tmp&&tmp < 123)
				a[2] += 1;
			else
				a[3] += 1;
		}
	}
	for (i = 0; i < 4; i++)
	{
		if (a[i]==0)
			sum+=1;
	}
	if (sum<2)
		return 1;
	else
		return -1;
}
int main()
{
	string str;
	while (cin >> str)
	{
		if (sub(str) == 1 && kinds(str) == 1)
			cout << "OK" << endl;
		else
			cout << "NG" << endl;
	}
	system("pause");
	return 0;
}

Python:

链接:https://www.nowcoder.com/questionTerminal/184edec193864f0985ad2684fbc86841
来源:牛客网

import re
try:
    while True:
        s = input()
         
        a = re.findall(r'(.{3,}).*\1', s)
        b1 = re.findall(r'\d', s)
        b2 = re.findall(r'[A-Z]', s)
        b3 = re.findall(r'[a-z]', s)
        b4 = re.findall(r'[^0-9A-Za-z]', s)
 		if ([b1, b2, b3, b4].count([]) <= 1 and a == [] and len(s) > 8):
        	print( 'OK' )
        else :
        	print('NG')
except:
    pass
链接:https://www.nowcoder.com/questionTerminal/184edec193864f0985ad2684fbc86841
来源:牛客网

import re,sys
for i in sys.stdin.readlines():
    print("OK" if len(i.strip())>8 and sum([1 if re.search(r"[A-Z]",i.strip()) else 0,\
    1 if re.search(r"[a-z]",i.strip()) else 0,1 if re.search(r"[0-9]",i.strip()) else 0,\
    1 if re.search(r"[^0-9a-zA-Z]",i.strip()) else 0])>2 and\
     sum(map(lambda c:i.strip().count(i.strip()[c:c+3])>1,range(1,len(i.strip())-3)))==0 else "NG")

python中re模块的使用:https://www.xuebuyuan.com/2042477.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值