题目描述
密码要求:
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