题解 | #迷宫问题#
密码验证合格程序
http://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String str = scan.nextLine();
int [] sum = new int [4];
char [] arrays = str.toCharArray();
for (int i = 0 ; i < arrays.length; i++) {
if (arrays.length < 8) {
break;
}
if (arrays[i] >= '0' && arrays[i] <= '9') {
sum[0] = 1;
}
if (arrays[i] >= 'a' && arrays[i] <= 'z') {
sum[1] = 1;
}
if (arrays[i] >= 'A' && arrays[i] <= 'Z') {
sum[2] = 1;
}
if (arrays[i] == '@' || arrays[i] == '#' ||
arrays[i] == '^' || arrays[i] == '&' || arrays[i] == '*' || arrays[i] == '$' ||
arrays[i] == '%') {
sum[3] = 1;
}
}
int sum2 = 0;
for (int i = 0; i < sum.length; i++) {
sum2 = sum2 + sum[i];
}
if (sum2 < 3) {
System.out.println("NG");
} else {
boolean flag = true;
//判断前后是否有长度大于2的重复字符串,类动态规划
for (int i = 0; i < arrays.length; i++) {//以第i个节点为尾的后字符串
String s = String.valueOf(arrays[i]);//初始化赋值;
loop1:
for (int j = i - 1; j >= i / 2; j--) {//以第i个字符为尾逐1包括前面字符的后字符串;重复字符串长度不会大于前i个字符数的一半
s = arrays[j] + s;
String s2 = "";
//前i个字符中,后字符串前的字符组成的前字符串
for (int k = 0 ; k < j; k++) {
s2 = s2 + arrays[k];
}
if (s2.contains(s) && s.length() > 2) {////判断前字符串是否包含后字符串和后字符串(重复字符串)长度是否大于2
flag = false;
break loop1;
}
}
}
if (flag) {
System.out.println("OK");
} else {
System.out.println("NG");
}
}
}
}
}