题目地址
题目描述
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度大于2的子串重复
输入描述:
一组或多组长度超过2的子符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NG
示例1
输入
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出
OK
NG
NG
OK
解题思路
按照每个要求,写对应的判断条件就可以。
我这里判断大小写字母使用的查表的方式(查表在 ACM 中很常见)。
另外,判断是否包含其他符号的方式是,移除大小写字母、移除数字后,还剩余的就都是其他字符。
代码
- Python 3
import re
def func():
s = input()
# 1、长度超过8位
if len(s) < 9:
print('NG')
return
# 2、包括大小写字母.数字.其它符号,以上四种至少三种
dic = 'abcdefghijklmnopqrstuvwxyz'
has_upper = False
has_lower = False
has_num = False
has_other = False
ss = s
for c in dic:
if c.upper() in ss:
has_upper = True
ss = ss.replace(c.upper(), '')
if c.lower() in ss:
has_lower = True
ss = ss.replace(c.lower(), '')
if re.findall('\d', ss):
has_num = True
ss = re.sub('\d', '', ss)
if len(ss) > 0:
has_other = True
c = 0
for f in [has_upper, has_lower, has_num, has_other]:
if f:
c += 1
if c < 3:
print('NG')
return
# 3、不能有相同长度大于2的子串重复
for i in range(len(s) - 3):
find_s = s[i:i+3]
if s.count(find_s) > 1:
print('NG')
return
print('OK')
while True:
try:
func()
except:
break