密码要求:
1.长度超过8位
2.包括:大写字母/小写字母/数字/其它符号,以上四种至少三种
3.不能分割出两个相等的长度大于 2 的子串,例如 abcabc 可以分割出两个 abc,不合法,ababa 则无法分割出2个aba。
注:其他符号不含空格或换行
数据范围:输入的字符串长度满足
密码要求:
1.长度超过8位
2.包括:大写字母/小写字母/数字/其它符号,以上四种至少三种
一组字符串。
如果符合要求输出:OK,否则输出NG
021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000 021Abc1111
OK NG NG OK OK
import sys # 判断密码是否合法 def is_legal(str1): lst1 = list(str1) # pop掉最后一个换行符 lst1.pop() # 长度判断 if len(lst1) <= 8: return 'NG' # 公共子串判断 for i in range(len(lst1)-2): # 长度为3的子串, 重复出现则返回NG if str1[i:i+3] in str1[i+1:]: return 'NG' # 四种情况判断 kind_dict = {'dig': 0, 'upper': 0, 'lower': 0, 'other': 0} for i in lst1: # 数字 if i.isdigit(): kind_dict['dig'] = 1 # 大写字母 elif ord('Z') >= ord(i) and ord('A') <= ord(i): kind_dict['upper'] = 1 # 小写字母 elif ord('z') >= ord(i) and ord('a') <= ord(i): kind_dict['lower'] = 1 # 其他 else: kind_dict['other'] = 1 # 满足三种以上 if sum(list(kind_dict.values())) >= 3: return 'OK' else: return 'NG' # 读取 lines = list(sys.stdin.readlines()) for i in lines: print(is_legal(i))
def check_(s): alpha=0 num=0 ALPHA=0 other=0 s=s.split() s=s[0] if len(s)<=8: print('NG') return None s2=s[:] c1=0 c2=1 for i in s: if 'a'<=i<='z': alpha=1 s=s[1:] if '0'<=i<='9': num=1 s=s[1:] if 'A'<=i<='Z': ALPHA=1 s=s[1:] if len(s)>0: other=1 if alpha+num+ALPHA+other>=3: c1=1 for i in range(3,len(s2)): if len(s2.split(s2[i-3:i]))>2: c2=0 break if c1+c2==2: print('OK') else: print('NG') import sys for line in sys.stdin: line=line[:-1] check_(line)
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author Yuliang.Lee * @version 1.0 * @date 2021/8/29 9:46 * 校验密码有效性: 密码要求: 1.长度超过8位 2.包括大小写字母.数字.其它符号,以上四种至少三种 3.不能有相同长度大于2的子串重复 示例1 输入: 021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000 输出: OK NG NG OK */ public class Main { public static String REGEX_1 = "[A-Z]"; public static String REGEX_2 = "[a-z]"; public static String REGEX_3 = "\\d"; public static String REGEX_4 = "[^A-Za-z0-9]"; public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { String password = in.nextLine(); if (password.length() <= 8) { System.out.println("NG"); continue; } int countRegex = 0; if (hasRegex(REGEX_1, password)) { countRegex++; } if (hasRegex(REGEX_2, password)) { countRegex++; } if (hasRegex(REGEX_3, password)) { countRegex++; } if (hasRegex(REGEX_4, password)) { countRegex++; } if (countRegex < 3) { System.out.println("NG"); continue; } if (isSubRepeat(password)) { System.out.println("NG"); continue; } System.out.println("OK"); } } public static boolean hasRegex(String regex, String input) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input); if (matcher.find()) { return true; } return false; } /** * 检查是否存在长度为3的多个相同的子串 * @param input * @return */ public static boolean isSubRepeat(String input) { for (int i = 0; i < input.length() - 3; i++) { // 如果用模式regex相关的话还需要处理转译字符 if (input.substring(i + 1).contains(input.substring(i, i + 3))) { return true; } } return false; } }
import java.util.*; public class Main{ public static Boolean checkPassword(String str){ // 验证长度 if(str.length() <= 8) return false; // 验证包括的字符种类 int low = 0, up = 0, num = 0, other = 0; for(int i = 0; i < str.length(); i++){ char tmp = str.charAt(i); if(tmp >= 'a' && tmp <= 'z'){ low = 1; } else if(tmp >= 'A' && tmp <= 'Z'){ up = 1; } else if(tmp >= '0' && tmp <= '9'){ num = 1; } else{ other = 1; } } if(low+up+num+other < 3) return false; // 验证是否有长度大于2的子串重复 for(int i = 0; i < str.length(); i++){ for(int j = i+3; j < str.length(); j++){ String tmp = str.substring(i, j); if(str.substring(j).contains(tmp)){ return false; } } } // 以上false都没有出现,返回true return true; } public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String str = sc.next(); if(checkPassword(str)){ System.out.println("OK"); } else{ System.out.println("NG"); } } } }
let line const getTypes = password => { let count = 0; /[0-9]/.test(password) && count++; /[a-z]/.test(password) && count++; /[A-Z]/.test(password) && count++; /[^A-Za-z0-9]/.test(password) && count++; return count; } const hasRepeat = password => { let arr = password.split('') for(let i = 0; i < password.length - 2; i++) { let subStr = arr.splice(i, 3, ' ').join('') if(arr.join('').includes(subStr)) return false arr = password.split('') } return true } while(line = readline()) { let length = line.length > 8 let type = getTypes(line) >= 3 let repeat = hasRepeat(line) let ans = (length && type && repeat) ? 'OK' : 'NG' print(ans) }
import java.util.Scanner; import java.util.HashSet; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String str = sc.nextLine(); boolean reslut = true; HashSet<String> hm = new HashSet(); if(str.length() <= 8){ //长度小于8 reslut = false; }else{//判断包含大小写,数字,特殊符号 // -1为保留尾部空元素 if (str.split("\\d",-1).length >= 2) hm.add("number"); if (str.split("[a-z]",-1).length >= 2) hm.add("lower"); if (str.split("[A-Z]",-1).length >= 2) hm.add("Upper"); if (str.split("[\\W_]",-1).length >= 2) hm.add("ha?"); } if(hm.size() < 3){//少于三种 reslut = false; } //判断是否有重复字符串 for (int i = 0; i <= str.length()-3; i++){ String string = str.substring(i,i+3); if(str.substring(i+1).contains(string)){ reslut = false; } } if(reslut){ System.out.println("OK"); }else{ System.out.println("NG"); } } } }
import sys import re def func(s): if len(s) <=8 : return 'NG' arr = [0, 0, 0, 0] for c in s: if c.isupper(): arr[0] = 1 elif c.islower(): arr[1] = 1 elif c.isdigit(): arr[2] = 1 else: arr[3] = 1 if sum(arr) < 3: return 'NG' if re.findall(r'(.{3,}).*\1', s): return 'NG' return 'OK' for line in sys.stdin: print(func(line.strip()))
public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ String str = in.nextLine(); //条件一 if(str.length() <= 8){ System.out.println("NG"); continue; } boolean[] flag = new boolean[4];//判断条件二 HashSet<String> set = new HashSet<>();//判断条件三 boolean is = true;//记录for循环里是否有输出 for(int i = 0;i < str.length();i++){ char c = str.charAt(i); if(i + 2 < str.length()){ String cur = str.substring(i,i + 3); if(set.contains(cur)){ System.out.println("NG"); is = false; break; }else{ set.add(cur); } } if(c >= '0' && c <= '9'){ flag[0] = true; }else if(c >= 'a' && c <= 'z'){ flag[1] = true; }else if(c >= 'A' && c <= 'Z'){ flag[2] = true; }else{ flag[3] = true; } } if(!is){//for循环里有输出 continue; } int count = 0; for(int i = 0;i < flag.length;i++){ if(flag[i] == false){ count++; } } if(count > 1){ System.out.println("NG"); continue; } System.out.println("OK"); } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.HashSet; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String passWord; while((passWord = br.readLine()) != null){ if(judge(passWord)) System.out.println("OK"); else System.out.println("NG"); } } private static boolean judge(String pwd) { // 长度不足8 if(pwd.length() <= 8) return false; int lowerAlphaCnt = 0, upperAlphaCnt = 0, digit = 0, other = 0; for(int i = 0; i < pwd.length(); i++){ if(pwd.charAt(i) >= 'A' && pwd.charAt(i) <= 'Z') upperAlphaCnt ++; else if(pwd.charAt(i) >= 'a' && pwd.charAt(i) <= 'z') lowerAlphaCnt ++; else if(pwd.charAt(i) >= '0' && pwd.charAt(i) <= '9') digit ++; else other ++; } // 字符种类不足三种 if(Math.min(lowerAlphaCnt, 1) + Math.min(upperAlphaCnt, 1) + Math.min(digit, 1) + Math.min(other, 1) < 3) return false; // 看是否有相同长度大于2的子串重复 HashSet<String> set = new HashSet<>(); for(int len = 3; len < pwd.length() / 2; len++){ for(int i = 0; i <= pwd.length() - len; i++){ String substr = pwd.substring(i, i + len); if(!set.contains(substr)){ set.add(substr); }else{ // 发现已经有这个子串了 return false; } } } return true; } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String pwd = in.nextLine(); HashMap<Integer,Boolean> map = new HashMap<>(4); //1.长度超过8位 if(pwd.length() <= 8){ System.out.println("NG"); continue; } //2.包括大小写字母.数字.其它符号,以上四种至少三种 for(int i = 0; i < pwd.length(); i++){ char c = pwd.charAt(i); //有三种了 if(map.size() == 3) break; //小写字母 if(c >= 'a' && c <= 'z'){ map.put(0,true); } //大写字母 else if(c >= 'A' && c <= 'Z'){ map.put(1,true); } //数字 else if(c >= '0' && c <= '9'){ map.put(2,true); } else{ map.put(3,true); } } if(map.size() < 3) { System.out.println("NG"); continue; } //3. 不能有相同长度大于2的子串重复。 //也就是说在这里,我们就只需要考虑长度为3的子字符串,是否有重复 boolean flag = false; for(int j = 0; j <= pwd.length() - 6; j++){ String sub = pwd.substring(j , j+3); if(pwd.indexOf(sub,j+3) != -1) { flag = true; break; } } if(flag){ System.out.println("NG"); continue; } System.out.println("OK"); } } }
3.不能有相同长度大于等于2的子串重复
上面测试样本有长度为2的重复, 给的结果还是 "OK"4@M$68(Oh%!n%~9&08&Z@#+dN0&Z
int main() { string a; while(cin>>a) { int flat[4]={0}; if(a.size()<=8) { cout<<"NG"<<endl; continue; } else { int j=0; for(int i=0;i<a.size();i++) { if(isdigit(a[i])) flat[0]=1; else if(islower(a[i])) flat[1]=1; else if(isupper(a[i])) flat[2]=1; else flat[3]=1; if(flat[0]+flat[1]+flat[2]+flat[3]==3) break; } if(flat[0]+flat[1]+flat[2]+flat[3]==3) { int i=2; for(;i<a.size()-2;i++) { if(a.find(string(a.begin()+i-2,a.begin()+i+1),i+1)!=string::npos) { cout<<"NG"<<endl; break; } } if(i==a.size()-2) cout<<"OK"<<endl; } else cout<<"NG"<<endl; } } return 0; }先检测长度,然后检测字符是否三种以上,然后检测重复,都满足则欧克
#include <stdio.h> (737)#include <string.h> #include <ctype.h> int main() { int i,j,n; char keywords[128]={0}; int num[4]={0}; sc: while(scanf("%s",keywords) != EOF) { n = strlen(keywords); num[0] = num[1] = num[2] = num[3] = 0; if( n <= 8) { printf("NG\n"); continue; } for(i=0; keywords[i]!=0; i++) { if(islower(keywords[i]) ) num[0]= 1; else if(isupper(keywords[i])) num[1]= 1; else if(isdigit(keywords[i])) num[2]= 1; else if(isgraph(keywords[i])) num[3]= 1; } if(num[0]+num[1]+num[2]+num[3] < 3) { printf("NG\n"); continue; } for(i=0; i+2 < n; i++) for(j=1; j+2 < n;j++) if(i!=j && keywords[i] == keywords[j] && keywords[i+1] == keywords[j+1] \ && keywords[i+2] == keywords[j+2]) { printf("NG\n"); goto sc; } printf("OK\n"); } return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String x = sc.nextLine(); int[] flag = new int[4]; if (x.length() <= 8) { System.out.println("NG"); continue; } for (int i = 0; i < x.length(); i++) { if (x.charAt(i) >= 'a' && x.charAt(i) <= 'z') { flag[0] = 1; } else if (x.charAt(i) >= 'A' && x.charAt(i) <= 'Z') { flag[1] = 1; } else if (x.charAt(i) >= '0' && x.charAt(i) <= '9') { flag[2] = 1; } else { flag[3] = 1; } } if (flag[0] + flag[1] + flag[2] + flag[3] < 3) { System.out.println("NG"); continue; } boolean valid = true; for1: for (int i = 0; i <= x.length() - 6; i++) { for2: for (int j = i + 3; j <= x.length() - 3; j++) { if (x.substring(i, i + 3).equals(x.substring(j, j + 3))) { System.out.println("NG"); valid = false; break for1; } } } if (valid) { System.out.println("OK"); } } } }
#include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<sstream> #include<algorithm> using namespace std; bool beyond2(string s) { int len=s.length(); int flag=0; for(int i=0; i<len-3; i++) { for(int j=i+3; j<=len-3; j++) { string temp1=s.substr(i,3); string temp2=s.substr(j,3); if(temp1==temp2) { flag=1; // cout<<"temp1: "<<temp1<<" temp2: "<<temp2<<endl; break; } } if(flag==1) break; } if(flag==0) return true; //cout<<"OK"<<endl; else return false; //cout<<"NG"<<endl; } bool beyond8(string s) { int len=s.length(); if(len>8) return true; return false; } bool isAa(string s) { int len=s.length(); int sum=0; int flag0=0,flag1=0,flag2=0,flag3=0,flag4=0; for(int i=0; i<len; i++) { if(s[i]>='a'&&s[i]<='z') flag0=1; else if(s[i]>='A'&&s[i]<='Z') flag1=1; else if(s[i]>='0'&&s[i]<='9') flag2=1; else if(s[i]>='a'&&s[i]<='z') flag3=1; else flag4=1; } if((flag0+flag1+flag2+flag3+flag4)>=3) return true; return false; } int main() { string s; while(getline(cin,s)) { if(beyond8(s)&&beyond2(s)&&isAa(s)) { cout<<"OK"<<endl; } else cout<<"NG"<<endl; } return 0; }
import java.util.Scanner; import java.util.TreeMap; import java.util.TreeSet; import java.util.regex.Pattern; public class Main { public static boolean checkLength(String password) { if (password.length() > 8) return true; else return false; } public static boolean checkCharKinds(String password) { int Digit = 0, lowercase = 0, uppercase = 0, others = 0; Pattern pattern1 = Pattern.compile("[0-9]*");// 正则表达式 Pattern pattern2 = Pattern.compile("[a-z]*");// 正则表达式 Pattern pattern3 = Pattern.compile("[A-Z]*");// 正则表达式 for (int i = 0; i < password.length(); i++) { if (pattern1.matcher(String.valueOf(password.charAt(i))).matches()) Digit = 1; else if (pattern2.matcher(String.valueOf(password.charAt(i))).matches()) lowercase = 1; else if (pattern3.matcher(String.valueOf(password.charAt(i))).matches()) uppercase = 1; else others = 1; } if ((Digit + lowercase + uppercase + others) >= 3) return true; else return false; } public static boolean checkCharRepeat(String password) { for (int i = 0; i < password.length() - 2; i++) { String substr1 = password.substring(i, i + 3); if (password.substring(i + 1).contains(substr1)) return false; } return true; } public static void main(String[] args) { Scanner cin = new Scanner(System.in); while (cin.hasNextLine()) { String psw = cin.nextLine(); if (checkLength(psw) && checkCharKinds(psw) && checkCharRepeat(psw)) System.out.println("OK"); else System.out.println("NG"); } } }