题目描述
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
说明:长度超过2的子串
输入描述:
一组或多组长度超过2的子符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NG
Java code:只通过了90%,10%没找见哪里出了毛病,尴尬。
import java.util.Scanner;
public class Main20 {
static boolean len(String s){ //长度监测
if (s.length()>8) {
return true;
} else {
//System.out.println("length");
return false;
}
}
static boolean type(String s){ //组合检查
char temp [] ;
temp = s.toCharArray();
int flag[] = {0,0,0,0};
//boolean big = false,small = false,num = false,other = false;
for (int i = 0; i < temp.length; i++) {
if ((int)temp[i] >= 65 && (int)temp[i] <= 90) {
//System.out.println("BIG");
flag[0] = 1;
}else if ((int)temp[i] >= 97 && (int)temp[i] <= 122) {
//System.out.println("SMALL");
//small = true;
flag[1] = 1;
}else if ((int)temp[i] >=48 &&(int)temp[i] <= 57) {
//System.out.println("NUM");
//num = true;
flag[2] = 1;
}else {
//System.out.println("OTHER");
//other = true;
flag[3] = 1;
}
}
//System.out.println(""+big + small+num+other);
if ((flag[0]+flag[1]+flag[2]+flag[3])>=3) {
return true;
} else {
return false;
}
// if ((big&&small&&num&&other) || (big||(small&&num&&other))
// || (small||(big&&num&&other)) || (num||(big&&num&&other)) || (other||(big&&num&&small))) {
// return true;
// } else {
// System.out.println("type");
// return false;
// }
}
static boolean sub(String s){ //子串检查
int k = 0;
int max = 0;
for (int i = 1; i < s.length(); i++) {
for (int j = 0; j < s.length() - i; j++) {
if (s.charAt(j) == s.charAt(i + j)) {
k++;
} else {
k = 0;
}
if (k > max) {
max = k;
} else{max =1;}
}
}
if (max > 2) {
//System.out.println(max);
//System.out.println("sub");
return false;
}else{
//System.out.println(max);
return true;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNextLine()) {
String str = scan.nextLine();
if (len(str)&&type(str)&&sub(str)) {
System.out.println("OK");
} else {
//len(str);type(str);sub(str);
System.out.println("NG");
}
}
}
}
idea:
把每个条件限制写成一个函数,第三个是难点,我觉得那10%出在这个上面。第三个是求最长重复子串!