密码验证合格程序

题目描述

密码要求:

 

1.长度超过8位

 

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

 

3.不能有相同长度超2的子串重复

 

说明:长度超过2的子串

输入描述:

一组或多组长度超过2的子符串。每组占一行


输出描述:

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

 

输入例子:
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出例子:
OK
NG
NG
OK


思路1:题目有三个要求,一是密码长度要大于8;二是至少包括三种符号;三是长度超过2的子字符串不能有重复。前两个条件好写,主要是判断重复子字符串,这里比较简单的判断方法是从第一个字符开始连续截取长度为三的子字符串,然后判断后面的子字符串是否包含此长度为三的字符串,若包含,则为NG。理由是即便字符串中有长度大于三的重复的字符串,它同时也包含了长度为三的子字符串。
 1 public static boolean judge(String str) {
 2         int length = str.length();
 3         for (int i = 0; i < length; i++) {
 4             for (int j = i + 3; j < length; j++) {
 5                 String substr1 = str.substring(i, j);
 6                 String substr2 = str.substring(j);
 7                 if (substr2.contains(substr1)) {
 8                     return false;
 9                 }
10             }
11         }
12         return true;
13     }

 思路2:也是比较常规的想法,从第一个字符开始,分别与间隔大于等于3的字符比较,若相等,则比较第二个字符,若再相等,继续比较第三个字符...若其中只有一个或者两个相等,则进行还原—还是从第一个字符比较,间隔大于3的字符为还没有与第一个字符比较的字符,描述不太清楚,看代码,参考了网友的:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner in = new Scanner(System.in);
 6         while (in.hasNext()) {
 7             char[] c = in.nextLine().toCharArray();
 8             if (c.length <= 8 || !judge1(c) || !judge2(c)) {
 9                 System.out.println("NG");
10             } else
11                 System.out.println("OK");
12         }
13     }
14 
15     public static boolean judge2(char[] c) {
16         int start1 = 0;//索引为start1的字符分别与间隔>=3的字符比较
17         int start2 = start1+3;//间隔>=3的字符,首次为
18         int count = 0;
19         for (int i = 0; i < c.length; i++) {
20             start1 = i;
21             start2 = start1 + 3;
22             count = 0;
23             while (start1 < start2 && start2 < c.length) {
24                 if (c[start1] == c[start2]) {
25                     count++;
26                     if (count >= 3)
27                         return false;
28                     start1++;
29                     start2++;
30                 } else if (start1 != i) {//若上面if语句执行但方法但还不满足count>=3;此处还原start1,start2为还没与start1比较的第一个字符
31 //                    if (count == 1) {
32 //                        count = 0;
33 //                        start1 = i;
34 //                    } else if (count == 2) {
35 //                        count = 0;
36 //                        start1 = i;
37 //                        start2 -= 1;
38 //                    }
39                     start1=i;//上面两个判断的通用写法
40                     start2=start2-(count-1);
41                     count=0;
42 
43                 } else {
44                     count = 0;
45                     start2++;
46                 }
47             }
48         }
49         return true;
50     }
51 
52     public static boolean judge1(char[] c) {
53         int digit = 0;
54         int Lowercase = 0;
55         int Uppercase = 0;
56         int other = 0;
57         for (int i = 0; i < c.length; i++) {
58             if ('0' <= c[i] && c[i] <= '9') {
59                 digit = 1;
60             } else if ('a' <= c[i] && c[i] <= 'z') {
61                 Lowercase = 1;
62             } else if ('A' <= c[i] && c[i] <= 'Z') {
63                 Uppercase = 1;
64             } else {
65                 other = 1;
66             }
67         }
68         return (digit + Lowercase + Uppercase + other) >= 3;
69     }
70 }

 


posted @ 2016-04-05 15:50  crazybuddy  阅读(1440)  评论(0编辑  收藏  举报