首页 > 试题广场 >

密码验证合格程序

[编程题]密码验证合格程序
  • 热度指数:398137 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

密码要求:

1.长度超过8位

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

3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)

数据范围:输入的字符串长度满足

输入描述:

一组字符串。



输出描述:

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

示例1

输入

021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000

输出

OK
NG
NG
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))

发表于 2022-07-25 20:33:55 回复(0)
抄大佬的,第二个分割确实很巧妙,大佬们挺强的
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)

发表于 2022-04-15 21:35:09 回复(0)
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;
    }
}


发表于 2021-08-29 12:37:31 回复(0)
class Solution:
    def func(self,m:str):
        dic={}
        a=[0 for _ in range(4)]
        b=[0 for _ in range(len(m)-2)]
        for eachstr in m:
            if not eachstr.isalnum():
                a[0]=1
            elif eachstr.isdigit():
                a[1]=1
            elif eachstr.islower():
                a[2]=1
            elif eachstr.isupper():
                a[3]=1
        for i in range(len(b)):
            b[i]=m[i:i+3]
        if len(b) != len(set(b)):
            return'NG'
        if len(m)<=8:
            return 'NG'
        if a[0]+a[1]+a[2]+a[3]<=2:
            return 'NG'
        return 'OK'
while True:
    try:
        m=input()
        print(Solution().func(m))
    except:
        break
发表于 2021-08-27 17:19:20 回复(0)
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");
            }
        }
    }
}

发表于 2021-07-15 15:37:11 回复(0)
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)
}

编辑于 2021-07-08 11:54:24 回复(0)
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");
            }
        }
    }
}

发表于 2021-06-12 17:12:16 回复(0)
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()))

发表于 2021-05-04 11:20:28 回复(0)
条件三:只需要找有无长度为3的字串重复
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");
        }
    }


发表于 2021-04-18 10:22:10 回复(0)
依题意设立三道检查逻辑即可。麻烦一些的点是检查某个子串是否重复,可以从长度为3的子串开始,利用hash表,在原密码字符串中滑窗检查其是否重复。这里可以对检查的逻辑进行剪枝,超过原来字符串长度一半的子串就不用检查了,这样的子串是不可能重复的。
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;
    }
}


发表于 2021-03-22 14:57:05 回复(0)
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");
        }
    }
}
没什么难点,第三条规则是不能有相同长度大于2的子字符串,那么我们直接看长度为3的子字符串就可以了。个人认为这种长度的子字符串,就不用考虑什么KMP算法了,没太大区别的,当然这里我用的indexOf()本来就是KMP实现的😜
另外一个容易纠结的点就是比如00000这种,是否算重复。本题是不算的,也就是说,两个重复的子字符串不可以重叠。
发表于 2021-01-02 19:20:05 回复(0)
妹的, 是我理解有误还是题出的不对
3.不能有相同长度大于等于2的子串重复
但是测试集实际的要求是"大于2"
4@M$68(Oh%!n%~9&08&Z@#+dN0&Z 
上面测试样本有长度为2的重复, 给的结果还是   "OK"
发表于 2020-07-02 14:44:24 回复(1)
这题特么考输入输出还是算法。。。。。
发表于 2020-04-12 23:55:58 回复(0)
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;
}
先检测长度,然后检测字符是否三种以上,然后检测重复,都满足则欧克
发表于 2020-03-19 12:07:50 回复(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;
}


发表于 2020-03-18 18:08:29 回复(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");
            }
        }
    }
}

发表于 2020-03-07 18:25:31 回复(1)
#include <stdio.h>
(737)#include <string.h>

int main()
{
	char PasswordStr[100] = { 0 };
	bool FlgA = 0, FlgB = 0, FlgC = 0, FlgD = 0, okFlg = 0;
	char RepeatChar[4] = { 0 };//长度为3的重复子串查找,最后为'\0'

	while (scanf("%s", PasswordStr) != EOF)
	{
		//长度超过8位
		if (strlen(PasswordStr) > 8)
		{
			//包括大写字母
			for (int i = 0; i < strlen(PasswordStr); i++)
			{
				if ((PasswordStr[i] >= 'A') && (PasswordStr[i] <= 'Z'))
				{
					FlgA = 1;
					break;
				}
			}
			//包括小写字母
			for (int i = 0; i < strlen(PasswordStr); i++)
			{
				if ((PasswordStr[i] >= 'a') && (PasswordStr[i] <= 'z'))
				{
					FlgB = 1;
					break;
				}
			}
			//包括数字
			for (int i = 0; i < strlen(PasswordStr); i++)
			{
				if ((PasswordStr[i] >= '0') && (PasswordStr[i] <= '9'))
				{
					FlgC = 1;
					break;
				}
			}
			//包括其他符号
			for (int i = 0; i < strlen(PasswordStr); i++)
			{
				if ((!((PasswordStr[i] >= 'A') && (PasswordStr[i] <= 'Z')))
					&& (!((PasswordStr[i] >= 'a') && (PasswordStr[i] <= 'z')))
					&& (!((PasswordStr[i] >= '0') && (PasswordStr[i] <= '9'))))
				{
					FlgD = 1;
					break;
				}
			}
			//包括大小写字母.数字.其它符号,以上四种至少三种
			if ((FlgA + FlgB + FlgC + FlgD) >= 3)
			{
				//不能有相同长度超2的子串重复
				for (int i = 0; i < (strlen(PasswordStr) - 2); i++)
				{
					strncpy(RepeatChar, PasswordStr + i, 3);
					if (strstr(PasswordStr + i + 1, RepeatChar) != NULL)//这里只要向后查找长度等于3的子串,就可以覆盖所有情况了
					{
						okFlg = 0;
						break;
					}
					else
					{
						okFlg = 1;
					}
				}
			}
		}

		if (okFlg == 1)
		{
			printf("%s\n", "OK");
		}
		else
		{
			printf("%s\n", "NG");
		}
		FlgA = 0;
		FlgB = 0;
		FlgC = 0;
		FlgD = 0;
		okFlg = 0;
	}
    
	return 0;
}

发表于 2020-02-24 13:45:40 回复(0)
这道题比较难得就是字符子串超过2的判断吧,两个for循环 第一次循环判断+3对比,之后挨个加,如果有相等的就直接退出打个标签。
#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;
}


发表于 2019-10-04 00:54:10 回复(1)
知识点:
1.cctype.h
   isspace(),isdigit(),isalpha(),ispunct(),islower(),isupper(),tolower(),toupper();
2.string.h
   getline(cin,str),getline(cin,str,ch);   字符串长度: int len=0,while(str[++len]);

解题思路:
1.读取字符串str,计算字符串长度len;
2.利用cctype.h头文件中的函数判断字符类型,定义一个数组类存储字符串中大写字母,小写字母,数字和其他符号的个数;
3.循环判断是否有长度为3的子串重复,先判断子串str1=(str[0],str[1],str[2])和str2=(str[3],str[4],str[5]),然后判断子串str1=(str[1],str[2],str[3])和str2=(str[4],str[5],str[6]),依次循环判断直到字符串尾。(注意str1子串循环范围i:0-len-6,str2子串循环范围j=i+3:len)
4.若len>8,步骤2数组非零取值大于等于3,步骤3条件不出现子串重复,则输出“OK”,否则输出“NG”

C++代码实现:
#include<iostream>
#include<cctype>
#include<string>
using namespace std;
bool code(string str);
bool Substring(string str,int n);
int main()
{
    string str;
    while(getline(cin,str))
    {
        if(code(str))
        {
            cout<<"OK"<<endl;
        }
        else
        {
            cout<<"NG"<<endl;
        }
    }
    return 0;
}
bool code(string str)
{
    int len=0,i;
    while(str[++len]);
    int temp[4]={0,0,0,0};
    for(i=0;i<len;i++)
    {
        if(isdigit(str[i])) temp[0]++;
        else if(islower(str[i])) temp[1]++;
        else if(isupper(str[i])) temp[2]++; 
        else temp[3]++;
    }
    int K=0;
    for(i=0;i<4;i++)
    {
        if(temp[i]>0)
            K++;
    }
    if((K>=3) && (len>8) && (Substring(str,len)))
        return true;
    else
        return false;
}
bool Substring(string str, int n)
{
    bool flag=true;
    for(int i=0;i<= n-6;i++)
    {
        for(int j=i+3;j<n;j++)
        {
            if(str[i]==str[j] && str[i+1]==str[j+1] && str[i+2]==str[j+2])
            {
                flag= false;
                break;
            }
        }
    }
    return flag;
}
发表于 2019-08-30 09:35:34 回复(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");         }     }
}

发表于 2019-06-22 20:41:38 回复(0)